Skip to content

Commit 6921b25

Browse files
committed
Fix: position cursor at end when focusing search input
Prevents text selection when opening select dropdown via typing, allowing users to continue typing without overwriting the first character.
1 parent 39156a7 commit 6921b25

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

packages/components/src/ui/select.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export interface SelectProps<T extends React.Key = string>
5757

5858
// Default search input built on top of CommandInput. Supports cmdk props at runtime.
5959
const DefaultSearchInput = forwardRef<HTMLInputElement, React.ComponentPropsWithoutRef<typeof CommandInput>>(
60-
(props, _ref) => <CommandInput {...props} />,
60+
(props, ref) => <CommandInput {...props} />,
6161
);
6262
DefaultSearchInput.displayName = 'SelectSearchInput';
6363

@@ -97,8 +97,16 @@ export function Select<T extends React.Key = string>({
9797
return;
9898
}
9999
requestAnimationFrame(() => {
100-
if (searchable && searchInputRef.current) {
101-
searchInputRef.current.focus();
100+
if (searchable) {
101+
// Query for the input element since CommandInput uses asChild and ref forwarding is complex
102+
const inputElement = popoverRef.current?.querySelector<HTMLInputElement>('input[type="text"]');
103+
if (inputElement) {
104+
inputElement.focus();
105+
const selectionEnd = inputElement.value.length;
106+
if (selectionEnd > 0) {
107+
inputElement.setSelectionRange(selectionEnd, selectionEnd);
108+
}
109+
}
102110
}
103111
const selectedEl = selectedItemRef.current as HTMLElement | null;
104112
if (selectedEl) selectedEl.scrollIntoView({ block: 'center' });

0 commit comments

Comments
 (0)