Skip to content

Commit

Permalink
🐛 Autocomplete: handle case all items disabled (#3534)
Browse files Browse the repository at this point in the history
  • Loading branch information
oddvernes authored Jun 19, 2024
1 parent f1f6e46 commit a4c180b
Showing 1 changed file with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,12 @@ type IndexFinderType = <T,>({
index,
optionDisabled,
availableItems,
allDisabled,
}: {
index: number
optionDisabled: AutocompleteProps<T>['optionDisabled']
availableItems: AutocompleteProps<T>['options']
allDisabled?: boolean
calc?: (n: number) => number
}) => number

Expand Down Expand Up @@ -167,7 +169,9 @@ const findNextIndex: IndexFinderType = ({
index,
optionDisabled,
availableItems,
allDisabled,
}) => {
if (allDisabled) return 0
const options = {
index,
optionDisabled,
Expand All @@ -188,7 +192,9 @@ const findPrevIndex: IndexFinderType = ({
index,
optionDisabled,
availableItems,
allDisabled,
}) => {
if (allDisabled) return 0
const options = {
index,
optionDisabled,
Expand Down Expand Up @@ -464,6 +470,8 @@ function AutocompleteInner<T>(
return inputOptions.filter((x) => !disabledItemsSet.has(x))
}, [inputOptions, optionDisabled])

const allDisabled = enabledItems.length === 0

const selectedDisabledItemsSet = useMemo(
() => new Set(selectedItems.filter(optionDisabled)),
[selectedItems, optionDisabled],
Expand Down Expand Up @@ -662,6 +670,7 @@ function AutocompleteInner<T>(
index: changes.highlightedIndex,
availableItems,
optionDisabled,
allDisabled,
}),
}
case useCombobox.stateChangeTypes.InputKeyDownArrowUp:
Expand All @@ -678,6 +687,7 @@ function AutocompleteInner<T>(
index: changes.highlightedIndex,
availableItems,
optionDisabled,
allDisabled,
}),
}
default:
Expand Down Expand Up @@ -725,6 +735,7 @@ function AutocompleteInner<T>(
index: changes.highlightedIndex,
availableItems,
optionDisabled,
allDisabled,
}),
}
case useCombobox.stateChangeTypes.InputKeyDownArrowUp:
Expand All @@ -741,6 +752,7 @@ function AutocompleteInner<T>(
index: changes.highlightedIndex,
availableItems,
optionDisabled,
allDisabled,
}),
}
case useCombobox.stateChangeTypes.InputKeyDownEnter:
Expand Down

0 comments on commit a4c180b

Please sign in to comment.