Skip to content

Commit

Permalink
feat(autocomplete): automatically focus first non-disabled item (#2186)
Browse files Browse the repository at this point in the history
Co-authored-by: WK Wong <wingkwong.code@gmail.com>
  • Loading branch information
juliesaia and wingkwong authored Oct 1, 2024
1 parent a4ab020 commit 557f10f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/popular-pigs-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/autocomplete": patch
---

automatically focus first non-disabled item
10 changes: 10 additions & 0 deletions packages/components/autocomplete/src/use-autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,16 @@ export function useAutocomplete<T extends object>(originalProps: UseAutocomplete
}
}, [inputRef.current]);

// focus first non-disabled item
useEffect(() => {
let key = state.collection.getFirstKey();

while (key && state.disabledKeys.has(key)) {
key = state.collection.getKeyAfter(key);
}
state.selectionManager.setFocusedKey(key);
}, [state.collection, state.disabledKeys]);

useEffect(() => {
if (isOpen) {
// apply the same with to the popover as the select
Expand Down

0 comments on commit 557f10f

Please sign in to comment.