Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Add optional Id prop on ErrorMessageProps type #3825

Merged
merged 3 commits into from
Jun 22, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/api/errormessage.md
Expand Up @@ -97,6 +97,17 @@ Either a React component or the name of an HTML element to render. If not specif
// --> {touched.email && error.email ? error.email : null}
```

### `id`

`id?: string`

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.
```jsx
// id will be used only for testing purposes
// not contributing anything to the core implementation.
<ErrorMessage name="email" id="form_email_id" />
```

### `name`

`name: string`
Expand Down
1 change: 1 addition & 0 deletions packages/formik/src/ErrorMessage.tsx
Expand Up @@ -4,6 +4,7 @@ import { getIn, isFunction } from './utils';
import { connect } from './connect';

export interface ErrorMessageProps {
id?: string;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just allow all typical div attributes and pull off our specific ones during render?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

export interface ErrorMessageProps extends React.HTMLAttributes<HTMLDivElement> {
  component?: string | React.ComponentType;
  children?: (errorMessage: string) => React.ReactNode;
  name: string;
  render?: (errorMessage: string) => React.ReactNode;
}

Not sure if there's a native-specific version of this?

name: string;
className?: string;
component?: string | React.ComponentType;
Expand Down
34 changes: 21 additions & 13 deletions packages/formik/test/Field.test.tsx
Expand Up @@ -426,23 +426,31 @@ describe('Field / FastField', () => {
}
);

cases('constructs error for a nested field when validateField is called', async (renderField) => {
const validationSchema = Yup.object({
user: Yup.object().shape({
name: Yup.string().required('required')
})
});
cases(
'constructs error for a nested field when validateField is called',
async renderField => {
const validationSchema = Yup.object({
user: Yup.object().shape({
name: Yup.string().required('required'),
}),
});

const { getFormProps, rerender } = renderField({}, { validationSchema });
const { getFormProps, rerender } = renderField(
{},
{ validationSchema }
);

rerender();
rerender();

act(() => {
getFormProps().validateField('user.name');
})
act(() => {
getFormProps().validateField('user.name');
});

await waitFor(() => expect(getFormProps().errors).toEqual({ user: { name: 'required' }}));
})
await waitFor(() =>
expect(getFormProps().errors).toEqual({ user: { name: 'required' } })
);
}
);
});

describe('warnings', () => {
Expand Down
9 changes: 5 additions & 4 deletions website/src/components/clients/Client.tsx
@@ -1,5 +1,5 @@
import React from 'react';
import Image from "next/image";
import Image from 'next/image';

interface ClientProps {
name: string;
Expand All @@ -19,9 +19,10 @@ export const Client = React.memo<ClientProps>(
loading="lazy"
className="inline"
style={{
maxWidth: "100%",
height: "auto"
}} />
maxWidth: '100%',
height: 'auto',
}}
/>
</span>
)
);
2 changes: 1 addition & 1 deletion website/src/components/forwardRefWithAs.tsx
Expand Up @@ -86,7 +86,7 @@ export function forwardRefWithAs<Props, ComponentType extends As>(
ref: React.RefObject<any>
) => React.ReactElement | null
) {
return React.forwardRef(comp as any) as unknown as ComponentWithAs<
return (React.forwardRef(comp as any) as unknown) as ComponentWithAs<
ComponentType,
Props
>;
Expand Down