Skip to content

Commit 3aa13af

Browse files
omarnytesnowystingerreidbarber
authored
feat(RAC): Allow passing data attributes to FieldError (#6895)
* Allow passing data attributes to FieldError * Update FieldError.tsx just fixing some lint, not sure how it passed --------- Co-authored-by: Robert Snow <[email protected]> Co-authored-by: Reid Barber <[email protected]>
1 parent 7336849 commit 3aa13af

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13+
import {DOMProps, ValidationResult} from "@react-types/shared";
14+
import {filterDOMProps} from "@react-aria/utils";
1315
import React, {createContext, ForwardedRef, forwardRef, useContext} from 'react';
1416
import {RenderProps, useRenderProps} from './utils';
1517
import {Text} from './Text';
16-
import {ValidationResult} from '@react-types/shared';
1718

1819
export const FieldErrorContext = createContext<ValidationResult | null>(null);
1920

2021
export interface FieldErrorRenderProps extends ValidationResult {}
21-
export interface FieldErrorProps extends RenderProps<FieldErrorRenderProps> {}
22+
export interface FieldErrorProps extends RenderProps<FieldErrorRenderProps>, DOMProps {}
2223

2324
function FieldError(props: FieldErrorProps, ref: ForwardedRef<HTMLElement>) {
2425
let validation = useContext(FieldErrorContext);
@@ -37,6 +38,7 @@ export {_FieldError as FieldError};
3738

3839
const FieldErrorInner = forwardRef((props: FieldErrorProps, ref: ForwardedRef<HTMLElement>) => {
3940
let validation = useContext(FieldErrorContext)!;
41+
let domProps = filterDOMProps(props)!;
4042
let renderProps = useRenderProps({
4143
...props,
4244
defaultClassName: 'react-aria-FieldError',
@@ -48,5 +50,5 @@ const FieldErrorInner = forwardRef((props: FieldErrorProps, ref: ForwardedRef<HT
4850
return null;
4951
}
5052

51-
return <Text slot="errorMessage" {...renderProps} ref={ref} />;
53+
return <Text slot="errorMessage" {...domProps} {...renderProps} ref={ref} />;
5254
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {FieldError} from '../src/FieldError';
2+
import {Input} from '../src/Input';
3+
import {Label} from '../src/Label';
4+
import React from 'react';
5+
import {render} from '@react-spectrum/test-utils-internal';
6+
import {TextField} from '../src/TextField';
7+
8+
describe('FieldError', function () {
9+
it('supports data attributes passed as props', async () => {
10+
const TEST_ID = 'testid';
11+
12+
const {getByTestId} = render(
13+
<TextField isInvalid>
14+
<Label>Email</Label>
15+
<Input />
16+
<FieldError data-testid={TEST_ID}>An error</FieldError>
17+
</TextField>
18+
);
19+
20+
const element = getByTestId(TEST_ID);
21+
expect(element).toHaveTextContent('An error');
22+
});
23+
});

0 commit comments

Comments
 (0)