Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(input): focus input when clicking anywhere on field wrapper (#1839)

Kenton Quatman09ab4bb67891
diff --git a/packages/core/src/NumberInput/XDSNumberInput.test.tsx b/packages/core/src/NumberInput/XDSNumberInput.test.tsx
index ebb2b30a4d..119760e442 100644
--- a/packages/core/src/NumberInput/XDSNumberInput.test.tsx
+++ b/packages/core/src/NumberInput/XDSNumberInput.test.tsx
@@ -8,7 +8,7 @@
*/
import {describe, it, expect, vi} from 'vitest';
-import {render, screen} from '@testing-library/react';
+import {render, screen, fireEvent} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {HashtagIcon} from '@heroicons/react/24/outline';
import {XDSNumberInput} from './XDSNumberInput';
@@ -632,4 +632,34 @@ describe('XDSNumberInput', () => {
expect(onChange).toHaveBeenCalledWith(null);
});
});
+
+ describe('click-to-focus', () => {
+ it('focuses input when clicking the start icon', () => {
+ render(
+ <XDSNumberInput
+ label="Qty"
+ value={0}
+ onChange={() => {}}
+ startIcon={<HashtagIcon />}
+ />,
+ );
+
+ const input = screen.getByRole('spinbutton');
+ const wrapper = input.parentElement!;
+ const iconElement = wrapper.querySelector('svg')!;
+
+ fireEvent.click(iconElement);
+ expect(input).toHaveFocus();
+ });
+
+ it('focuses input when clicking the wrapper padding', () => {
+ render(<XDSNumberInput label="Qty" value={0} onChange={() => {}} />);
+
+ const input = screen.getByRole('spinbutton');
+ const wrapper = input.parentElement!;
+
+ fireEvent.click(wrapper);
+ expect(input).toHaveFocus();
+ });
+ });
});
diff --git a/packages/core/src/NumberInput/XDSNumberInput.tsx b/packages/core/src/NumberInput/XDSNumberInput.tsx
index 9e145539f6..2ab2e63aed 100644
--- a/packages/core/src/NumberInput/XDSNumberInput.tsx
+++ b/packages/core/src/NumberInput/XDSNumberInput.tsx
@@ -45,6 +45,7 @@ import {
} from '../Field';
import {XDSIcon, renderIconSlot, type XDSIconType} from '../Icon';
import {useXDSSize} from '../SizeContext/XDSSizeContext';
+import {useInputContainer} from '../hooks/useInputContainer';
const styles = stylex.create({
wrapper: {
@@ -364,6 +365,7 @@ export function XDSNumberInput({
const descriptionID = useId();
const statusMessageID = useId();
const inputRef = useRef<HTMLInputElement | null>(null);
+ const containerRef = useRef<HTMLDivElement | null>(null);
// Pending input while user is typing (null = show formatted value)
const [pendingInput, setPendingInput] = useState<string | null>(null);
@@ -508,6 +510,14 @@ export function XDSNumberInput({
inputRef.current?.focus();
}, [hasClear, onChange]);
+ // Focus input when clicking anywhere on the wrapper (icons, padding, etc.)
+ const {onClick: handleWrapperClick, onMouseUp: handleWrapperMouseUp} =
+ useInputContainer({
+ containerRef,
+ inputRef,
+ disabled: isDisabled,
+ });
+
return (
<XDSField
label={label}
@@ -530,6 +540,9 @@ export function XDSNumberInput({
}
labelTooltip={labelTooltip}>
<div
+ ref={containerRef}
+ onClick={handleWrapperClick}
+ onMouseUp={handleWrapperMouseUp}
{...mergeProps(
xdsClassName('number-input', {size, status: status?.type ?? null}),
stylex.props(
diff --git a/packages/core/src/TextArea/XDSTextArea.test.tsx b/packages/core/src/TextArea/XDSTextArea.test.tsx
index 99c5632a84..2e8f7db0a6 100644
--- a/packages/core/src/TextArea/XDSTextArea.test.tsx
+++ b/packages/core/src/TextArea/XDSTextArea.test.tsx
@@ -551,4 +551,34 @@ describe('XDSTextArea', () => {
expect(screen.getByRole('textbox')).not.toHaveAttribute('name');
});
});
+
+ describe('click-to-focus', () => {
+ it('focuses textarea when clicking the start icon', () => {
+ render(
+ <XDSTextArea
+ label="Notes"
+ value=""
+ onChange={() => {}}
+ startIcon={<MagnifyingGlassIcon />}
+ />,
+ );
+
+ const textarea = screen.getByRole('textbox');
+ const wrapper = textarea.parentElement!;
+ const iconElement = wrapper.querySelector('svg')!;
+
+ fireEvent.click(iconElement);
+ expect(textarea).toHaveFocus();
+ });
+
+ it('focuses textarea when clicking the wrapper padding', () => {
+ render(<XDSTextArea label="Notes" value="" onChange={() => {}} />);
+
+ const textarea = screen.getByRole('textbox');
+ const wrapper = textarea.parentElement!;
+
+ fireEvent.click(wrapper);
+ expect(textarea).toHaveFocus();
+ });
+ });
});
diff --git a/packages/core/src/TextArea/XDSTextArea.tsx b/packages/core/src/TextArea/XDSTextArea.tsx
index be1ec8e6a1..84903fc727 100644
--- a/packages/core/src/TextArea/XDSTextArea.tsx
+++ b/packages/core/src/TextArea/XDSTextArea.tsx
@@ -18,6 +18,8 @@ import {
useId,
useOptimistic,
useTransition,
+ useCallback,
+ useRef,
type ChangeEvent,
type ClipboardEvent,
type FocusEvent,
@@ -42,6 +44,7 @@ import {XDSIcon, renderIconSlot, type XDSIconType} from '../Icon';
import {XDSSpinner} from '../Spinner';
import {xdsClassName, mergeProps} from '../utils';
import type {XDSBaseProps} from '../XDSBaseProps';
+import {useInputContainer} from '../hooks/useInputContainer';
const COUNTER_WARNING_THRESHOLD = 0.8;
@@ -275,6 +278,8 @@ export function XDSTextArea({
const descriptionID = useId();
const statusMessageID = useId();
const counterID = useId();
+ const textareaRef = useRef<HTMLTextAreaElement | null>(null);
+ const containerRef = useRef<HTMLDivElement | null>(null);
const [, startTransition] = useTransition();
const [optimisticValue, setOptimisticValue] = useOptimistic(value);
@@ -317,6 +322,28 @@ export function XDSTextArea({
const effectivelyDisabled = isDisabled || isBusy;
+ // Combine refs
+ const setRefs = useCallback(
+ (el: HTMLTextAreaElement | null) => {
+ textareaRef.current = el;
+ if (typeof ref === 'function') {
+ ref(el);
+ } else if (ref) {
+ (ref as React.MutableRefObject<HTMLTextAreaElement | null>).current =
+ el;
+ }
+ },
+ [ref],
+ );
+
+ // Focus textarea when clicking anywhere on the wrapper (icons, padding, etc.)
+ const {onClick: handleWrapperClick, onMouseUp: handleWrapperMouseUp} =
+ useInputContainer({
+ containerRef,
+ inputRef: textareaRef,
+ disabled: effectivelyDisabled,
+ });
+
return (
<XDSField
label={label}
@@ -338,6 +365,9 @@ export function XDSTextArea({
}
labelTooltip={labelTooltip}>
<div
+ ref={containerRef}
+ onClick={handleWrapperClick}
+ onMouseUp={handleWrapperMouseUp}
{...mergeProps(
xdsClassName('textarea', {status: status?.type ?? null}),
stylex.props(
@@ -356,7 +386,7 @@ export function XDSTextArea({
renderIconSlot(startIcon, {size: 'sm', color: 'secondary'})}
<textarea
{...rest}
- ref={ref}
+ ref={setRefs}
id={id}
name={htmlName}
value={String(optimisticValue)}
diff --git a/packages/core/src/TextInput/XDSTextInput.test.tsx b/packages/core/src/TextInput/XDSTextInput.test.tsx
index 74aee76fa5..87fbb3bd6b 100644
--- a/packages/core/src/TextInput/XDSTextInput.test.tsx
+++ b/packages/core/src/TextInput/XDSTextInput.test.tsx
@@ -8,7 +8,7 @@
*/
import {describe, it, expect, vi} from 'vitest';
-import {render, screen} from '@testing-library/react';
+import {render, screen, fireEvent} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {MagnifyingGlassIcon} from '@heroicons/react/24/outline';
import {XDSTextInput} from './XDSTextInput';
@@ -466,4 +466,35 @@ describe('XDSTextInput', () => {
expect(onChange).toHaveBeenCalledWith('', null);
});
});
+
+ describe('click-to-focus', () => {
+ it('focuses input when clicking the start icon', () => {
+ render(
+ <XDSTextInput
+ label="Search"
+ value=""
+ onChange={() => {}}
+ startIcon={<MagnifyingGlassIcon />}
+ />,
+ );
+
+ const input = screen.getByRole('textbox');
+ const wrapper = input.parentElement!;
+ // The icon is rendered before the input; grab the first non-input child
+ const iconElement = wrapper.querySelector('svg')!;
+
+ fireEvent.click(iconElement);
+ expect(input).toHaveFocus();
+ });
+
+ it('focuses input when clicking the wrapper padding', () => {
+ render(<XDSTextInput label="Name" value="" onChange={() => {}} />);
+
+ const input = screen.getByRole('textbox');
+ const wrapper = input.parentElement!;
+
+ fireEvent.click(wrapper);
+ expect(input).toHaveFocus();
+ });
+ });
});
diff --git a/packages/core/src/TextInput/XDSTextInput.tsx b/packages/core/src/TextInput/XDSTextInput.tsx
index 374220a331..428047dd64 100644
--- a/packages/core/src/TextInput/XDSTextInput.tsx
+++ b/packages/core/src/TextInput/XDSTextInput.tsx
@@ -114,6 +114,7 @@ export type {
} from '../Field';
import {xdsClassName, mergeProps} from '../utils';
import {useXDSSize} from '../SizeContext/XDSSizeContext';
+import {useInputContainer} from '../hooks/useInputContainer';
import {XDSBaseProps} from '../XDSBaseProps';
export type XDSTextInputType = 'text' | 'password' | 'email';
@@ -267,6 +268,7 @@ export function XDSTextInput({
const descriptionID = useId();
const statusMessageID = useId();
const inputRef = useRef<HTMLInputElement | null>(null);
+ const containerRef = useRef<HTMLDivElement | null>(null);
const [, startTransition] = useTransition();
const [optimisticValue, setOptimisticValue] = useOptimistic(value);
@@ -325,6 +327,14 @@ export function XDSTextInput({
[ref],
);
+ // Focus input when clicking anywhere on the wrapper (icons, padding, etc.)
+ const {onClick: handleWrapperClick, onMouseUp: handleWrapperMouseUp} =
+ useInputContainer({
+ containerRef,
+ inputRef,
+ disabled: isDisabled,
+ });
+
return (
<XDSField
label={label}
@@ -346,6 +356,9 @@ export function XDSTextInput({
}
labelTooltip={labelTooltip}>
<div
+ ref={containerRef}
+ onClick={handleWrapperClick}
+ onMouseUp={handleWrapperMouseUp}
{...mergeProps(
xdsClassName('text-input', {size, status: status?.type ?? null}),
stylex.props(
diff --git a/packages/core/src/TimeInput/XDSTimeInput.test.tsx b/packages/core/src/TimeInput/XDSTimeInput.test.tsx
index 8c7e61b3fd..038e3e3cd6 100644
--- a/packages/core/src/TimeInput/XDSTimeInput.test.tsx
+++ b/packages/core/src/TimeInput/XDSTimeInput.test.tsx
@@ -8,7 +8,7 @@
*/
import {describe, it, expect, vi} from 'vitest';
-import {render, screen} from '@testing-library/react';
+import {render, screen, fireEvent} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {XDSTimeInput} from './XDSTimeInput';
import type {ISOTimeString} from '../utils';
@@ -184,4 +184,28 @@ describe('XDSTimeInput', () => {
// onChange should be called immediately when input is valid, not waiting for blur
expect(onChange).toHaveBeenCalledWith('15:45');
});
+
+ it('focuses input when clicking the clock icon', () => {
+ render(<XDSTimeInput label="Time" onChange={() => {}} />);
+
+ const input = screen.getByRole('textbox');
+ const wrapper = input.parentElement!;
+ // The icon container is the first child div (before the input)
+ const iconContainer = wrapper.querySelector(':scope > div') as HTMLElement;
+
+ fireEvent.click(iconContainer);
+ expect(input).toHaveFocus();
+ });
+
+ it('focuses input when clicking the wrapper padding', () => {
+ render(<XDSTimeInput label="Time" onChange={() => {}} />);
+
+ const input = screen.getByRole('textbox');
+ const wrapper = input.parentElement!;
+
+ // Simulate clicking padding area by dispatching click directly on wrapper
+ // with wrapper as both target and currentTarget
+ fireEvent.click(wrapper);
+ expect(input).toHaveFocus();
+ });
});
diff --git a/packages/core/src/TimeInput/XDSTimeInput.tsx b/packages/core/src/TimeInput/XDSTimeInput.tsx
index ba9bccb599..c46f770efd 100644
--- a/packages/core/src/TimeInput/XDSTimeInput.tsx
+++ b/packages/core/src/TimeInput/XDSTimeInput.tsx
@@ -59,6 +59,7 @@ import {
} from '../utils';
import type {XDSBaseProps} from '../XDSBaseProps';
import {useXDSSize} from '../SizeContext/XDSSizeContext';
+import {useInputContainer} from '../hooks/useInputContainer';
const styles = stylex.create({
icon: {
@@ -313,6 +314,7 @@ export function XDSTimeInput({
const descriptionID = useId();
const statusMessageID = useId();
const inputRef = useRef<HTMLInputElement | null>(null);
+ const containerRef = useRef<HTMLDivElement | null>(null);
const [, startTransition] = useTransition();
const [optimisticValue, setOptimisticValue] = useOptimistic(value);
@@ -498,6 +500,14 @@ export function XDSTimeInput({
[ref],
);
+ // Focus input when clicking anywhere on the wrapper (icons, padding, etc.)
+ const {onClick: handleWrapperClick, onMouseUp: handleWrapperMouseUp} =
+ useInputContainer({
+ containerRef,
+ inputRef,
+ disabled: isDisabled,
+ });
+
return (
<XDSField
label={label}
@@ -519,6 +529,9 @@ export function XDSTimeInput({
}
labelTooltip={labelTooltip}>
<div
+ ref={containerRef}
+ onClick={handleWrapperClick}
+ onMouseUp={handleWrapperMouseUp}
{...mergeProps(
xdsClassName('time-input', {size, status: status?.type ?? null}),
stylex.props(
diff --git a/packages/core/src/hooks/index.ts b/packages/core/src/hooks/index.ts
index e5b82a1f34..f7aea98700 100644
--- a/packages/core/src/hooks/index.ts
+++ b/packages/core/src/hooks/index.ts
@@ -44,3 +44,6 @@ export type {
UseClickableContainerOptions,
ClickableContainerResult,
} from './useClickableContainer';
+
+export {useInputContainer} from './useInputContainer';
+export type {UseInputContainerOptions} from './useInputContainer';
diff --git a/packages/core/src/hooks/useInputContainer.ts b/packages/core/src/hooks/useInputContainer.ts
new file mode 100644
index 0000000000..68d5c25e56
--- /dev/null
+++ b/packages/core/src/hooks/useInputContainer.ts
@@ -0,0 +1,102 @@
+'use client';
+
+/**
+ * @file useInputContainer.ts
+ * @input Container ref, input ref
+ * @output onClick and onMouseUp handlers for the input container wrapper
+ * @position Hook for input wrapper containers that delegate focus to the inner input/textarea
+ *
+ * Uses useClickableContainer to safely handle nested interactive elements
+ * (clear buttons, calendar toggles, etc.) while focusing the input when
+ * clicking non-interactive areas (icons, padding, status indicators).
+ */
+
+import {useCallback, type RefObject} from 'react';
+import {useClickableContainer} from './useClickableContainer';
+
+/**
+ * Input types that should receive `.focus()` when the container is clicked.
+ * Other input types (e.g. checkbox, radio, file) use `.click()` instead.
+ */
+const FOCUS_INPUT_TYPES = new Set([
+ 'text',
+ 'password',
+ 'email',
+ 'number',
+ 'search',
+ 'tel',
+ 'url',
+ 'date',
+ 'datetime-local',
+ 'month',
+ 'time',
+ 'week',
+]);
+
+export interface UseInputContainerOptions {
+ /** Ref to the outer container/wrapper element */
+ containerRef: RefObject<HTMLElement | null>;
+ /** Ref to the inner input or textarea element */
+ inputRef: RefObject<
+ HTMLInputElement | HTMLTextAreaElement | HTMLElement | null
+ >;
+ /** Whether the input is disabled */
+ disabled?: boolean;
+}
+
+/**
+ * Hook that makes an input container wrapper clickable, delegating focus
+ * to the inner input/textarea when the user clicks non-interactive areas
+ * (icons, padding, status indicators).
+ *
+ * Nested interactive elements (clear buttons, links) are handled safely
+ * via useClickableContainer — clicking them does NOT steal focus.
+ *
+ * @compositionHint Use inside input wrapper components (XDSTextInput,
+ * XDSNumberInput, XDSTimeInput, XDSTextArea, etc.).
+ *
+ * @example
+ * ```
+ * const containerRef = useRef<HTMLDivElement>(null);
+ * const inputRef = useRef<HTMLInputElement>(null);
+ * const { onClick, onMouseUp } = useInputContainer({
+ * containerRef,
+ * inputRef,
+ * });
+ * return (
+ * <div ref={containerRef} onClick={onClick} onMouseUp={onMouseUp}>
+ * <XDSIcon icon="search" />
+ * <input ref={inputRef} />
+ * </div>
+ * );
+ * ```
+ */
+export function useInputContainer({
+ containerRef,
+ inputRef,
+ disabled = false,
+}: UseInputContainerOptions) {
+ const onClick = useCallback(() => {
+ const input = inputRef.current;
+ if (input == null) return;
+ if (
+ input instanceof HTMLInputElement &&
+ FOCUS_INPUT_TYPES.has(input.type)
+ ) {
+ input.focus();
+ } else if (input instanceof HTMLTextAreaElement) {
+ input.focus();
+ } else if (input instanceof HTMLElement) {
+ input.click();
+ } else if ('focus' in input) {
+ (input as unknown as {focus: () => void}).focus();
+ }
+ }, [inputRef]);
+
+ return useClickableContainer({
+ containerRef,
+ interactiveRef: inputRef,
+ onClick,
+ disabled,
+ });
+}