Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions .changeset/fix-combobox-auto-focus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@cube-dev/ui-kit": patch
---

Fix ComboBox auto-focus behavior when using `allowsCustomValue`. The component now correctly maintains focus on the first filtered item while typing, allowing Enter key selection to work properly. The focus is automatically re-established when the currently focused item is filtered out of the list.

5 changes: 5 additions & 0 deletions .cursor/worktrees.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"setup-worktree": [
"pnpm install"
]
}
22 changes: 20 additions & 2 deletions src/components/fields/ComboBox/ComboBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1665,10 +1665,28 @@ export const ComboBox = forwardRef(function ComboBox<T extends object>(

if (lastFocusSourceRef) lastFocusSourceRef.current = 'keyboard';

if (selectionManager.focusedKey == null) {
// Check if we need to set or update focus
const currentFocusedKey = selectionManager.focusedKey;
let needsRefocus = false;

if (currentFocusedKey == null) {
// No focus at all - need to set initial focus
needsRefocus = true;
} else {
// Check if currently focused key still exists in the collection
const focusedItemExists = collection.getItem(currentFocusedKey) != null;
if (!focusedItemExists) {
// Focused item was filtered out - need to refocus
needsRefocus = true;
}
}

if (needsRefocus) {
const keyToFocus =
effectiveSelectedKey != null ? effectiveSelectedKey : collectFirstKey();
if (keyToFocus != null) selectionManager.setFocusedKey(keyToFocus);
if (keyToFocus != null) {
selectionManager.setFocusedKey(keyToFocus);
}
}
}, [shouldShowPopover, effectiveSelectedKey]);

Expand Down
Loading