Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions packages/react-aria-components/src/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@ export interface TextFieldRenderProps {
* Whether the value is invalid.
* @selector [data-invalid]
*/
isInvalid: boolean
isInvalid: boolean,
/**
* Whether the text field is read only.
* @selector [data-readonly]
*/
isReadOnly: boolean,
/**
* Whether the text field is required.
* @selector [data-required]
*/
isRequired: boolean
}

export interface TextFieldProps extends Omit<AriaTextFieldProps, 'label' | 'placeholder' | 'description' | 'errorMessage' | 'validationState' | 'validationBehavior'>, RACValidation, Omit<DOMProps, 'style' | 'className' | 'children'>, SlotProps, RenderProps<TextFieldRenderProps> {
Expand Down Expand Up @@ -65,7 +75,9 @@ function TextField(props: TextFieldProps, ref: ForwardedRef<HTMLDivElement>) {
...props,
values: {
isDisabled: props.isDisabled || false,
isInvalid: validation.isInvalid
isInvalid: validation.isInvalid,
isReadOnly: props.isReadOnly || false,
isRequired: props.isRequired || false
},
defaultClassName: 'react-aria-TextField'
});
Expand All @@ -77,7 +89,9 @@ function TextField(props: TextFieldProps, ref: ForwardedRef<HTMLDivElement>) {
ref={ref}
slot={props.slot || undefined}
data-disabled={props.isDisabled || undefined}
data-invalid={validation.isInvalid || undefined}>
data-invalid={validation.isInvalid || undefined}
data-readonly={props.isReadOnly || undefined}
data-required={props.isRequired || undefined}>
<Provider
values={[
[LabelContext, {...labelProps, ref: labelRef}],
Expand Down
24 changes: 24 additions & 0 deletions packages/react-aria-components/test/TextField.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,30 @@ describe('TextField', () => {
expect(input).not.toHaveClass('focus');
});

it('should support read-only state', async () => {
let {getByRole, rerender} = render(
<TestTextField input={component} />
);

let input = getByRole('textbox');

expect(input.closest('.react-aria-TextField')).not.toHaveAttribute('data-readonly');
rerender(<TestTextField input={component} isReadOnly />);
expect(input.closest('.react-aria-TextField')).toHaveAttribute('data-readonly');
});

it('should support required state', async () => {
let {getByRole, rerender} = render(
<TestTextField input={component} />
);

let input = getByRole('textbox');

expect(input.closest('.react-aria-TextField')).not.toHaveAttribute('data-required');
rerender(<TestTextField input={component} isRequired />);
expect(input.closest('.react-aria-TextField')).toHaveAttribute('data-required');
});

it('should render data- attributes only on the outer element', () => {
let {getAllByTestId} = render(<TestTextField input={component} />);
let outerEl = getAllByTestId('text-field-test');
Expand Down