-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Fix: Introduce useFastField React hook #1772
Closed
Closed
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
--- | ||
id: useFastField | ||
title: useFastField() | ||
custom_edit_url: https://github.com/jaredpalmer/formik/edit/master/docs/api/useFastField.md | ||
--- | ||
|
||
`useFastField` is a custom React hook that will automagically help you hook up inputs in a similar way to `useField`. | ||
The `useFastField` behaviour imitates `useField` when Formik is configured to validate onBlur. The Formik model is not updated after an onChange event, only the input field is updated. After an onBlur event occurs, the Formik v model is updated resulting in a more performant way at the expense of some minor differences. There are 2 ways to use it. | ||
|
||
## Example | ||
|
||
```tsx | ||
import React from 'react'; | ||
import { useFastField, Formik } from 'formik'; | ||
|
||
const MyTextField = ({ label, ...props }) => { | ||
const [field, meta] = useFastField(props.name); | ||
return ( | ||
<> | ||
<label> | ||
{label} | ||
<input {...field} {...props} /> | ||
</label> | ||
{meta.touched && meta.error ? ( | ||
<div className="error">{meta.error}</div> | ||
) : null} | ||
</> | ||
); | ||
}; | ||
|
||
const Example = () => ( | ||
<div> | ||
<h1>My Form</h1> | ||
<Formik | ||
initialValues={{ email: '', firstName: 'red', lastName: '' }} | ||
onSubmit={(values, actions) => { | ||
setTimeout(() => { | ||
alert(JSON.stringify(values, null, 2)); | ||
actions.setSubmitting(false); | ||
}, 1000); | ||
}} | ||
render={(props: FormikProps<Values>) => ( | ||
<form onSubmit={props.handleSubmit}> | ||
<MyTextField name="firstName" type="text" label="First Name" /> | ||
<MyTextField name="lastName" type="text" label="Last Name" /> | ||
<MyTextField name="email" type="email" label="Email" /> | ||
<button type="submit">Submit</button> | ||
</form> | ||
)} | ||
/> | ||
</div> | ||
); | ||
``` | ||
|
||
--- | ||
|
||
# Reference | ||
|
||
## `useFastField<Value = any>(name: string): [FieldInputProps<Value>, FieldMetaProps<Value>]` | ||
|
||
A custom React Hook that returns a tuple (2 element array) containing `FieldProps` and `FieldMetaProps`. It accepts either a string of a field name or an object as an argument. The object must at least contain a `name` key. This object should identical to the props that you would pass to `<FastField>` and the returned helpers will imitate the behavior of `<FastField>`. This is useful, and generally preferred, since it allows you to take advantage of formik's checkbox, radio, and multiple select behavior when the object contains the relevant key/values (e.g. `type: 'checkbox'`, `multiple: true`, etc.). | ||
|
||
```jsx | ||
import React from 'react'; | ||
import { useFastField } from 'formik'; | ||
|
||
function MyTextField(props) { | ||
// this will return field props for an <input /> | ||
const [field, meta] = useFastField(props.name); | ||
return ( | ||
<> | ||
<input {...field} {...props} /> | ||
{meta.error && meta.touched && <div>{meta.error}</div>} | ||
</> | ||
); | ||
} | ||
|
||
function MyInput(props) { | ||
// this will return field exactly like <Field>{({ field }) => ... }</Field> | ||
const [field, meta] = useFastField(props); | ||
return ( | ||
<> | ||
<input {...field} {...props} /> | ||
{meta.error && meta.touched && <div>{meta.error}</div>} | ||
</> | ||
); | ||
} | ||
``` | ||
|
||
### `FieldInputProps` | ||
|
||
An object that contains: | ||
|
||
- `name: string` - The name of the field | ||
- `checked?: boolean` - Whether or not the input is checked, this is only defined if `useField` is passed an object with a `name`, `type: "checkbox"` or `type: radio`. | ||
- `onBlur: () => void;` - A blur event handler | ||
- `onChange: (e: React.ChangeEvent<any>) => void` - A change event handler | ||
- `value: any` - The field's value (plucked out of `values`) or, if it is a checkbox or radio input, then potentially the `value` passed into `useField`. | ||
- `multiple?: boolean` - Whether or not the multiple values can be selected. This is only ever defined when `useField` is passed an object with `multiple: true` | ||
|
||
for a given field in Formik state. This is to avoid needing to manually wire up inputs. | ||
|
||
### `FieldMetaProps` | ||
|
||
An object that contains relevant computed metadata about a field. More specifically, | ||
|
||
- `error?: string` - The field's error message (plucked out of `errors`) | ||
- `initialError?: string` - The field's initial error if the field is present in `initialErrors` (plucked out of `initialErrors`) | ||
- `initialTouched: boolean` - The field's initial value if the field is present in `initialTouched` (plucked out of `initialTouched`) | ||
- `initialValue?: any` - The field's initial value if the field is given a value in `initialValues` (plucked out of `initialValues`) | ||
- `touched: boolean` - Whether the field has been visited (plucked out of `touched`) | ||
- `value: any` - The field's value (plucked out of `values`) |
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 |
---|---|---|
@@ -1,7 +1,24 @@ | ||
{ | ||
"docs": { | ||
"Getting Started": ["overview", "tutorial", "resources"], | ||
"Guides": ["guides/validation", "guides/arrays", "guides/typescript", "guides/react-native", "guides/form-submission"], | ||
"API Reference": ["api/formik", "api/withFormik", "api/field", "api/usefield", "api/fieldarray", "api/form", "api/errormessage", "api/connect", "api/fastfield"] | ||
"Guides": [ | ||
"guides/validation", | ||
"guides/arrays", | ||
"guides/typescript", | ||
"guides/react-native", | ||
"guides/form-submission" | ||
], | ||
"API Reference": [ | ||
"api/formik", | ||
"api/withFormik", | ||
"api/field", | ||
"api/usefield", | ||
"api/usefastfield", | ||
"api/fieldarray", | ||
"api/form", | ||
"api/errormessage", | ||
"api/connect", | ||
"api/fastfield" | ||
] | ||
} | ||
} | ||
} |
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.
@sparkboom missing helpers right?