Skip to content

Commit

Permalink
fix: improves type safty for autocomplete when optionLabel is required (
Browse files Browse the repository at this point in the history
  • Loading branch information
FredrikMWold authored and mhwaage committed Jun 11, 2024
1 parent 6bfcb5b commit 54d63ef
Showing 1 changed file with 24 additions and 7 deletions.
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
? {
/** 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

0 comments on commit 54d63ef

Please sign in to comment.