Skip to content

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

TypeScript

XDSTextInput isDisabled (#49)

Andrey Goderf99f3ab99a8e
diff --git a/apps/storybook/stories/TextInput.stories.tsx b/apps/storybook/stories/TextInput.stories.tsx
index 77c9ed6586..0088b066df 100644
--- a/apps/storybook/stories/TextInput.stories.tsx
+++ b/apps/storybook/stories/TextInput.stories.tsx
@@ -1,6 +1,6 @@
-import { useState } from 'react';
-import type { Meta, StoryObj } from '@storybook/react';
-import { XDSTextInput } from '@xds/core/TextInput';
+import {useState} from 'react';
+import type {Meta, StoryObj} from '@storybook/react';
+import {XDSTextInput} from '@xds/core/TextInput';
const meta: Meta<typeof XDSTextInput> = {
title: 'Core/XDSTextInput',
@@ -13,7 +13,8 @@ const meta: Meta<typeof XDSTextInput> = {
},
isLabelHidden: {
control: 'boolean',
- description: 'Visually hide the label (still accessible to screen readers)',
+ description:
+ 'Visually hide the label (still accessible to screen readers)',
},
placeholder: {
control: 'text',
@@ -29,11 +30,17 @@ const meta: Meta<typeof XDSTextInput> = {
},
isOptional: {
control: 'boolean',
- description: 'Whether the field is optional (mutually exclusive with isRequired)',
+ description:
+ 'Whether the field is optional (mutually exclusive with isRequired)',
},
isRequired: {
control: 'boolean',
- description: 'Whether the field is required (mutually exclusive with isOptional)',
+ description:
+ 'Whether the field is required (mutually exclusive with isOptional)',
+ },
+ isDisabled: {
+ control: 'boolean',
+ description: 'Whether the input is disabled',
},
},
};
@@ -42,7 +49,7 @@ export default meta;
type Story = StoryObj<typeof XDSTextInput>;
export const Default: Story = {
- render: (args) => {
+ render: args => {
const [value, setValue] = useState(args.value ?? '');
return <XDSTextInput {...args} value={value} onChange={setValue} />;
},
@@ -53,7 +60,7 @@ export const Default: Story = {
};
export const WithDescription: Story = {
- render: (args) => {
+ render: args => {
const [value, setValue] = useState(args.value ?? '');
return <XDSTextInput {...args} value={value} onChange={setValue} />;
},
@@ -65,7 +72,7 @@ export const WithDescription: Story = {
};
export const WithHiddenLabel: Story = {
- render: (args) => {
+ render: args => {
const [value, setValue] = useState(args.value ?? '');
return <XDSTextInput {...args} value={value} onChange={setValue} />;
},
@@ -77,7 +84,7 @@ export const WithHiddenLabel: Story = {
};
export const WithValue: Story = {
- render: (args) => {
+ render: args => {
const [value, setValue] = useState(args.value ?? 'Hello, world!');
return <XDSTextInput {...args} value={value} onChange={setValue} />;
},
@@ -96,22 +103,71 @@ export const AllVariations: Story = {
const [value5, setValue5] = useState('');
const [value6, setValue6] = useState('');
const [value7, setValue7] = useState('');
+ const [value8, setValue8] = useState('Disabled input');
return (
- <div style={{ display: 'flex', flexDirection: 'column', gap: '16px', maxWidth: '300px' }}>
- <XDSTextInput label="Visible label" value={value1} onChange={setValue1} placeholder="Enter text..." />
- <XDSTextInput label="With description" description="Helpful description text" value={value4} onChange={setValue4} placeholder="Enter text..." />
- <XDSTextInput label="Hidden label" isLabelHidden value={value2} onChange={setValue2} placeholder="Hidden label input" />
+ <div
+ style={{
+ display: 'flex',
+ flexDirection: 'column',
+ gap: '16px',
+ maxWidth: '300px',
+ }}>
+ <XDSTextInput
+ label="Visible label"
+ value={value1}
+ onChange={setValue1}
+ placeholder="Enter text..."
+ />
+ <XDSTextInput
+ label="With description"
+ description="Helpful description text"
+ value={value4}
+ onChange={setValue4}
+ placeholder="Enter text..."
+ />
+ <XDSTextInput
+ label="Hidden label"
+ isLabelHidden
+ value={value2}
+ onChange={setValue2}
+ placeholder="Hidden label input"
+ />
<XDSTextInput label="With value" value={value3} onChange={setValue3} />
- <XDSTextInput label="Optional field" isOptional value={value5} onChange={setValue5} placeholder="Optional..." />
- <XDSTextInput label="Required field" isRequired value={value6} onChange={setValue6} placeholder="Required..." />
- <XDSTextInput label="Description with optional" description="Enter your nickname" isOptional value={value7} onChange={setValue7} placeholder="Nickname..." />
+ <XDSTextInput
+ label="Optional field"
+ isOptional
+ value={value5}
+ onChange={setValue5}
+ placeholder="Optional..."
+ />
+ <XDSTextInput
+ label="Required field"
+ isRequired
+ value={value6}
+ onChange={setValue6}
+ placeholder="Required..."
+ />
+ <XDSTextInput
+ label="Description with optional"
+ description="Enter your nickname"
+ isOptional
+ value={value7}
+ onChange={setValue7}
+ placeholder="Nickname..."
+ />
+ <XDSTextInput
+ label="Disabled field"
+ isDisabled
+ value={value8}
+ onChange={setValue8}
+ />
</div>
);
},
};
export const OptionalField: Story = {
- render: (args) => {
+ render: args => {
const [value, setValue] = useState(args.value ?? '');
return <XDSTextInput {...args} value={value} onChange={setValue} />;
},
@@ -123,7 +179,7 @@ export const OptionalField: Story = {
};
export const RequiredField: Story = {
- render: (args) => {
+ render: args => {
const [value, setValue] = useState(args.value ?? '');
return <XDSTextInput {...args} value={value} onChange={setValue} />;
},
@@ -135,7 +191,7 @@ export const RequiredField: Story = {
};
export const DescriptionWithOptional: Story = {
- render: (args) => {
+ render: args => {
const [value, setValue] = useState(args.value ?? '');
return <XDSTextInput {...args} value={value} onChange={setValue} />;
},
@@ -146,3 +202,15 @@ export const DescriptionWithOptional: Story = {
placeholder: 'Your bio here...',
},
};
+
+export const Disabled: Story = {
+ render: args => {
+ const [value, setValue] = useState(args.value ?? 'Cannot edit this');
+ return <XDSTextInput {...args} value={value} onChange={setValue} />;
+ },
+ args: {
+ label: 'Locked Field',
+ isDisabled: true,
+ value: 'Cannot edit this',
+ },
+};
diff --git a/packages/core/src/TextInput/XDSTextInput.test.tsx b/packages/core/src/TextInput/XDSTextInput.test.tsx
index 8b5a6ae203..bb9cd6e359 100644
--- a/packages/core/src/TextInput/XDSTextInput.test.tsx
+++ b/packages/core/src/TextInput/XDSTextInput.test.tsx
@@ -105,4 +105,28 @@ describe('XDSTextInput', () => {
render(<XDSTextInput label="Username" value="" onChange={() => {}} />);
expect(screen.getByRole('textbox')).not.toHaveAttribute('aria-required');
});
+
+ it('sets disabled attribute when isDisabled is true', () => {
+ render(
+ <XDSTextInput label="Name" isDisabled value="" onChange={() => {}} />,
+ );
+ expect(screen.getByRole('textbox')).toBeDisabled();
+ });
+
+ it('does not fire onChange when disabled', async () => {
+ const user = userEvent.setup();
+ const handleChange = vi.fn();
+ render(
+ <XDSTextInput label="Name" isDisabled value="" onChange={handleChange} />,
+ );
+
+ const input = screen.getByRole('textbox');
+ await user.type(input, 'test');
+ expect(handleChange).not.toHaveBeenCalled();
+ });
+
+ it('is not disabled by default', () => {
+ render(<XDSTextInput label="Name" value="" onChange={() => {}} />);
+ expect(screen.getByRole('textbox')).not.toBeDisabled();
+ });
});
diff --git a/packages/core/src/TextInput/XDSTextInput.tsx b/packages/core/src/TextInput/XDSTextInput.tsx
index 3fd195cb42..b314008241 100644
--- a/packages/core/src/TextInput/XDSTextInput.tsx
+++ b/packages/core/src/TextInput/XDSTextInput.tsx
@@ -54,6 +54,11 @@ const styles = stylex.create({
color: colorVars['--color-text-placeholder'],
},
},
+ disabled: {
+ cursor: 'not-allowed',
+ opacity: 0.5,
+ borderColor: color.dividerEmphasized,
+ },
});
export interface XDSTextInputProps {
@@ -80,6 +85,11 @@ export interface XDSTextInputProps {
* @default false
*/
isRequired?: boolean;
+ /**
+ * Whether the input is disabled.
+ * @default false
+ */
+ isDisabled?: boolean;
/**
* Callback fired when the input value changes.
*/
@@ -111,6 +121,7 @@ export const XDSTextInput = forwardRef<HTMLInputElement, XDSTextInputProps>(
description,
isOptional = false,
isRequired = false,
+ isDisabled = false,
onChange,
value,
placeholder,
@@ -136,9 +147,10 @@ export const XDSTextInput = forwardRef<HTMLInputElement, XDSTextInputProps>(
value={value}
onChange={e => onChange(e.target.value, e)}
placeholder={placeholder}
+ disabled={isDisabled}
aria-describedby={description ? descriptionID : undefined}
aria-required={isRequired === true ? 'true' : undefined}
- {...stylex.props(styles.input)}
+ {...stylex.props(styles.input, isDisabled && styles.disabled)}
/>
</XDSField>
);