-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
fix(number-input): adjust invalid logic and add proptype check for value #5217
Conversation
Deploy preview for the-carbon-components ready! Built with commit bddf497 https://deploy-preview-5217--the-carbon-components.netlify.com |
Deploy preview for carbon-elements ready! Built with commit 632a0c2 |
Deploy preview for carbon-components-react failed. Built with commit 632a0c2 https://app.netlify.com/sites/carbon-components-react/deploys/5e3992046ac43f00072f542f |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A triple whammy, nice work! Tested a few scenarios and they all work as expected 👍 ✅
@abbeyhrt Thank you for jumping in! Let me take some time to review this given it seems change in some key logic in the component. Don't hesitate to ping me if you don't hear back in a reasonable amount of time. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put a few comments, please don't hesitate to ping me if you have any questions. Thank you again for jumping in @abbeyhrt!
static getDerivedStateFromProps({ min, max, value = 0 }, state) { | ||
const { prevValue } = state; | ||
|
||
if (typeof value === 'string' && value === '' && state.value !== '') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (typeof value === 'string' && value === '' && state.value !== '') { | |
if (!useControlledStateWithValue && value === '' && prevValue !== '') { |
- Returning non-
null
value, which means changing the state to the returning value, whenevervalue
prop is an empty string andvalue
state is not an empty string causes canceling any state update (to non-empty string, e.g. upon up/down buttons or user's type) ifvalue
prop is an empty string. The problem is observed with<NumberInput value="" />
where it doesn't allow user to make any edit at all. Wondering if you simply wanted to detect update invalue
prop to an empty string, which led to my suggestion above. Note thatgDSFP
is called for state update requests in addition to prop updates. useControlledStateWithValue
flag must make the entiregDSFP
no-op (by returningnull
). The purpose of flaguseControlledStateWithValue
is not doing anyvalue
state update fromvalue
proptypeof value === 'string'
is redundant if we are doing strict equal check ofvalue
with an empty string (given strict equal check involves type checking)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@abbeyhrt Any thoughts on this...? Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll make the changes for the last two points, those seem good to me 👍 Could you help me understand your first point? The intent behind it was to prevent anybody from setting the value to anything but an empty string or a number, could you provide steps for me to reproduce the problem if value
is set to an empty string?
Co-Authored-By: Akira Sudoh <asudoh@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @abbeyhrt for all the updates!
Closes #4294
Closes #4997
Closes #4996
Done during mob programming with @joshblack.
This PR adjusts the way that
data-invalid
andaria-invalid
were being set by assigning all invalidation logic toisInputInvalid
and adjusts the min/max valid check so thatallowEmpty
works as expected. This expands the proptype check forvalue
anddefaultValue
to include string so that the allowEmpty state of '' doesn't create a warning and then adds a check ingetDerivedStateFromProps
so that a user can't enter any string except an empty one.Changelog
New
allowEmpty
is trueisInputInvalid
logicChanged
value
anddefaultValue
Removed
Testing / Reviewing
Verify that the Number Input works as expected and that it resolves the issues from #4294, #4997, #4996. Mainly check that if
allowEmpty
is true, the error doesn't show up for the empty state and verify thataria-invalid
anddata-invalid
correspond to when a number is invalid.