Skip to content

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

TypeScript

Add isOptional / isRequired to XDSField (#43)

Andrey Godere8edd7e94f99
diff --git a/apps/storybook/stories/Field.stories.tsx b/apps/storybook/stories/Field.stories.tsx
index 788b966f43..34bf67cfe6 100644
--- a/apps/storybook/stories/Field.stories.tsx
+++ b/apps/storybook/stories/Field.stories.tsx
@@ -27,6 +27,14 @@ const meta: Meta<typeof XDSField> = {
control: 'text',
description: 'ID for the description element (use for aria-describedby)',
},
+ isOptional: {
+ control: 'boolean',
+ description: 'Whether the field is optional (mutually exclusive with isRequired)',
+ },
+ isRequired: {
+ control: 'boolean',
+ description: 'Whether the field is required (mutually exclusive with isOptional)',
+ },
},
};
@@ -98,6 +106,9 @@ export const AllVariations: Story = {
render: () => {
const [value1, setValue1] = useState('');
const [value2, setValue2] = useState('');
+ const [value3, setValue3] = useState('');
+ const [value4, setValue4] = useState('');
+ const [value5, setValue5] = useState('');
return (
<div style={{ display: 'flex', flexDirection: 'column', gap: '24px', maxWidth: '300px' }}>
<XDSField label="Default field" inputID="default-input">
@@ -117,7 +128,97 @@ export const AllVariations: Story = {
style={{ padding: '8px', fontSize: '14px', width: '100%', boxSizing: 'border-box' }}
/>
</XDSField>
+ <XDSField label="Optional field" isOptional inputID="optional-input">
+ <input
+ id="optional-input"
+ value={value3}
+ onChange={(e) => setValue3(e.target.value)}
+ style={{ padding: '8px', fontSize: '14px', width: '100%', boxSizing: 'border-box' }}
+ />
+ </XDSField>
+ <XDSField label="Required field" isRequired inputID="required-input">
+ <input
+ id="required-input"
+ value={value4}
+ onChange={(e) => setValue4(e.target.value)}
+ style={{ padding: '8px', fontSize: '14px', width: '100%', boxSizing: 'border-box' }}
+ />
+ </XDSField>
+ <XDSField label="With description and optional" description="Enter your nickname" isOptional inputID="desc-optional-input" descriptionID="desc-optional-text">
+ <input
+ id="desc-optional-input"
+ aria-describedby="desc-optional-text"
+ value={value5}
+ onChange={(e) => setValue5(e.target.value)}
+ style={{ padding: '8px', fontSize: '14px', width: '100%', boxSizing: 'border-box' }}
+ />
+ </XDSField>
</div>
);
},
};
+
+export const OptionalField: Story = {
+ render: (args) => {
+ const [value, setValue] = useState('');
+ return (
+ <XDSField {...args} inputID="nickname-input">
+ <input
+ id="nickname-input"
+ value={value}
+ onChange={(e) => setValue(e.target.value)}
+ placeholder="Enter your nickname"
+ style={{ padding: '8px', fontSize: '14px', width: '100%', boxSizing: 'border-box' }}
+ />
+ </XDSField>
+ );
+ },
+ args: {
+ label: 'Nickname',
+ isOptional: true,
+ },
+};
+
+export const RequiredField: Story = {
+ render: (args) => {
+ const [value, setValue] = useState('');
+ return (
+ <XDSField {...args} inputID="username-input">
+ <input
+ id="username-input"
+ value={value}
+ onChange={(e) => setValue(e.target.value)}
+ placeholder="Enter your username"
+ style={{ padding: '8px', fontSize: '14px', width: '100%', boxSizing: 'border-box' }}
+ />
+ </XDSField>
+ );
+ },
+ args: {
+ label: 'Username',
+ isRequired: true,
+ },
+};
+
+export const DescriptionWithOptional: Story = {
+ render: (args) => {
+ const [value, setValue] = useState('');
+ return (
+ <XDSField {...args} inputID="bio-input" descriptionID="bio-desc">
+ <input
+ id="bio-input"
+ aria-describedby="bio-desc"
+ value={value}
+ onChange={(e) => setValue(e.target.value)}
+ placeholder="Tell us about yourself"
+ style={{ padding: '8px', fontSize: '14px', width: '100%', boxSizing: 'border-box' }}
+ />
+ </XDSField>
+ );
+ },
+ args: {
+ label: 'Bio',
+ description: 'A short description about yourself',
+ isOptional: true,
+ },
+};
diff --git a/apps/storybook/stories/TextInput.stories.tsx b/apps/storybook/stories/TextInput.stories.tsx
index f400754d69..77c9ed6586 100644
--- a/apps/storybook/stories/TextInput.stories.tsx
+++ b/apps/storybook/stories/TextInput.stories.tsx
@@ -27,6 +27,14 @@ const meta: Meta<typeof XDSTextInput> = {
control: 'text',
description: 'Current input value (required)',
},
+ isOptional: {
+ control: 'boolean',
+ description: 'Whether the field is optional (mutually exclusive with isRequired)',
+ },
+ isRequired: {
+ control: 'boolean',
+ description: 'Whether the field is required (mutually exclusive with isOptional)',
+ },
},
};
@@ -85,13 +93,56 @@ export const AllVariations: Story = {
const [value2, setValue2] = useState('');
const [value3, setValue3] = useState('Pre-filled value');
const [value4, setValue4] = useState('');
+ const [value5, setValue5] = useState('');
+ const [value6, setValue6] = useState('');
+ const [value7, setValue7] = useState('');
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" />
<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..." />
</div>
);
},
};
+
+export const OptionalField: Story = {
+ render: (args) => {
+ const [value, setValue] = useState(args.value ?? '');
+ return <XDSTextInput {...args} value={value} onChange={setValue} />;
+ },
+ args: {
+ label: 'Nickname',
+ isOptional: true,
+ placeholder: 'Enter your nickname',
+ },
+};
+
+export const RequiredField: Story = {
+ render: (args) => {
+ const [value, setValue] = useState(args.value ?? '');
+ return <XDSTextInput {...args} value={value} onChange={setValue} />;
+ },
+ args: {
+ label: 'Username',
+ isRequired: true,
+ placeholder: 'Enter your username',
+ },
+};
+
+export const DescriptionWithOptional: Story = {
+ render: (args) => {
+ const [value, setValue] = useState(args.value ?? '');
+ return <XDSTextInput {...args} value={value} onChange={setValue} />;
+ },
+ args: {
+ label: 'Bio',
+ description: 'Tell us about yourself',
+ isOptional: true,
+ placeholder: 'Your bio here...',
+ },
+};
diff --git a/packages/core/src/Field/README.md b/packages/core/src/Field/README.md
index d4f015e3a2..32d5d550ed 100644
--- a/packages/core/src/Field/README.md
+++ b/packages/core/src/Field/README.md
@@ -8,6 +8,7 @@ A form field wrapper component that provides label and description.
- **Label Support**: Required label for accessibility (can be visually hidden)
- **Description**: Optional description text displayed between the label and input
+- **Optional/Required Indicators**: Display "Optional" or "Required" text with bullet separator
- **Accessible**: Label properly associated with input via htmlFor/id
- **Styled with StyleX**: Uses XDS design tokens for consistent styling
@@ -40,6 +41,31 @@ const searchId = useId();
<XDSField label="Search" isLabelHidden inputID={searchId}>
<input id={searchId} placeholder="Search..." />
</XDSField>
+
+// Optional field
+const nicknameId = useId();
+<XDSField label="Nickname" isOptional inputID={nicknameId}>
+ <input id={nicknameId} placeholder="Enter your nickname" />
+</XDSField>
+
+// Required field
+const usernameId = useId();
+<XDSField label="Username" isRequired inputID={usernameId}>
+ <input id={usernameId} placeholder="Enter your username" />
+</XDSField>
+
+// Description with optional indicator (shows bullet separator)
+const bioId = useId();
+const bioDescId = useId();
+<XDSField
+ label="Bio"
+ description="Tell us about yourself"
+ isOptional
+ inputID={bioId}
+ descriptionID={bioDescId}
+>
+ <input id={bioId} aria-describedby={bioDescId} />
+</XDSField>
```
## Props
@@ -51,6 +77,8 @@ const searchId = useId();
| `description` | `string` | No | Description text displayed between the label and input |
| `inputID` | `string` | Yes | ID for the input element (used for label's htmlFor attribute) |
| `descriptionID` | `string` | No | ID for the description element (use for aria-describedby on the input) |
+| `isOptional` | `boolean` | No | Whether the field is optional (mutually exclusive with isRequired) |
+| `isRequired` | `boolean` | No | Whether the field is required (mutually exclusive with isOptional) |
| `children` | `ReactNode` | Yes | The input or control to render |
## Files
@@ -67,3 +95,5 @@ const searchId = useId();
- Label is always rendered for accessibility; use `isLabelHidden` to hide visually
- Hidden label uses CSS technique that remains accessible to screen readers
- Description is rendered when provided; if `descriptionID` is also provided, the description element gets that ID for `aria-describedby` association
+- `isOptional` and `isRequired` are mutually exclusive; setting both will show "Optional"
+- Optional/Required text appears on the same line as the label
diff --git a/packages/core/src/Field/XDSField.test.tsx b/packages/core/src/Field/XDSField.test.tsx
index 6d9c26340f..76da43b178 100644
--- a/packages/core/src/Field/XDSField.test.tsx
+++ b/packages/core/src/Field/XDSField.test.tsx
@@ -82,4 +82,53 @@ describe('XDSField', () => {
expect(description).toBeInTheDocument();
expect(description).not.toHaveAttribute('id');
});
+
+ it('renders Optional text when isOptional is true', () => {
+ render(
+ <XDSField label="Name" inputID="name-input" isOptional>
+ <input id="name-input" />
+ </XDSField>
+ );
+ expect(screen.getByText('Optional')).toBeInTheDocument();
+ });
+
+ it('renders Required text when isRequired is true', () => {
+ render(
+ <XDSField label="Name" inputID="name-input" isRequired>
+ <input id="name-input" />
+ </XDSField>
+ );
+ expect(screen.getByText('Required')).toBeInTheDocument();
+ });
+
+ it('renders description and Optional text separately', () => {
+ render(
+ <XDSField label="Name" inputID="name-input" description="Enter your name" descriptionID="name-desc" isOptional>
+ <input id="name-input" aria-describedby="name-desc" />
+ </XDSField>
+ );
+ expect(screen.getByText('Enter your name')).toBeInTheDocument();
+ expect(screen.getByText('Optional')).toBeInTheDocument();
+ });
+
+ it('renders description and Required text separately', () => {
+ render(
+ <XDSField label="Name" inputID="name-input" description="This field is mandatory" descriptionID="name-desc" isRequired>
+ <input id="name-input" aria-describedby="name-desc" />
+ </XDSField>
+ );
+ expect(screen.getByText('This field is mandatory')).toBeInTheDocument();
+ expect(screen.getByText('Required')).toBeInTheDocument();
+ });
+
+ it('renders Optional text next to label with bullet separator', () => {
+ render(
+ <XDSField label="Name" inputID="name-input" isOptional>
+ <input id="name-input" />
+ </XDSField>
+ );
+ expect(screen.getByText('Optional')).toBeInTheDocument();
+ expect(screen.getByText('Name')).toBeInTheDocument();
+ expect(screen.getByText('∙', { exact: false })).toBeInTheDocument();
+ });
});
diff --git a/packages/core/src/Field/XDSField.tsx b/packages/core/src/Field/XDSField.tsx
index c9bbccbbaf..9dad25cb27 100644
--- a/packages/core/src/Field/XDSField.tsx
+++ b/packages/core/src/Field/XDSField.tsx
@@ -21,6 +21,11 @@ const styles = stylex.create({
flexDirection: 'column',
gap: spacing.space1,
},
+ labelRow: {
+ display: 'flex',
+ flexDirection: 'row',
+ alignItems: 'baseline',
+ },
label: {
fontFamily: typography.fontFamilyBody,
fontSize: '0.875rem',
@@ -47,6 +52,12 @@ const styles = stylex.create({
fontSize: '0.75rem',
color: color.textSecondary,
},
+ optionalRequired: {
+ fontFamily: typography.fontFamilyBody,
+ fontSize: '0.75rem',
+ color: color.textSecondary,
+ marginInlineStart: spacing.space1,
+ },
});
export interface XDSFieldProps extends Omit<HTMLAttributes<HTMLDivElement>, 'children'> {
@@ -71,6 +82,16 @@ export interface XDSFieldProps extends Omit<HTMLAttributes<HTMLDivElement>, 'chi
* ID for the description element (use for aria-describedby on the input).
*/
descriptionID?: string;
+ /**
+ * Whether the field is optional. Mutually exclusive with isRequired.
+ * @default false
+ */
+ isOptional?: boolean;
+ /**
+ * Whether the field is required. Mutually exclusive with isOptional.
+ * @default false
+ */
+ isRequired?: boolean;
/**
* The input or control to render inside the field.
*/
@@ -90,16 +111,24 @@ export interface XDSFieldProps extends Omit<HTMLAttributes<HTMLDivElement>, 'chi
* ```
*/
export const XDSField = forwardRef<HTMLDivElement, XDSFieldProps>(
- ({ label, isLabelHidden = false, description, inputID, descriptionID, children, ...props }, ref) => {
+ ({ label, isLabelHidden = false, description, inputID, descriptionID, isOptional = false, isRequired = false, children, ...props }, ref) => {
return (
<div ref={ref} {...stylex.props(styles.container)} {...props}>
- <label
- htmlFor={inputID}
- {...stylex.props(styles.label, isLabelHidden && styles.labelHidden)}
- >
- {label}
- </label>
- {description && (
+ <div {...stylex.props(styles.labelRow)}>
+ <label
+ htmlFor={inputID}
+ {...stylex.props(styles.label, isLabelHidden && styles.labelHidden)}
+ >
+ {label}
+ </label>
+ {isOptional || isRequired ? (
+ <span {...stylex.props(styles.optionalRequired)}>
+ <span aria-hidden="true">{'\u2219 '}</span>
+ {isOptional ? 'Optional' : 'Required'}
+ </span>
+ ) : null}
+ </div>
+ {description != null && (
<span id={descriptionID} {...stylex.props(styles.description)}>
{description}
</span>
diff --git a/packages/core/src/TextInput/README.md b/packages/core/src/TextInput/README.md
index 98be60c5e3..394532e96d 100644
--- a/packages/core/src/TextInput/README.md
+++ b/packages/core/src/TextInput/README.md
@@ -7,6 +7,8 @@ A text input component for collecting user text input.
## Features
- **Label Support**: Required label for accessibility (can be visually hidden)
+- **Description**: Optional description text displayed between the label and input
+- **Optional/Required Indicators**: Display "Optional" or "Required" text with bullet separator
- **Accessible**: Label properly associated with input via htmlFor/id
- **Styled with StyleX**: Uses XDS design tokens for consistent styling
@@ -23,6 +25,18 @@ import { XDSTextInput } from '@xds/core/TextInput';
// Hidden label (for screen readers only)
<XDSTextInput label="Search" isLabelHidden value={query} onChange={setQuery} placeholder="Search..." />
+
+// With description
+<XDSTextInput label="Email" description="We'll never share your email" value={email} onChange={setEmail} />
+
+// Optional field
+<XDSTextInput label="Nickname" isOptional value={nickname} onChange={setNickname} />
+
+// Required field
+<XDSTextInput label="Username" isRequired value={username} onChange={setUsername} />
+
+// Description with optional indicator
+<XDSTextInput label="Bio" description="Tell us about yourself" isOptional value={bio} onChange={setBio} />
```
## Props
@@ -33,6 +47,9 @@ import { XDSTextInput } from '@xds/core/TextInput';
| `value` | `string` | Yes | Current value of the input |
| `onChange` | `(value: string, e: ChangeEvent<HTMLInputElement>) => void` | Yes | Callback fired when input value changes |
| `isLabelHidden` | `boolean` | No | Visually hide the label (still accessible to screen readers) |
+| `description` | `string` | No | Description text displayed between the label and input |
+| `isOptional` | `boolean` | No | Whether the field is optional (mutually exclusive with isRequired) |
+| `isRequired` | `boolean` | No | Whether the field is required (mutually exclusive with isOptional) |
| `placeholder` | `string` | No | Placeholder text |
## Files
@@ -48,3 +65,6 @@ import { XDSTextInput } from '@xds/core/TextInput';
- Uses `useId` hook for accessible label-input association
- Label is always rendered for accessibility; use `isLabelHidden` to hide visually
- Hidden label uses CSS technique that remains accessible to screen readers
+- Wraps `XDSField` component for label, description, and optional/required handling
+- `isOptional` and `isRequired` are mutually exclusive; setting both will show "Optional"
+- Optional/Required text appears on the same line as the label
diff --git a/packages/core/src/TextInput/XDSTextInput.test.tsx b/packages/core/src/TextInput/XDSTextInput.test.tsx
index b7e7a39c17..ad6404ee93 100644
--- a/packages/core/src/TextInput/XDSTextInput.test.tsx
+++ b/packages/core/src/TextInput/XDSTextInput.test.tsx
@@ -68,4 +68,14 @@ describe('XDSTextInput', () => {
const label = screen.getByText('Email');
expect(label).toBeVisible();
});
+
+ it('sets aria-required when isRequired is true', () => {
+ render(<XDSTextInput label="Username" isRequired value="" onChange={() => {}} />);
+ expect(screen.getByRole('textbox')).toHaveAttribute('aria-required', 'true');
+ });
+
+ it('does not set aria-required when isRequired is false', () => {
+ render(<XDSTextInput label="Username" value="" onChange={() => {}} />);
+ expect(screen.getByRole('textbox')).not.toHaveAttribute('aria-required');
+ });
});
diff --git a/packages/core/src/TextInput/XDSTextInput.tsx b/packages/core/src/TextInput/XDSTextInput.tsx
index 1c14367dab..2aaaa02623 100644
--- a/packages/core/src/TextInput/XDSTextInput.tsx
+++ b/packages/core/src/TextInput/XDSTextInput.tsx
@@ -64,6 +64,16 @@ export interface XDSTextInputProps {
* Description text displayed between the label and input.
*/
description?: string;
+ /**
+ * Whether the field is optional. Mutually exclusive with isRequired.
+ * @default false
+ */
+ isOptional?: boolean;
+ /**
+ * Whether the field is required. Mutually exclusive with isOptional.
+ * @default false
+ */
+ isRequired?: boolean;
/**
* Callback fired when the input value changes.
*/
@@ -88,7 +98,7 @@ export interface XDSTextInputProps {
* ```
*/
export const XDSTextInput = forwardRef<HTMLInputElement, XDSTextInputProps>(
- ({ label, isLabelHidden = false, description, onChange, value, placeholder }, ref) => {
+ ({ label, isLabelHidden = false, description, isOptional = false, isRequired = false, onChange, value, placeholder }, ref) => {
const id = useId();
const descriptionID = useId();
@@ -99,6 +109,8 @@ export const XDSTextInput = forwardRef<HTMLInputElement, XDSTextInputProps>(
description={description}
inputID={id}
descriptionID={description ? descriptionID : undefined}
+ isOptional={isOptional}
+ isRequired={isRequired}
>
<input
ref={ref}
@@ -108,6 +120,7 @@ export const XDSTextInput = forwardRef<HTMLInputElement, XDSTextInputProps>(
onChange={(e) => onChange(e.target.value, e)}
placeholder={placeholder}
aria-describedby={description ? descriptionID : undefined}
+ aria-required={isRequired === true ? 'true' : undefined}
{...stylex.props(styles.input)}
/>
</XDSField>