-
Notifications
You must be signed in to change notification settings - Fork 60
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
feat: upstream FormikField #1054
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React from "react"; | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
|
||
import FormikField from "./FormikField"; | ||
import Select from "../Select"; | ||
import { Formik } from "formik"; | ||
|
||
const meta: Meta<typeof FormikField> = { | ||
title: "FormikField", | ||
component: FormikField, | ||
tags: ["autodocs"], | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof FormikField>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
name: "username", | ||
label: "Username", | ||
type: "text", | ||
}, | ||
render: (args) => ( | ||
<Formik initialValues={{ username: "" }} onSubmit={() => {}}> | ||
<FormikField {...args} /> | ||
</Formik> | ||
), | ||
}; | ||
|
||
export const Fields: Story = { | ||
args: { | ||
component: Select, | ||
name: "release", | ||
label: "Release", | ||
options: [ | ||
{ value: "", disabled: true, label: "Select an option" }, | ||
{ value: "1", label: "Cosmic Cuttlefish" }, | ||
{ value: "2", label: "Bionic Beaver" }, | ||
{ value: "3", label: "Xenial Xerus" }, | ||
], | ||
}, | ||
parameters: { | ||
docs: { | ||
description: { | ||
story: ` | ||
Any React Components input can be provided to FormikField (e.g. Input, Textarea or Select) or you may provide a custom component. | ||
|
||
Any additional props that need to be passed can be given to FormikField. | ||
`, | ||
}, | ||
}, | ||
}, | ||
render: (args) => ( | ||
<Formik initialValues={{ release: "" }} onSubmit={() => {}}> | ||
<FormikField {...args} /> | ||
</Formik> | ||
), | ||
}; | ||
|
||
export const Errors: Story = { | ||
args: Default.args, | ||
parameters: { | ||
docs: { | ||
description: { | ||
story: ` | ||
Formik parameters are passed to the field using Formik's \`useField\`. This means that validation and errors, state handlers etc. should all just work. | ||
`, | ||
}, | ||
}, | ||
}, | ||
render: (args) => ( | ||
<Formik | ||
initialErrors={{ username: "This username has already been taken." }} | ||
initialTouched={{ username: true }} | ||
initialValues={{ username: "" }} | ||
onSubmit={() => {}} | ||
> | ||
<FormikField {...args} /> | ||
</Formik> | ||
), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React from "react"; | ||
import { render, screen } from "@testing-library/react"; | ||
import { Formik } from "formik"; | ||
|
||
import FormikField from "./FormikField"; | ||
|
||
describe("FormikField", () => { | ||
it("can set a different component", () => { | ||
const Component = () => <select />; | ||
render( | ||
<Formik initialValues={{}} onSubmit={jest.fn()}> | ||
<FormikField component={Component} name="username" /> | ||
</Formik> | ||
); | ||
|
||
expect(screen.getByRole("combobox")).toBeInTheDocument(); | ||
expect(screen.queryByRole("textbox")).not.toBeInTheDocument(); | ||
}); | ||
|
||
it("can pass errors", () => { | ||
render( | ||
<Formik | ||
initialErrors={{ username: "Uh oh!" }} | ||
initialTouched={{ username: true }} | ||
initialValues={{ username: "" }} | ||
onSubmit={jest.fn()} | ||
> | ||
<FormikField name="username" /> | ||
</Formik> | ||
); | ||
|
||
expect(screen.getByRole("textbox")).toHaveAccessibleErrorMessage( | ||
"Error: Uh oh!" | ||
); | ||
}); | ||
|
||
it("can hide the errors", () => { | ||
render( | ||
<Formik | ||
initialErrors={{ username: "Uh oh!" }} | ||
initialTouched={{ username: true }} | ||
initialValues={{ username: "" }} | ||
onSubmit={jest.fn()} | ||
> | ||
<FormikField displayError={false} name="username" /> | ||
</Formik> | ||
); | ||
|
||
expect(screen.getByRole("textbox")).not.toHaveAccessibleErrorMessage(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React from "react"; | ||
import { useField } from "formik"; | ||
import { | ||
type ComponentProps, | ||
type ComponentType, | ||
type ElementType, | ||
type HTMLProps, | ||
} from "react"; | ||
import Input from "components/Input"; | ||
|
||
export type Props<C extends ElementType | ComponentType = typeof Input> = { | ||
/** | ||
* The component to display. | ||
* @default Input | ||
*/ | ||
component?: C; | ||
/** | ||
* This can be used to hide errors returned by Formik. | ||
*/ | ||
displayError?: boolean; | ||
/** | ||
* The name of the field as given to Formik. | ||
*/ | ||
name: string; | ||
value?: HTMLProps<HTMLElement>["value"]; | ||
} & ComponentProps<C>; | ||
|
||
/** | ||
* This component makes it easier to use Vanilla form inputs with Formik. It | ||
* makes use of Formik's context to automatically map errors, values, states | ||
* etc. onto the provided field. | ||
*/ | ||
const FormikField = <C extends ElementType | ComponentType = typeof Input>({ | ||
component: Component = Input, | ||
displayError = true, | ||
name, | ||
value, | ||
label, | ||
...props | ||
}: Props<C>): JSX.Element => { | ||
const [field, meta] = useField({ name, type: props.type, value }); | ||
|
||
return ( | ||
<Component | ||
aria-label={label} | ||
error={meta.touched && displayError ? meta.error : null} | ||
label={label} | ||
{...field} | ||
{...props} | ||
/> | ||
); | ||
}; | ||
|
||
export default FormikField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default, type Props as FormikFieldProps } from "./FormikField"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the docs doesn't render as expected. For instance:
Show code
, the entire code is presented, but I assume we only want the bit fromrender
to be displayed.The first issue also appears in
Card
stories, which is one of the few other.stories.tsx
docs. There are also some slight inconsistencies in presentation between.stories.tsx
and.stories.mdx
docs e.g. the Props header missing in.stories.tsx
docs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an open issue here, with a workaround that I've applied to both files: storybookjs/storybook#22281.
It looks like the table couldn't handle the
Input
component as a default but this can be set via a jsdoc string so I've done that.It looks like it was decided not to customise the page too much and stick with the CSF defaults: #1010 (comment).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation and links, and for also fixing the inconsistency in
Card
stories!