Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Input] Unable to limit length #813

Open
mmatejic opened this issue Mar 10, 2021 · 0 comments
Open

[Input] Unable to limit length #813

mmatejic opened this issue Mar 10, 2021 · 0 comments
Labels
bug Something isn't working @react @momentum-ui/react

Comments

@mmatejic
Copy link

mmatejic commented Mar 10, 2021

Issue Type

Bug Report

Component or Pattern Affected

Input

Expected Behavior

If value is conditionally trimmed/sliced in my onChage function, input should keep that value.

Current Behavior

Looks like the problem is in keeping value in Input's state itself. On value change, It triggers componentDidUpdate twice: first time for prop change, and second time for setState in its onChange func. In second update, prevProps.value and props.value are always equal, therefore input continues to update its value.

Possible Solution

Addition of maxLength prop could solve this, or changing whole logic of updating its value

Steps to Reproduce (for bugs)

  1. Set prop value to value in component's state
  2. On change, do not update state with new one
  3. Value will continue to change
  • Even hardcoding value will only work initially. Afterwards it will have whatever user is typing

onChange = (e) => {
const { value, name } = e.target;

if (value.length > 10) {
e.preventDefault();
}
else {
this.setState({ [name]: value });
}
};

render() {
return < Input name="fieldName" value={this.state.value} onChange={this.onChange} />
}

Context

I'm trying to prevent user from entering x number of characters.

Your Environment

  • Library: React
  • Version used: 23.21.5
  • Browser Name and version: Safari 14.0.3
  • Operating System and version: macOS 11.2.3
  • Link to your project: N/A
@futurethang futurethang added @react @momentum-ui/react bug Something isn't working labels Mar 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working @react @momentum-ui/react
Projects
None yet
Development

No branches or pull requests

2 participants