Last active
June 6, 2020 12:57
-
-
Save janjakubnanista/398e25c0c693f622ce7fe43fd1f9106d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// We will start by defining the props that both the single | |
// and the multiple versions of our Select have in common | |
export interface BaseSelectProps<T> { | |
items: T[]; | |
idFromValue: (value: T) => string | number; | |
labelFromValue: (value: T) => React.ReactNode; | |
itemComponent: React.ComponentType<SelectItemProps<T>>; | |
} | |
// We then define props specific for the single version | |
export interface SingleSelectProps<T> extends BaseSelectProps<T> { | |
multiple: false; | |
value?: T; | |
onChange: (value: T) => void; | |
} | |
// And props specific for the multiple version | |
export interface MultiSelectProps<T> extends BaseSelectProps<T> { | |
multiple: true; | |
value?: T[]; | |
onChange: (value: T[]) => void; | |
} | |
// Finally we create a type that covers both the single and the multiple version of Select | |
export type SelectProps<T> = MultiSelectProps<T> | SingleSelectProps<T>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment