-
Notifications
You must be signed in to change notification settings - Fork 4.6k
CustomSelectControl: support generic props type #63985
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
Changes from 6 commits
4f5c724
478581c
7c28ddf
162aa1d
1aaf455
19c5f92
987a017
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ import type { FocusEventHandler, MouseEventHandler } from 'react'; | |
| /** | ||
| * The object structure for the options array. | ||
| */ | ||
| type Option = { | ||
| export type CustomSelectOption = { | ||
| key: string; | ||
| name: string; | ||
| style?: React.CSSProperties; | ||
|
|
@@ -24,15 +24,15 @@ type Option = { | |
| /** | ||
| * The object returned from the onChange event. | ||
| */ | ||
| type ChangeObject = { | ||
| export type CustomSelectChangeObject< T extends CustomSelectOption > = { | ||
|
||
| highlightedIndex?: number; | ||
| inputValue?: string; | ||
| isOpen?: boolean; | ||
| type?: string; | ||
| selectedItem: Option; | ||
| selectedItem: T; | ||
| }; | ||
|
|
||
| export type CustomSelectProps = { | ||
| export type CustomSelectProps< T extends CustomSelectOption > = { | ||
| /** | ||
| * Optional classname for the component. | ||
| */ | ||
|
|
@@ -55,7 +55,7 @@ export type CustomSelectProps = { | |
| * Function called with the control's internal state changes. The `selectedItem` | ||
| * property contains the next selected item. | ||
| */ | ||
| onChange?: ( newValue: ChangeObject ) => void; | ||
| onChange?: ( newValue: CustomSelectChangeObject< T > ) => void; | ||
| /** | ||
| * A handler for `blur` events on the trigger button. | ||
| * | ||
|
|
@@ -83,7 +83,7 @@ export type CustomSelectProps = { | |
| /** | ||
| * The list of options that can be chosen from. | ||
| */ | ||
| options: Array< Option >; | ||
| options: Array< T >; | ||
| /** | ||
| * The size of the control. | ||
| * | ||
|
|
@@ -93,7 +93,7 @@ export type CustomSelectProps = { | |
| /** | ||
| * Can be used to externally control the value of the control. | ||
| */ | ||
| value?: Option; | ||
| value?: T; | ||
| /** | ||
| * Use the `showSelectedHint` property instead. | ||
| * @deprecated | ||
|
|
||

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 we add a default to the generic so any existing consumers will continue working without changing anything?
The same question applies to all instances where we're adding a generic and there wasn't one before.
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.
The generic param is not going to be set manually by user. It's used for type inferring. To be specific, it will use the item type of the
optionsprop automatically. So consumers would not need to change anything.You could checkout this branch and test with the snippets that pasted in the "Testing Instructions" section.
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.
TypeScript should always be able to infer the
Ttype.CustomSelectPropshave a mandatoryoptions: Array<T>field, soTis always the type of whatever you pass asoptions, it's easy to infer. TypeScript will merely check if that type is compatible withCustomSelectOption.