-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat(SegmentedControl): allow for passing stricter type for options #7051
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
feat(SegmentedControl): allow for passing stricter type for options #7051
Conversation
Generate changelog in
|
|
|
||
| // This allows the ability to pass a more strict type for `options`/`onValueChange` | ||
| // i.e. <SegmentedControl<Intent> /> | ||
| interface ReactFCWithGeneric extends React.FC<SegmentedControlProps> { |
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.
Nit this is a specific interface for SegmentedControl so lets name it something in accordance with that.
| interface ReactFCWithGeneric extends React.FC<SegmentedControlProps> { | |
| interface GenericSegmentedControl extends React.FC<SegmentedControlProps> { |
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.
✔️
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.
Renamed and commented to match the other cases.
| }; | ||
| export const SizeSelect: React.FC<SizeSelectProps> = ({ label, size, optionLabels, onChange }) => ( | ||
| <FormGroup label={label}> | ||
| <SegmentedControl<Size> |
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.
Out of curiosity, in these types of cases if you omit <Size> is the generic type parameter properly inferred from the options array? I'm guessing not because there's no as const.
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'm guessing not because there's no as const.
Correct, it can't be inferred from the options in that case. But it is inferred from the type of the value prop.
| { label: optionLabels[1], value: "regular" }, | ||
| { label: optionLabels[2], value: "large" }, | ||
| ]} | ||
| onValueChange={onChange} |
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.
Another point of curiosity (since we ran into this issue with another generic component recently), if you provide an arrow function here, does the type of the argument get properly inferred?
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.
Yes.
If you specify a type in the generic, it will make sure the onChange arg has the proper type`.
If you don't specify a generic, the arrow function argument is just string
9fe373b to
92d2661
Compare
|
@ggdouglas @jscheiny -- rebased this one. |
6eb66b1 to
ad256bb
Compare
497943c to
bc3e966
Compare
199ce78 to
0eefcda
Compare
|
Going to replace with a newer PR. |
Checklist
Changes proposed in this pull request:
Allow for stricter type on
SegmentedControlthan juststring, which reduces the need for type casting in many cases (such asonChangecall).For example, you can still have just
as before, for general string Options, or you can have
to make sure that all passed option values, and the
onChange, have theOptionsEnumtype.Reviewers should focus on:
Improved type casting abilities, and mainly, the reduction of having to type cast.
Screenshot
No visual changes.