Skip to content

Commit

Permalink
feat(RAC): Allow passing data attributes to FieldError (adobe#6895)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Aug 21, 2024
1 parent 7336849 commit 3aa13af
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/react-aria-components/src/FieldError.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
* governing permissions and limitations under the License.
*/

import {DOMProps, ValidationResult} from "@react-types/shared";
import {filterDOMProps} from "@react-aria/utils";
import React, {createContext, ForwardedRef, forwardRef, useContext} from 'react';
import {RenderProps, useRenderProps} from './utils';
import {Text} from './Text';
import {ValidationResult} from '@react-types/shared';

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

export interface FieldErrorRenderProps extends ValidationResult {}
export interface FieldErrorProps extends RenderProps<FieldErrorRenderProps> {}
export interface FieldErrorProps extends RenderProps<FieldErrorRenderProps>, DOMProps {}

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

const FieldErrorInner = forwardRef((props: FieldErrorProps, ref: ForwardedRef<HTMLElement>) => {
let validation = useContext(FieldErrorContext)!;
let domProps = filterDOMProps(props)!;
let renderProps = useRenderProps({
...props,
defaultClassName: 'react-aria-FieldError',
Expand All @@ -48,5 +50,5 @@ const FieldErrorInner = forwardRef((props: FieldErrorProps, ref: ForwardedRef<HT
return null;
}

return <Text slot="errorMessage" {...renderProps} ref={ref} />;
return <Text slot="errorMessage" {...domProps} {...renderProps} ref={ref} />;
});
23 changes: 23 additions & 0 deletions packages/react-aria-components/test/FieldError.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {FieldError} from '../src/FieldError';
import {Input} from '../src/Input';
import {Label} from '../src/Label';
import React from 'react';
import {render} from '@react-spectrum/test-utils-internal';
import {TextField} from '../src/TextField';

describe('FieldError', function () {
it('supports data attributes passed as props', async () => {
const TEST_ID = 'testid';

const {getByTestId} = render(
<TextField isInvalid>
<Label>Email</Label>
<Input />
<FieldError data-testid={TEST_ID}>An error</FieldError>
</TextField>
);

const element = getByTestId(TEST_ID);
expect(element).toHaveTextContent('An error');
});
});

0 comments on commit 3aa13af

Please sign in to comment.