Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/fix-combobox-auto-focus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@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. Additionally, the refocus logic now properly verifies that the selected item exists in the filtered collection before attempting to focus on it, preventing focus on non-existent keys.
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"
]
}
37 changes: 33 additions & 4 deletions src/components/fields/ComboBox/ComboBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1665,10 +1665,39 @@ export const ComboBox = forwardRef(function ComboBox<T extends object>(

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

if (selectionManager.focusedKey == null) {
const keyToFocus =
effectiveSelectedKey != null ? effectiveSelectedKey : collectFirstKey();
if (keyToFocus != null) selectionManager.setFocusedKey(keyToFocus);
// 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) {
let keyToFocus: Key | null = null;

// First try to focus on the selected key if it exists in the collection
if (
effectiveSelectedKey != null &&
collection.getItem(effectiveSelectedKey) != null
) {
keyToFocus = effectiveSelectedKey;
} else {
// Fall back to the first key in the collection
keyToFocus = collectFirstKey();
}

if (keyToFocus != null) {
selectionManager.setFocusedKey(keyToFocus);
}
}
}, [shouldShowPopover, effectiveSelectedKey]);

Expand Down
Loading