Skip to content
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: improves type safty for autocomplete when optionLabel is required #3408

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,28 @@ const findPrevIndex: IndexFinderType = ({
return prevIndex
}

type OptionLabelProps<T> = T extends string | number
oddvernes marked this conversation as resolved.
Show resolved Hide resolved
? {
/** Custom option label */
optionLabel?: (option: T) => string
/** Disable option
* @default () => false
*/
optionDisabled?: (option: T) => boolean
/** List of options in dropdown */
options: (string | number)[]
}
: {
/** Custom option label */
optionLabel: (option: T) => string
/** Disable option
* @default () => false
*/
optionDisabled?: (option: T) => boolean
/** List of options in dropdown */
options: T[]
}

export type AutocompleteChanges<T> = { selectedItems: T[] }

export type AutocompleteProps<T> = {
Expand Down Expand Up @@ -251,16 +273,10 @@ export type AutocompleteProps<T> = {
multiple?: boolean
/** Add select-all option. Throws an error if true while multiple = false */
allowSelectAll?: boolean
/** Custom option label. NOTE: This is required when option is an object */
optionLabel?: (option: T) => string
/** Custom option template */
optionComponent?: (option: T, isSelected: boolean) => ReactNode
/** Disable use of react portal for dropdown */
disablePortal?: boolean
/** Disable option
* @default () => false
*/
optionDisabled?: (option: T) => boolean
/** Custom filter function for options */
optionsFilter?: (option: T, inputValue: string) => boolean
/** If `true` the width of the dropdown will adjust to the width of the input */
Expand All @@ -279,7 +295,8 @@ export type AutocompleteProps<T> = {
* @default 300
*/
dropdownHeight?: number
} & HTMLAttributes<HTMLDivElement>
} & HTMLAttributes<HTMLDivElement> &
OptionLabelProps<T>

function AutocompleteInner<T>(
props: AutocompleteProps<T>,
Expand Down
Loading