-
Notifications
You must be signed in to change notification settings - Fork 4.7k
NumberControl: Add TypeScript prop types #43791
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
Conversation
|
Size Change: +2.84 kB (0%) Total Size: 1.25 MB
ℹ️ View Unchanged
|
| }; | ||
|
|
||
| const autoComplete = typeProp === 'number' ? 'off' : null; | ||
| const autoComplete = typeProp === 'number' ? 'off' : undefined; |
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.
null is an invalid value for the autoComplete attribute.
| return ( | ||
| <Root className="components-unit-control-wrapper" style={ style }> | ||
| <ValueInput | ||
| aria-label={ label } |
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.
label variable is a ReactNode, but aria-label only accepts strings. I went ahead and removed this redundant aria-label, because the same value is added on line 270 via label prop.
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.
Noted. Basically the input will be labelled via an actual label element (with the for attribute), instead of the aria-label attribute 👌
|
Going to leave a review at later / early next week, but in the meantime I'm going to leave a link to my previous attempt #38753 as it may be useful |
ciampo
left a comment
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.
Good job! I left a few comments, a good chunk of which are inspired by the previous work in #38753.
Regarding the migration to TypeScript, the main part that still need to be addressed is which type we should assign to the value prop. I wrote a good recap of the situation in #40535 (comment). Although I would understand if we preferred to defer that decision to a separate PR, for the sake of getting this PR merged quickly and unblock further work that depends on NumberControl being typed
| return ( | ||
| <Root className="components-unit-control-wrapper" style={ style }> | ||
| <ValueInput | ||
| aria-label={ label } |
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.
Noted. Basically the input will be labelled via an actual label element (with the for attribute), instead of the aria-label attribute 👌
Co-authored-by: Marco Ciampini <[email protected]>
Yes exactly, the main motivation of this PR is to unblock our way to clearing out the |
|
|
||
| const handleOnChange = ( next: string ) => { | ||
| const handleOnChange = ( next?: string ) => { | ||
| // @ts-expect-error TODO: Investigate if this is problematic |
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.
Should this use ensureNumber ?
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.
This one can't be fixed by ensureNumber because the problem is that next can potentially be undefined.
Technically, we can remove the ts-ignore without changing effective runtime behavior by doing something like this:
let nextValue = typeof next === 'undefined' ? NaN : parseFloat( next );But then again, I really think this one needs further investigation because it's unclear whether it's intended for setValue() to sometimes receive a NaN. It could actually be problematic, so I wouldn't want to imply that this behavior is legit.
| const applyEmptyValue = | ||
| required === false && currentValue === ''; | ||
|
|
||
| // @ts-expect-error TODO: Investigate if this is wrong |
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.
Instead of "Investigate if this is wrong", should the comment be more about the fact that this error will only be solved once we make a decision on the value type?
(same for the rest of the @ts-expect-error comments about the type of the value prop)
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.
Good call, I improved the TODO comments all around. 90948f0
One of the interesting causes was the isValueEmpty() util function. I was able to fix the typing, but it's a bit out of scope for this PR so I'll submit it separately.
ciampo
left a comment
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.
🚀
Good to be merged once we add a CHANGELOG entry
Part of #35744
What?
Adds "provisional" prop types to NumberControl.
Why?
In the past, we've had major complications with properly typing NumberControl due to the string/number ambiguity of the values both upstream and downstream. This is why NumberControl is untyped and
@ts-nochecked at the moment.However, this has started to bottleneck TypeScript migrations for other components that inherit NumberControl props. I think it would be useful to improve the situation a bit by actually defining prop types, even if there are some contradictions that need to be
@ts-ignored.I hope this reads as a constructive baby step forward, rather than a messy pile of bandaids. The benefit is that we can clearly demarcate where the type contradictions are, instead of keeping a huge untyped blackhole in our component chain.
How?
See inline code comments for minor runtime changes.
Testing Instructions
npm run storybook:devand see the Docs view for NumberControl and UnitControl.