Skip to content

Commit 6f1a82e

Browse files
authored
FEATURE: Add optional Id prop on ErrorMessageProps type (#3825)
- Additional optional `id` prop added to `ErrorMessageProps` to make it easily testable in e2e tests. - Updated the docs of the section ErrorMessage and added an example to demonstrate this update. - Since it doesn't change the functionality, it didn't need to be covered under any test case. **NOTE:** This change won't affect any functionalities and since it is an optional param, it can be omitted if not needed. **This is my first contribution to this project, please let me know if I missed something..**
1 parent 48671ac commit 6f1a82e

File tree

5 files changed

+39
-18
lines changed

5 files changed

+39
-18
lines changed

docs/api/errormessage.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ Either a React component or the name of an HTML element to render. If not specif
9797
// --> {touched.email && error.email ? error.email : null}
9898
```
9999

100+
### `id`
101+
102+
`id?: string`
103+
104+
A field's id in Formik state. To get access to DOM elements for e2e testing purposes, tt doesn't impact the implementation in any way as the prop can still be omitted.
105+
```jsx
106+
// id will be used only for testing purposes
107+
// not contributing anything to the core implementation.
108+
<ErrorMessage name="email" id="form_email_id" />
109+
```
110+
100111
### `name`
101112

102113
`name: string`

packages/formik/src/ErrorMessage.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getIn, isFunction } from './utils';
44
import { connect } from './connect';
55

66
export interface ErrorMessageProps {
7+
id?: string;
78
name: string;
89
className?: string;
910
component?: string | React.ComponentType;

packages/formik/test/Field.test.tsx

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -426,23 +426,31 @@ describe('Field / FastField', () => {
426426
}
427427
);
428428

429-
cases('constructs error for a nested field when validateField is called', async (renderField) => {
430-
const validationSchema = Yup.object({
431-
user: Yup.object().shape({
432-
name: Yup.string().required('required')
433-
})
434-
});
429+
cases(
430+
'constructs error for a nested field when validateField is called',
431+
async renderField => {
432+
const validationSchema = Yup.object({
433+
user: Yup.object().shape({
434+
name: Yup.string().required('required'),
435+
}),
436+
});
435437

436-
const { getFormProps, rerender } = renderField({}, { validationSchema });
438+
const { getFormProps, rerender } = renderField(
439+
{},
440+
{ validationSchema }
441+
);
437442

438-
rerender();
443+
rerender();
439444

440-
act(() => {
441-
getFormProps().validateField('user.name');
442-
})
445+
act(() => {
446+
getFormProps().validateField('user.name');
447+
});
443448

444-
await waitFor(() => expect(getFormProps().errors).toEqual({ user: { name: 'required' }}));
445-
})
449+
await waitFor(() =>
450+
expect(getFormProps().errors).toEqual({ user: { name: 'required' } })
451+
);
452+
}
453+
);
446454
});
447455

448456
describe('warnings', () => {

website/src/components/clients/Client.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import Image from "next/image";
2+
import Image from 'next/image';
33

44
interface ClientProps {
55
name: string;
@@ -19,9 +19,10 @@ export const Client = React.memo<ClientProps>(
1919
loading="lazy"
2020
className="inline"
2121
style={{
22-
maxWidth: "100%",
23-
height: "auto"
24-
}} />
22+
maxWidth: '100%',
23+
height: 'auto',
24+
}}
25+
/>
2526
</span>
2627
)
2728
);

website/src/components/forwardRefWithAs.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export function forwardRefWithAs<Props, ComponentType extends As>(
8686
ref: React.RefObject<any>
8787
) => React.ReactElement | null
8888
) {
89-
return React.forwardRef(comp as any) as unknown as ComponentWithAs<
89+
return (React.forwardRef(comp as any) as unknown) as ComponentWithAs<
9090
ComponentType,
9191
Props
9292
>;

0 commit comments

Comments
 (0)