Skip to content

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

TypeScript

fix(TextInput,TextArea): a disabled field stays in the tab order (#8018)

Ojas Mutrejad4f09c26d371
diff --git a/.changeset/text-fields-focusable-disabled.md b/.changeset/text-fields-focusable-disabled.md
new file mode 100644
index 0000000000..f7b93c9907
--- /dev/null
+++ b/.changeset/text-fields-focusable-disabled.md
@@ -0,0 +1,9 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] TextInput, TextArea: a disabled field now keeps its place in the tab order, so keyboard and screen-reader users can reach it and hear that it is unavailable (WCAG 2.2 SC 2.1.1 Keyboard, 4.1.2 Name, Role, Value and 2.4.7 Focus Visible; `spec:AST-085` FR1–FR3 and FR6). It exposes `aria-disabled="true"` and is read-only instead of carrying the native `disabled` attribute, as the field already did when a `disabledMessage` was set. It is still dimmed, cannot be edited, and is left out of form submission. Like a natively disabled field, it runs no caller event handler other than focus and blur, and Enter in a disabled TextInput does not submit its form. Under keyboard focus it shows the field's focus ring, dimmed with the rest of the field.
+
+Part of #7814.
+
+@HelloOjasMutreja
diff --git a/apps/storybook/stories/TextArea.stories.tsx b/apps/storybook/stories/TextArea.stories.tsx
index 22eb2f4628..8f9068ec62 100644
--- a/apps/storybook/stories/TextArea.stories.tsx
+++ b/apps/storybook/stories/TextArea.stories.tsx
@@ -59,7 +59,7 @@ const meta: Meta<typeof TextArea> = {
isReadOnly: {
control: 'boolean',
description:
- 'Whether the textarea is read-only. The value still submits with the form but cannot be edited. Unlike isDisabled it is not dimmed and stays in the tab order.',
+ 'Whether the textarea is read-only. The value still submits with the form but cannot be edited. Unlike isDisabled it is not dimmed and keeps its form value.',
},
disabledMessage: {
control: 'text',
@@ -276,7 +276,7 @@ export const DescriptionWithOptional: Story = {
};
// Read-only shows a value the user should see and send but not change. Unlike
-// a disabled field it is not dimmed, stays in the tab order, and still submits.
+// a disabled field it is not dimmed and still submits.
export const ReadOnly: Story = {
render: args => {
const [value, setValue] = useState(
diff --git a/apps/storybook/stories/TextInput.stories.tsx b/apps/storybook/stories/TextInput.stories.tsx
index bb972733ab..f487cba727 100644
--- a/apps/storybook/stories/TextInput.stories.tsx
+++ b/apps/storybook/stories/TextInput.stories.tsx
@@ -57,7 +57,7 @@ const meta: Meta<typeof TextInput> = {
isReadOnly: {
control: 'boolean',
description:
- 'Whether the input is read-only. The value still submits with the form but cannot be edited. Unlike isDisabled it is not dimmed and stays in the tab order.',
+ 'Whether the input is read-only. The value still submits with the form but cannot be edited. Unlike isDisabled it is not dimmed and keeps its form value.',
},
disabledMessage: {
control: 'text',
@@ -236,7 +236,7 @@ export const DescriptionWithOptional: Story = {
};
// Read-only shows a value the user should see and send but not change. Unlike
-// a disabled field it is not dimmed, stays in the tab order, and still submits.
+// a disabled field it is not dimmed and still submits.
export const ReadOnly: Story = {
render: args => {
const [value, setValue] = useState(args.value ?? 'ACCT-4417-9920');
diff --git a/packages/core/src/Field/inputDisabledFocus.ts b/packages/core/src/Field/inputDisabledFocus.ts
new file mode 100644
index 0000000000..0e20014007
--- /dev/null
+++ b/packages/core/src/Field/inputDisabledFocus.ts
@@ -0,0 +1,28 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file inputDisabledFocus.ts
+ * @input Uses StyleX and theme tokens
+ * @output inputDisabledFocusStyles (internal; not re-exported from Field/index.ts)
+ * @position Field wrapper style shared by TextInput and TextArea
+ */
+
+import * as stylex from '@stylexjs/stylex';
+import {colorVars} from '../theme/tokens.stylex';
+
+export const inputDisabledFocusStyles = stylex.create({
+ // The shared disabled style drops the focus ring; restore it while the field's
+ // own control, a direct child, has keyboard focus (`spec:AST-085` FR6), so a
+ // focused status button keeps only its own outline.
+ ring: {
+ borderColor: {
+ default: colorVars['--color-border-emphasized'],
+ ':has(> [aria-disabled="true"]:focus-visible)':
+ colorVars['--color-accent'],
+ },
+ boxShadow: {
+ default: 'none',
+ ':has(> [aria-disabled="true"]:focus-visible)': `inset 0px 0px 0px 2px ${colorVars['--color-accent-muted']}`,
+ },
+ },
+});
diff --git a/packages/core/src/TextArea/TextArea.doc.mjs b/packages/core/src/TextArea/TextArea.doc.mjs
index 4620094fda..e88b40a8cc 100644
--- a/packages/core/src/TextArea/TextArea.doc.mjs
+++ b/packages/core/src/TextArea/TextArea.doc.mjs
@@ -151,14 +151,14 @@ export const docs = {
name: 'isReadOnly',
type: 'boolean',
description:
- 'Makes the textarea read-only: the value is shown at full opacity and still submits with the form, but cannot be edited. Unlike isDisabled, a read-only textarea is not dimmed and stays in the tab order. isDisabled takes precedence when both are set.',
+ 'Makes the textarea read-only: the value is shown at full opacity and still submits with the form, but cannot be edited. Unlike isDisabled, a read-only textarea is not dimmed and keeps its form value. isDisabled takes precedence when both are set.',
default: 'false',
},
{
name: 'disabledMessage',
type: 'string',
description:
- 'Explains why the textarea is disabled. With isDisabled, shows a tooltip on hover/keyboard focus and keeps the textarea focusable via aria-disabled (the field becomes read-only). Use this instead of wrapping a disabled TextArea in Tooltip. Disabled controls swallow the hover events an external Tooltip needs.',
+ 'Explains why the textarea is disabled. With isDisabled, shows a tooltip on hover/keyboard focus and keeps the textarea focusable via aria-disabled (the field becomes read-only). Use this instead of wrapping a disabled TextArea in Tooltip, so the reason is attached to the field itself.',
},
{
name: 'isLoading',
@@ -364,7 +364,7 @@ export const docs = {
{
guidance: false,
description:
- "Don't wrap a disabled TextArea in Tooltip to explain why it's disabled; disabled controls swallow the hover events the wrapper needs. Use the disabledMessage prop instead.",
+ "Don't wrap a disabled TextArea in Tooltip to explain why it's disabled. Use the disabledMessage prop, which attaches the reason to the field itself.",
},
],
},
@@ -437,7 +437,7 @@ export const docsZh = {
name: 'isReadOnly',
type: 'boolean',
description:
- '将文本域设为只读:值以完整不透明度显示并仍随表单提交,但无法编辑。与 isDisabled 不同,只读文本域不会变暗,并保留在 Tab 顺序中。同时设置时 isDisabled 优先。',
+ '将文本域设为只读:值以完整不透明度显示并仍随表单提交,但无法编辑。与 isDisabled 不同,只读文本域不会变暗,并保留其表单值。同时设置时 isDisabled 优先。',
default: 'false',
},
{
@@ -637,7 +637,7 @@ export const docsZh = {
{
guidance: false,
description:
- "Don't wrap a disabled TextArea in Tooltip to explain why it's disabled; disabled controls swallow the hover events the wrapper needs. Use the disabledMessage prop instead.",
+ "Don't wrap a disabled TextArea in Tooltip to explain why it's disabled. Use the disabledMessage prop, which attaches the reason to the field itself.",
},
],
},
@@ -710,7 +710,7 @@ export const docsDense = {
'Shows "Required" indicator+sets aria-required. Mutually exclusive w/ isOptional.',
isDisabled: 'Disables textarea, prevents interaction.',
isReadOnly:
- 'Read-only: value visible + still submits, but not editable. Unlike isDisabled: not dimmed, stays in tab order.',
+ 'Read-only: value visible + still submits, but not editable. Unlike isDisabled: not dimmed.',
disabledMessage:
'Explains why textarea is disabled. With isDisabled, shows tooltip on hover/focus + keeps textarea focusable via aria-disabled (field becomes read-only). Use instead of wrapping a disabled TextArea in Tooltip.',
isLoading: 'Loading state w/ spinner inside input.',
diff --git a/packages/core/src/TextArea/TextArea.test.tsx b/packages/core/src/TextArea/TextArea.test.tsx
index ec05feab9b..59224419cf 100644
--- a/packages/core/src/TextArea/TextArea.test.tsx
+++ b/packages/core/src/TextArea/TextArea.test.tsx
@@ -13,10 +13,27 @@ import {useState} from 'react';
import {describe, it, expect, vi, beforeEach, afterEach} from 'vitest';
import {render, screen, fireEvent, waitFor} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
+import * as stylex from '@stylexjs/stylex';
+import {colorVars} from '../theme/tokens.stylex';
import {TestIcon} from '../__tests__/TestIcon';
import {TextArea} from './TextArea';
import {__resetLiveRegionsForTest} from '../hooks/useAnnounce';
+// jsdom cannot match `:focus-visible`: compare the ring's StyleX class hash.
+const RING = stylex
+ .props(
+ stylex.create({
+ ring: {
+ boxShadow: {
+ default: null,
+ ':has(> [aria-disabled="true"]:focus-visible)': `inset 0px 0px 0px 2px ${colorVars['--color-accent-muted']}`,
+ },
+ },
+ }).ring,
+ )
+ .className!.split(' ')
+ .pop()!;
+
// FieldStatus announces status messages through the persistent useAnnounce
// singletons; remove them between tests so role/aria-live queries in this
// file never match a leftover region.
@@ -166,11 +183,50 @@ describe('TextArea', () => {
expect(screen.getByRole('textbox')).toHaveAttribute('rows', '3');
});
- it('is disabled when isDisabled is true', () => {
+ it('exposes aria-disabled and read-only, not native disabled, when isDisabled is true', () => {
render(
<TextArea label="Description" isDisabled value="" onChange={() => {}} />,
);
- expect(screen.getByRole('textbox')).toBeDisabled();
+ const textarea = screen.getByRole('textbox');
+ expect(textarea).not.toBeDisabled();
+ expect(textarea).toHaveAttribute('aria-disabled', 'true');
+ expect(textarea).toHaveAttribute('readonly');
+ });
+
+ it('keeps a disabled textarea in the tab order (spec:AST-085)', async () => {
+ const user = userEvent.setup();
+ render(
+ <>
+ <TextArea label="First" value="" onChange={() => {}} />
+ <TextArea label="Second" isDisabled value="" onChange={() => {}} />
+ <TextArea label="Third" value="" onChange={() => {}} />
+ </>,
+ );
+ await user.tab();
+ await user.tab();
+ expect(screen.getByRole('textbox', {name: 'Second'})).toHaveFocus();
+ });
+
+ it('runs no caller handler from a disabled textarea and rings it under focus (spec:AST-085 FR2, FR6)', async () => {
+ const user = userEvent.setup();
+ const handlers = {onPaste: vi.fn(), onKeyDown: vi.fn()};
+ render(
+ <TextArea
+ label="Notes"
+ isDisabled
+ value=""
+ onChange={() => {}}
+ {...handlers}
+ />,
+ );
+ const textarea = screen.getByRole('textbox');
+ await user.click(textarea);
+ await user.keyboard('a');
+ await user.paste('x');
+ for (const handler of Object.values(handlers)) {
+ expect(handler).not.toHaveBeenCalled();
+ }
+ expect(textarea.parentElement).toHaveClass(RING);
});
it('is not disabled by default', () => {
@@ -872,7 +928,10 @@ describe('TextArea', () => {
/>
</form>,
);
- expect(screen.getByRole('textbox')).toBeDisabled();
+ expect(screen.getByRole('textbox')).toHaveAttribute(
+ 'aria-disabled',
+ 'true',
+ );
expect([
...new FormData(container.querySelector('form')!).keys(),
]).toEqual([]);
@@ -1049,13 +1108,13 @@ describe('TextArea', () => {
expect(textarea).toHaveValue('');
});
- it('remains natively disabled when disabled without a reason', () => {
+ it('stays focusable when disabled without a reason', () => {
render(
<TextArea label="Notes" value="" onChange={() => {}} isDisabled />,
);
const textarea = screen.getByRole('textbox');
- expect(textarea).toBeDisabled();
- expect(textarea).not.toHaveAttribute('aria-disabled');
+ expect(textarea).not.toBeDisabled();
+ expect(textarea).toHaveAttribute('aria-disabled', 'true');
});
});
});
diff --git a/packages/core/src/TextArea/TextArea.tsx b/packages/core/src/TextArea/TextArea.tsx
index bcb02cb8fe..fa6dfda8cc 100644
--- a/packages/core/src/TextArea/TextArea.tsx
+++ b/packages/core/src/TextArea/TextArea.tsx
@@ -52,6 +52,7 @@ import {
type FieldInputBaseProps,
useFieldInputStyling,
} from '../Field/fieldParts';
+import {inputDisabledFocusStyles} from '../Field/inputDisabledFocus';
import type {SizeValue} from '../utils/types';
import {useInputContainer} from '../hooks/useInputContainer';
import {useInputStatusIcon} from '../hooks/useInputStatusIcon';
@@ -253,7 +254,9 @@ export interface TextAreaProps extends Omit<
*/
rows?: number;
/**
- * Whether the textarea is disabled.
+ * Whether the textarea is disabled. A disabled textarea is dimmed, cannot be
+ * edited, and is left out of form submission, but it stays in the tab order
+ * (`aria-disabled`, not the native attribute).
* @default false
*/
isDisabled?: boolean;
@@ -261,7 +264,7 @@ export interface TextAreaProps extends Omit<
* Whether the textarea is read-only.
* The value is shown at full opacity and still submits with the form, but
* cannot be edited. Unlike `isDisabled`, a read-only textarea is not dimmed
- * and stays in the tab order — use it for a value the user should see and
+ * and keeps its form value — use it for a value the user should see and
* send but not change. `isDisabled` takes precedence when both are set.
* @default false
*/
@@ -273,8 +276,8 @@ export interface TextAreaProps extends Omit<
* discoverable by keyboard and assistive technology. The field cannot be
* edited (it becomes read-only) while disabled.
*
- * Use this instead of wrapping a disabled textarea in `Tooltip` — disabled
- * controls don't emit the pointer events an external tooltip needs.
+ * Use this instead of wrapping a disabled textarea in `Tooltip`, so the
+ * reason is attached to the field itself.
*
* @example
* ```
@@ -443,12 +446,20 @@ export function TextArea({
// (spec:AST-086 FR8).
useBusyAnnouncement(isBusy, t('@astryx.spinner.loading'), containerRef);
- // Disabled-reason tooltip. Disabled controls swallow pointer events, so the
- // tooltip listeners attach to the textarea container (which already exists)
+ // Disabled-reason tooltip. The tooltip listeners attach to the textarea container (which already exists)
// and the textarea stays perceivable via aria-disabled instead of the native
// disabled attribute. The field is made read-only so it can't be typed into,
// and value mutation is blocked by the isDisabled guard in handleChange.
const showsDisabledMessage = isDisabled && !!disabledMessage;
+ // A disabled field keeps its tab stop but, like a natively disabled one,
+ // runs no caller event handler other than focus and blur (`spec:AST-085` FR2).
+ const fieldProps = isDisabled
+ ? Object.fromEntries(
+ Object.entries(rest).filter(
+ ([key]) => !/^on[A-Z]/.test(key) || /^on(Focus|Blur)/.test(key),
+ ),
+ )
+ : rest;
const disabledMessageTooltip = useTooltip({
placement: 'above',
// The container div is not naturally focusable; focusin bubbles up from
@@ -598,6 +609,7 @@ export function TextArea({
inputWrapperStyles.base,
styles.wrapper,
isDisabled && inputWrapperStyles.disabled,
+ isDisabled && inputDisabledFocusStyles.ring,
status && inputStatusBorderStyles[status.type],
status && !isDisabled && inputStatusHoverShadowStyles[status.type],
status && inputStatusFocusWithinStyles[status.type],
@@ -612,23 +624,23 @@ export function TextArea({
</span>
)}
<textarea
- {...rest}
+ {...fieldProps}
ref={useMergedRefs(ref, textareaRef)}
id={id}
name={isDisabled ? undefined : htmlName}
value={optimisticValue}
onChange={handleChange}
- onPaste={onPaste}
+ onPaste={isDisabled ? undefined : onPaste}
onFocus={onFocus}
onBlur={onBlur}
placeholder={placeholder}
rows={rows}
- // With a disabledMessage the textarea keeps focusability via
- // aria-disabled so the reason is focus-discoverable; readOnly + the
- // handleChange guard keep the value from changing.
- disabled={isDisabled && !showsDisabledMessage}
- aria-disabled={showsDisabledMessage ? 'true' : undefined}
- readOnly={isReadOnly || showsDisabledMessage || undefined}
+ // A disabled textarea keeps its place in the tab order
+ // (spec:AST-085): aria-disabled instead of the native attribute,
+ // with readOnly + the handleChange guard keeping the value from
+ // changing.
+ aria-disabled={isDisabled ? 'true' : undefined}
+ readOnly={isReadOnly || isDisabled || undefined}
spellCheck={hasSpellCheck}
autoFocus={hasAutoFocus}
data-autofocus={hasAutoFocus || undefined}
diff --git a/packages/core/src/TextInput/TextInput.doc.mjs b/packages/core/src/TextInput/TextInput.doc.mjs
index 9a83ea0ced..4ed255df89 100644
--- a/packages/core/src/TextInput/TextInput.doc.mjs
+++ b/packages/core/src/TextInput/TextInput.doc.mjs
@@ -93,14 +93,14 @@ export const docs = {
name: 'isReadOnly',
type: 'boolean',
description:
- 'Makes the input read-only: the value is shown at full opacity and still submits with the form, but cannot be edited. Unlike isDisabled, a read-only input is not dimmed and stays in the tab order. isDisabled takes precedence when both are set.',
+ 'Makes the input read-only: the value is shown at full opacity and still submits with the form, but cannot be edited. Unlike isDisabled, a read-only input is not dimmed and keeps its form value. isDisabled takes precedence when both are set.',
default: 'false',
},
{
name: 'disabledMessage',
type: 'string',
description:
- 'Explains why the input is disabled. With isDisabled, shows a tooltip on hover/keyboard focus and keeps the input focusable via aria-disabled (the field becomes read-only). Use this instead of wrapping a disabled TextInput in Tooltip. Disabled controls swallow the hover events an external Tooltip needs.',
+ 'Explains why the input is disabled. With isDisabled, shows a tooltip on hover/keyboard focus and keeps the input focusable via aria-disabled (the field becomes read-only). Use this instead of wrapping a disabled TextInput in Tooltip, so the reason is attached to the field itself.',
},
{
name: 'isLoading',
@@ -219,7 +219,7 @@ export const docs = {
{guidance: false, description: "Don't use placeholder text as a replacement for a label; placeholders disappear on focus and are not reliably read by screen readers."},
{guidance: false, description: "Don't use TextInput for multi-line content like comments or descriptions; use TextArea instead."},
{guidance: false, description: "Don't mark every field as required; only flag mandatory fields so users are not overwhelmed by validation errors."},
- {guidance: false, description: "Don't wrap a disabled TextInput in Tooltip to explain why it's disabled; disabled controls swallow the hover events the wrapper needs. Use the disabledMessage prop instead."},
+ {guidance: false, description: "Don't wrap a disabled TextInput in Tooltip to explain why it's disabled. Use the disabledMessage prop, which attaches the reason to the field itself."},
],
anatomy: [
{name: 'Label', required: true, description: 'Text that identifies the field. Always rendered for accessibility even when visually hidden.'},
@@ -311,14 +311,14 @@ export const docsZh = {
name: 'isReadOnly',
type: 'boolean',
description:
- '将输入框设为只读:值以完整不透明度显示并仍随表单提交,但无法编辑。与 isDisabled 不同,只读输入框不会变暗,并保留在 Tab 顺序中。同时设置时 isDisabled 优先。',
+ '将输入框设为只读:值以完整不透明度显示并仍随表单提交,但无法编辑。与 isDisabled 不同,只读输入框不会变暗,并保留其表单值。同时设置时 isDisabled 优先。',
default: 'false',
},
{
name: 'disabledMessage',
type: 'string',
description:
- '说明输入框被禁用的原因。与 isDisabled 一起使用时,在悬停/键盘聚焦时显示工具提示,并通过 aria-disabled 保持输入框可聚焦(字段变为只读)。请使用此属性,而不是用 Tooltip 包裹已禁用的 TextInput——已禁用的控件会吞掉外部 Tooltip 所需的悬停事件。',
+ '说明输入框被禁用的原因。与 isDisabled 一起使用时,在悬停/键盘聚焦时显示工具提示,并通过 aria-disabled 保持输入框可聚焦(字段变为只读)。请使用此属性,而不是用 Tooltip 包裹已禁用的 TextInput,以便把原因附加在字段本身上。',
},
{
name: 'isLoading',
@@ -430,7 +430,7 @@ export const docsZh = {
{guidance: false, description: "Don't use placeholder text as a replacement for a label; placeholders disappear on focus and are not reliably read by screen readers."},
{guidance: false, description: "Don't use TextInput for multi-line content like comments or descriptions; use TextArea instead."},
{guidance: false, description: "Don't mark every field as required; only flag mandatory fields so users are not overwhelmed by validation errors."},
- {guidance: false, description: "Don't wrap a disabled TextInput in Tooltip to explain why it's disabled; disabled controls swallow the hover events the wrapper needs. Use the disabledMessage prop instead."},
+ {guidance: false, description: "Don't wrap a disabled TextInput in Tooltip to explain why it's disabled. Use the disabledMessage prop, which attaches the reason to the field itself."},
],
anatomy: [
{name: 'Label', required: true, description: 'Text that identifies the field. Always rendered for accessibility even when visually hidden.'},
@@ -458,7 +458,7 @@ export const docsDense = {
{guidance: false, description: "Don't use placeholder text as a replacement for a label; placeholders disappear on focus and are not reliably read by screen readers."},
{guidance: false, description: "Don't use TextInput for multi-line content like comments or descriptions; use TextArea instead."},
{guidance: false, description: "Don't mark every field as required; only flag mandatory fields so users are not overwhelmed by validation errors."},
- {guidance: false, description: "Don't wrap a disabled TextInput in Tooltip to explain why it's disabled; disabled controls swallow the hover events the wrapper needs. Use the disabledMessage prop instead."},
+ {guidance: false, description: "Don't wrap a disabled TextInput in Tooltip to explain why it's disabled. Use the disabledMessage prop, which attaches the reason to the field itself."},
],
anatomy: [
{name: 'Label', required: true, description: 'Text that identifies the field. Always rendered for accessibility even when visually hidden.'},
@@ -489,7 +489,7 @@ export const docsDense = {
isRequired: 'Shows "Required" indicator+sets aria-required. Mutually exclusive w/ isOptional.',
isDisabled: 'Disables input, prevents interaction, dims element.',
isReadOnly:
- 'Read-only: value visible + still submits, but not editable. Unlike isDisabled: not dimmed, stays in tab order.',
+ 'Read-only: value visible + still submits, but not editable. Unlike isDisabled: not dimmed.',
disabledMessage:
'Explains why input is disabled. With isDisabled, shows tooltip on hover/focus + keeps input focusable via aria-disabled (field becomes read-only). Use instead of wrapping a disabled TextInput in Tooltip.',
isLoading: 'Loading state w/ spinner+aria-busy.',
diff --git a/packages/core/src/TextInput/TextInput.test.tsx b/packages/core/src/TextInput/TextInput.test.tsx
index df16b96608..69acce3ec2 100644
--- a/packages/core/src/TextInput/TextInput.test.tsx
+++ b/packages/core/src/TextInput/TextInput.test.tsx
@@ -13,11 +13,28 @@
import {describe, it, expect, vi, beforeEach} from 'vitest';
import {render, screen, fireEvent, waitFor} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
+import * as stylex from '@stylexjs/stylex';
+import {colorVars} from '../theme/tokens.stylex';
import {TestIcon} from '../__tests__/TestIcon';
import {declaredValue} from '../__tests__/stylexDeclarations';
import {InputGroup} from '../InputGroup';
import {TextInput} from './TextInput';
+// jsdom cannot match `:focus-visible`: compare the ring's StyleX class hash.
+const RING = stylex
+ .props(
+ stylex.create({
+ ring: {
+ boxShadow: {
+ default: null,
+ ':has(> [aria-disabled="true"]:focus-visible)': `inset 0px 0px 0px 2px ${colorVars['--color-accent-muted']}`,
+ },
+ },
+ }).ring,
+ )
+ .className!.split(' ')
+ .pop()!;
+
// Mock showPopover/hidePopover since jsdom does not implement them. Used by the
// disabledMessage tooltip.
beforeEach(() => {
@@ -138,9 +155,55 @@ describe('TextInput', () => {
expect(screen.getByRole('textbox')).not.toHaveAttribute('aria-required');
});
- it('sets disabled attribute when isDisabled is true', () => {
+ it('exposes aria-disabled and read-only, not native disabled, when isDisabled is true', () => {
+ render(<TextInput label="Name" isDisabled value="" onChange={() => {}} />);
+ const input = screen.getByRole('textbox');
+ expect(input).not.toBeDisabled();
+ expect(input).toHaveAttribute('aria-disabled', 'true');
+ expect(input).toHaveAttribute('readonly');
+ });
+
+ it('keeps a disabled input in the tab order (spec:AST-085)', async () => {
+ const user = userEvent.setup();
+ render(
+ <>
+ <TextInput label="First" value="" onChange={() => {}} />
+ <TextInput label="Second" isDisabled value="" onChange={() => {}} />
+ <TextInput label="Third" value="" onChange={() => {}} />
+ </>,
+ );
+ await user.tab();
+ await user.tab();
+ expect(screen.getByRole('textbox', {name: 'Second'})).toHaveFocus();
+ });
+
+ it('runs no caller handler and submits no form from a disabled input (spec:AST-085 FR2)', async () => {
+ const user = userEvent.setup();
+ const handlers = {onEnter: vi.fn(), onKeyDown: vi.fn(), onPaste: vi.fn()};
+ const onSubmit = vi.fn((e: React.FormEvent) => e.preventDefault());
+ render(
+ <form onSubmit={onSubmit}>
+ <TextInput
+ label="Name"
+ isDisabled
+ value=""
+ onChange={() => {}}
+ {...handlers}
+ />
+ </form>,
+ );
+ await user.click(screen.getByRole('textbox'));
+ await user.keyboard('{Enter}');
+ await user.paste('x');
+ for (const handler of [...Object.values(handlers), onSubmit]) {
+ expect(handler).not.toHaveBeenCalled();
+ }
+ });
+
+ it('rings a disabled input under keyboard focus (spec:AST-085 FR6)', () => {
render(<TextInput label="Name" isDisabled value="" onChange={() => {}} />);
- expect(screen.getByRole('textbox')).toBeDisabled();
+ const wrapper = screen.getByRole('textbox').parentElement!;
+ expect(wrapper).toHaveClass(RING);
});
it('does not fire onChange when disabled', async () => {
@@ -515,7 +578,10 @@ describe('TextInput', () => {
/>
</form>,
);
- expect(screen.getByRole('textbox')).toBeDisabled();
+ expect(screen.getByRole('textbox')).toHaveAttribute(
+ 'aria-disabled',
+ 'true',
+ );
expect([
...new FormData(container.querySelector('form')!).keys(),
]).toEqual([]);
@@ -888,13 +954,13 @@ describe('TextInput', () => {
expect(input).toHaveValue('');
});
- it('remains natively disabled when disabled without a reason', () => {
+ it('stays focusable when disabled without a reason', () => {
render(
<TextInput label="Owner" value="" onChange={() => {}} isDisabled />,
);
const input = screen.getByRole('textbox');
- expect(input).toBeDisabled();
- expect(input).not.toHaveAttribute('aria-disabled');
+ expect(input).not.toBeDisabled();
+ expect(input).toHaveAttribute('aria-disabled', 'true');
});
});
});
diff --git a/packages/core/src/TextInput/TextInput.tsx b/packages/core/src/TextInput/TextInput.tsx
index d503a843bf..75f858daba 100644
--- a/packages/core/src/TextInput/TextInput.tsx
+++ b/packages/core/src/TextInput/TextInput.tsx
@@ -113,6 +113,7 @@ import {
type FieldInputBaseProps,
useFieldInputStyling,
} from '../Field/fieldParts';
+import {inputDisabledFocusStyles} from '../Field/inputDisabledFocus';
import type {SizeValue} from '../utils/types';
import {themeProps} from '../utils/themeProps';
import {useTranslator} from '../i18n';
@@ -156,7 +157,9 @@ export interface TextInputProps extends Omit<
*/
isRequired?: boolean;
/**
- * Whether the input is disabled.
+ * Whether the input is disabled. A disabled input is dimmed, cannot be
+ * edited, and is left out of form submission, but it stays in the tab order
+ * (`aria-disabled`, not the native attribute).
* @default false
*/
isDisabled?: boolean;
@@ -164,7 +167,7 @@ export interface TextInputProps extends Omit<
* Whether the input is read-only.
* The value is shown at full opacity and still submits with the form, but
* cannot be edited. Unlike `isDisabled`, a read-only input is not dimmed and
- * stays in the tab order — use it for a value the user should see and send
+ * keeps its form value — use it for a value the user should see and send
* but not change. `isDisabled` takes precedence when both are set.
* @default false
*/
@@ -176,8 +179,8 @@ export interface TextInputProps extends Omit<
* keyboard and assistive technology. The field cannot be edited (it becomes
* read-only) while disabled.
*
- * Use this instead of wrapping a disabled input in `Tooltip` — disabled
- * controls don't emit the pointer events an external tooltip needs.
+ * Use this instead of wrapping a disabled input in `Tooltip`, so the reason
+ * is attached to the field itself.
*
* @example
* ```
@@ -331,6 +334,15 @@ export function TextInput({
});
const t = useTranslator();
const isEffectivelyRequired = useResolvedRequired({isRequired, isOptional});
+ // A disabled field keeps its tab stop but, like a natively disabled one,
+ // runs no caller event handler other than focus and blur (`spec:AST-085` FR2).
+ const fieldProps = isDisabled
+ ? Object.fromEntries(
+ Object.entries(rest).filter(
+ ([key]) => !/^on[A-Z]/.test(key) || /^on(Focus|Blur)/.test(key),
+ ),
+ )
+ : rest;
const size = useSize(sizeProp, 'md');
const generatedID = useId();
@@ -349,8 +361,7 @@ export function TextInput({
// (spec:AST-086 FR8).
useBusyAnnouncement(isBusy, t('@astryx.spinner.loading'), containerRef);
- // Disabled-reason tooltip. Disabled controls swallow pointer events, so the
- // tooltip listeners attach to the input container (which already exists) and
+ // Disabled-reason tooltip. The tooltip listeners attach to the input container (which already exists) and
// the input stays perceivable via aria-disabled instead of the native
// disabled attribute. The field is made read-only so it can't be typed into,
// and value mutation is blocked by the isDisabled guard in handleChange.
@@ -451,6 +462,7 @@ export function TextInput({
inputWrapperStyles.base,
sizeStyles[size],
isDisabled && inputWrapperStyles.disabled,
+ isDisabled && inputDisabledFocusStyles.ring,
status && inputStatusBorderStyles[status.type],
status && !isDisabled && inputStatusHoverShadowStyles[status.type],
status && inputStatusFocusWithinStyles[status.type],
@@ -463,7 +475,7 @@ export function TextInput({
{startIcon && renderIconSlot(startIcon, {size: 'sm', color: 'secondary'})}
{inputGroup && <VisuallyHidden id={inputLabelID}>{label}</VisuallyHidden>}
<input
- {...rest}
+ {...fieldProps}
ref={useMergedRefs(ref, inputRef)}
id={id}
name={isDisabled ? undefined : htmlName}
@@ -471,8 +483,15 @@ export function TextInput({
value={optimisticValue}
onChange={handleChange}
onKeyDown={
- onEnter || onKeyDown
+ isDisabled || onEnter || onKeyDown
? e => {
+ if (isDisabled) {
+ // A read-only field still submits its form on Enter.
+ if (e.key === 'Enter') {
+ e.preventDefault();
+ }
+ return;
+ }
// The composing keydown fires before compositionend, so without
// this guard pressing Enter to commit a Japanese/Chinese/Korean
// IME conversion would trigger onEnter (submit/save actions)
@@ -486,12 +505,11 @@ export function TextInput({
: undefined
}
placeholder={placeholder}
- // With a disabledMessage the input keeps focusability via aria-disabled
- // so the reason is focus-discoverable; readOnly + the handleChange guard
- // keep the value from changing.
- disabled={isDisabled && !showsDisabledMessage}
- aria-disabled={showsDisabledMessage ? 'true' : undefined}
- readOnly={isReadOnly || showsDisabledMessage || undefined}
+ // A disabled input keeps its place in the tab order (spec:AST-085):
+ // aria-disabled instead of the native attribute, with readOnly + the
+ // handleChange guard keeping the value from changing.
+ aria-disabled={isDisabled ? 'true' : undefined}
+ readOnly={isReadOnly || isDisabled || undefined}
autoFocus={hasAutoFocus}
data-autofocus={hasAutoFocus || undefined}
aria-describedby={