Skip to content

Commit 0c7746a

Browse files
authored
fix: add missing data attributes to ColorField (#9105)
* fix: add missing data attributes to Color Field * Add isReadonly and isRequired to renderProps * add tests
1 parent f7811f3 commit 0c7746a

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

packages/react-aria-components/src/ColorField.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ export interface ColorFieldRenderProps {
4444
* @selector [data-invalid]
4545
*/
4646
isInvalid: boolean,
47+
/**
48+
* Whether the color field is read only.
49+
* @selector [data-readonly]
50+
*/
51+
isReadOnly: boolean,
52+
/**
53+
* Whether the color field is required.
54+
* @selector [data-required]
55+
*/
56+
isRequired: boolean,
4757
/**
4858
* The color channel that this field edits, or "hex" if no `channel` prop is set.
4959
* @selector [data-channel="hex | hue | saturation | ..."]
@@ -192,7 +202,9 @@ function useChildren(
192202
state,
193203
channel: props.channel || 'hex',
194204
isDisabled: props.isDisabled || false,
195-
isInvalid: validation.isInvalid || false
205+
isInvalid: validation.isInvalid || false,
206+
isReadOnly: props.isReadOnly || false,
207+
isRequired: props.isRequired || false
196208
},
197209
defaultClassName: 'react-aria-ColorField'
198210
});
@@ -222,7 +234,9 @@ function useChildren(
222234
slot={props.slot || undefined}
223235
data-channel={props.channel || 'hex'}
224236
data-disabled={props.isDisabled || undefined}
225-
data-invalid={validation.isInvalid || undefined} />
237+
data-invalid={validation.isInvalid || undefined}
238+
data-readonly={props.isReadOnly || undefined}
239+
data-required={props.isRequired || undefined} />
226240
</Provider>
227241
);
228242
}

packages/react-aria-components/test/ColorField.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,30 @@ describe('ColorField', () => {
8484
expect(input).toHaveValue('');
8585
});
8686

87+
it('should support read-only state', async () => {
88+
let {getByRole, rerender} = render(
89+
<TestColorField />
90+
);
91+
92+
let input = getByRole('textbox');
93+
94+
expect(input.closest('.react-aria-ColorField')).not.toHaveAttribute('data-readonly');
95+
rerender(<TestColorField isReadOnly />);
96+
expect(input.closest('.react-aria-ColorField')).toHaveAttribute('data-readonly');
97+
});
98+
99+
it('should support required state', async () => {
100+
let {getByRole, rerender} = render(
101+
<TestColorField />
102+
);
103+
104+
let input = getByRole('textbox');
105+
106+
expect(input.closest('.react-aria-ColorField')).not.toHaveAttribute('data-required');
107+
rerender(<TestColorField isRequired />);
108+
expect(input.closest('.react-aria-ColorField')).toHaveAttribute('data-required');
109+
});
110+
87111
it('should render data- attributes only on the outer element', () => {
88112
let {getAllByTestId} = render(
89113
<TestColorField data-testid="number-field" />

0 commit comments

Comments
 (0)