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

[v2] Hooks #1063

Merged
merged 111 commits into from May 29, 2019
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
111 commits
Select commit Hold shift + click to select a range
1635d7d
Patch @types/react for 16.7
jaredpalmer Oct 30, 2018
4dac2ce
Scaffold hooks rewrite
jaredpalmer Oct 30, 2018
42be908
Properly rename FormikContext type to FormikCtx
jaredpalmer Oct 30, 2018
6c670ad
Bump deps
jaredpalmer Nov 2, 2018
deee988
Tweak useFormik signature
jaredpalmer Nov 2, 2018
adf97a0
Add useFormikContext, all methods except validation, submit
jaredpalmer Nov 2, 2018
7adbfbd
Add validation and submission
jaredpalmer Nov 2, 2018
c3cc916
Add Form
jaredpalmer Nov 2, 2018
a609f20
Add comment about deferredUpdates
jaredpalmer Nov 2, 2018
a4d139b
It works!
jaredpalmer Nov 2, 2018
10b49c6
Add todo on validate on change
jaredpalmer Nov 2, 2018
5931c03
Merge branch 'master' into hooks
jaredpalmer Nov 12, 2018
b1ddd27
Update tests for rtl
jaredpalmer Nov 12, 2018
a02e311
Update field tests
jaredpalmer Nov 12, 2018
ccb7d3f
Merge branch 'master' into hooks
jaredpalmer Nov 12, 2018
5e82c6b
Fix Formik tests
jaredpalmer Nov 12, 2018
6b1080d
Update to latest hooks alpha
jaredpalmer Nov 13, 2018
0980bb5
Comment out should validate
jaredpalmer Dec 4, 2018
beb9817
Upgrade to TS 3.2.1
jaredpalmer Dec 4, 2018
ea7d40a
Use latest @types/react and @types/react-dom
jaredpalmer Dec 4, 2018
5a8e912
[v2] Add useField (#1159)
jaredpalmer Dec 5, 2018
f902ec2
v2.0.0-canary.5a8e912
jaredpalmer Dec 5, 2018
8870ab0
Update react peerDep ^16.7.0-alpha
jaredpalmer Dec 6, 2018
9e8e9aa
Merge branch 'master' into hooks
jaredpalmer Dec 6, 2018
a92ff70
Update deps
jaredpalmer Dec 6, 2018
0a9bb2d
Pass validateOnBlur and validateOnChange through context
jaredpalmer Dec 6, 2018
74da625
Remove Field-level validation
jaredpalmer Dec 6, 2018
ce6b860
Debounce change validation
jaredpalmer Dec 6, 2018
669b73b
Remove isValidatin+ didMount, make validation cancellable in useEffect
jaredpalmer Dec 8, 2018
0be8e37
Add async onSubmit + automatically call setSubmitting(false) (#1183)
jaredpalmer Dec 13, 2018
13b4bcc
Merge branch 'master' into hooks
jaredpalmer Dec 13, 2018
effcf9a
Add some docs on useField
jaredpalmer Dec 14, 2018
4cb077e
v2.0.0-canary.effcf9a
jaredpalmer Dec 14, 2018
3130d18
Only update errors if they are not deeply equal
jaredpalmer Dec 14, 2018
d64d9f8
Update snap
jaredpalmer Dec 14, 2018
09a2bd3
v2.0.0-canary.effcf9b
jaredpalmer Dec 14, 2018
f5ba4e3
Remove debounce validation ms
jaredpalmer Dec 18, 2018
1645bb4
Rename FormikCtx back to FormikContext
jaredpalmer Dec 18, 2018
e387633
Remove unused deps
jaredpalmer Dec 18, 2018
5080c91
Merge branch 'master' into hooks
jaredpalmer Dec 18, 2018
6553174
Merge branch 'master' into hooks
jaredpalmer Dec 18, 2018
f99cbd5
Bump hoist-non-react-statics to 3.x for forwardRef support
jaredpalmer Dec 18, 2018
e0b023d
Comment out Field validation tests
jaredpalmer Dec 18, 2018
0a12805
Catch validate
jaredpalmer Dec 26, 2018
0b6f1c4
Add warning to Field
jaredpalmer Dec 26, 2018
bb6a46a
Remove initialValues hook
jaredpalmer Dec 26, 2018
c4c803f
Add a nice warning if useField or Field lives outside of Formik
jaredpalmer Dec 26, 2018
757452a
Add back Field-level validation
jaredpalmer Dec 26, 2018
4776131
Feature/hooks tests passing (#1249)
jackjocross Jan 18, 2019
0a49da3
v2.0.0-gamma.1
jaredpalmer Jan 20, 2019
0c55fc3
Merge branch 'master' into hooks
jaredpalmer Jan 22, 2019
300487d
Check isMounted before dispatching after executeSubmit (#1290)
gustavopch Feb 1, 2019
8c7b7fe
Merge branch 'master' into hooks
jaredpalmer Feb 8, 2019
65eb6bd
Fix docs
jaredpalmer Feb 8, 2019
f5d421f
Fix withFormik TS hoist
jaredpalmer Feb 8, 2019
addc523
Upgrade React, TypeScript, @types/react, and fix type errors
jaredpalmer Feb 21, 2019
0f6f59c
Fix warnings (#1348)
sijad Mar 8, 2019
06d1940
Comment out FastField test for now
jaredpalmer Mar 8, 2019
7eaa69a
Merge branch 'hooks' of github.com:jaredpalmer/formik into hooks
jaredpalmer Mar 8, 2019
dac2544
v2.0.0
jaredpalmer Mar 8, 2019
cae9e57
v2.0.1-alpha.0
jaredpalmer Mar 8, 2019
14ac079
Add deprecation warning to <Field render>
jaredpalmer Mar 8, 2019
97fb40c
Create <Field as> and deprecate <Field component> and <Field render> …
jaredpalmer Apr 2, 2019
967d6f7
Remove prod check before useField warning
jaredpalmer Apr 2, 2019
006d60f
[v2] Update and document field meta prop (#1409)
jaredpalmer Apr 2, 2019
a497d7f
[v2] Revert missing enableReinitialize in useFormik hook (#1399)
tz5514 Apr 2, 2019
3850f2b
v2.0.1-alpha.1
jaredpalmer Apr 2, 2019
56ce6ed
Fixes docs typo (#1412)
jhoch Apr 3, 2019
ddf01d0
Only show deprecation warnings when Field mounts
jaredpalmer Apr 3, 2019
67f26ac
Add tests for Field warnings and deprecations
jaredpalmer Apr 3, 2019
fff33fa
Only show deprecation warnings when Field mounts (#1414)
jaredpalmer Apr 3, 2019
10ee6e9
Merge branch 'hooks' of github.com:jaredpalmer/formik into hooks
jaredpalmer Apr 3, 2019
c5cbb34
v2.0.1-alpha.2
jaredpalmer Apr 3, 2019
afa001f
Fixed error message in Field.tsx (#1434)
nareshbhatia Apr 12, 2019
4332637
Add initialXXXX and fix isValid (#1410)
jaredpalmer Apr 26, 2019
04bf37f
Merge branch 'master' into hooks
jaredpalmer Apr 26, 2019
b838b27
Fix up a rogue search and replace
jaredpalmer Apr 26, 2019
47cfc2f
Fix useField example syntax highlight (#1451)
Fonger Apr 26, 2019
630ea14
Upgrade to 16.9, THANKYOU KENTCDODDS AND SUNIL!!!
jaredpalmer Apr 26, 2019
198ab0e
Merge branch 'master' into hooks
jaredpalmer Apr 26, 2019
bd077e6
Use useCallback everywhere
jaredpalmer Apr 26, 2019
a8f75fd
Fix withFormik
jaredpalmer Apr 26, 2019
0ca5dbd
Revert "Fix withFormik"
jaredpalmer May 16, 2019
60becbe
Revert "Use useCallback everywhere"
jaredpalmer May 16, 2019
46ceb86
Use eslint-typescript from CRA
jaredpalmer May 16, 2019
2a08d5e
Implement useCallback properly
jaredpalmer May 16, 2019
779716d
v2.0.1-alpha.3
jaredpalmer May 16, 2019
157bc69
Update circle node version
jaredpalmer May 16, 2019
0fa816b
Fix version
jaredpalmer May 16, 2019
e34e237
v2.0.1-alpha.3
jaredpalmer May 16, 2019
527fee4
Only warn about deprecations in development
jaredpalmer May 16, 2019
281cbe5
v2.0.1-alpha.4
jaredpalmer May 16, 2019
ead448c
Ensure that warnings only run on mount once!
jaredpalmer May 16, 2019
2987359
Remove component usage from example
jaredpalmer May 16, 2019
c51dfd9
Call stopPropagation() if present on submits (#1515)
jaredpalmer May 16, 2019
c469321
v2.0.1-alpha.5
jaredpalmer May 16, 2019
0872e62
Merge branch 'master' into hooks
jaredpalmer May 16, 2019
902a40a
Move to @palmerhq/tsdx (#1516)
jaredpalmer May 16, 2019
2bc08b7
Merge branch 'master' into hooks
jaredpalmer May 17, 2019
f6cfa09
Bump react types and fix hoist-non-react-statics?
jaredpalmer May 17, 2019
3378b58
Merge branch 'master' into hooks
jaredpalmer May 17, 2019
fa2bc4c
v2.0.1-rc.0
jaredpalmer May 17, 2019
701643e
Upgrade to storybook 5
jaredpalmer May 18, 2019
bfcdba4
Bump to tsdx 0.5.12
jaredpalmer May 28, 2019
2b9a25a
v2.0.1-rc.1
jaredpalmer May 28, 2019
123e392
[v2]: Distinguish between high/low priority validation (#1522)
jaredpalmer May 29, 2019
64ab584
Upgrade to tsdx 0.6.0
jaredpalmer May 29, 2019
e6dca87
v2.0.1-rc.2
jaredpalmer May 29, 2019
cfb7e15
Start spinning up a v2 docs website
jaredpalmer May 29, 2019
5f2cdfb
Prepare for next branch
jaredpalmer May 29, 2019
94f346a
Merge branch 'master' into hooks
jaredpalmer May 29, 2019
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
45 changes: 25 additions & 20 deletions .size-snapshot.json
@@ -1,36 +1,41 @@
{
"./dist/formik.umd.production.js": {
"bundled": 141389,
"minified": 38919,
"gzipped": 11819
},
"./dist/formik.umd.development.js": {
"bundled": 172577,
"minified": 49765,
"gzipped": 14998
"bundled": 123663,
"minified": 31550,
"gzipped": 10196
},
"./dist/formik.cjs.production.js": {
"bundled": 42006,
"minified": 21340,
"gzipped": 5354
"bundled": 35255,
"minified": 16877,
"gzipped": 5039
},
"./dist/formik.cjs.development.js": {
"bundled": 42894,
"minified": 22223,
"gzipped": 5697
"bundled": 35842,
"minified": 17526,
"gzipped": 5278
},
"dist/index.js": {
"bundled": 33614,
"minified": 17803,
"gzipped": 5329
},
"dist/formik.esm.js": {
"bundled": 38805,
"minified": 21612,
"gzipped": 5575,
"bundled": 32128,
"minified": 16540,
"gzipped": 5150,
"treeshaked": {
"rollup": {
"code": 766,
"import_statements": 344
"code": 347,
"import_statements": 313
},
"webpack": {
"code": 3439
"code": 2692
}
}
},
"./dist/formik.umd.development.js": {
"bundled": 125491,
"minified": 32979,
"gzipped": 10840
}
}
79 changes: 79 additions & 0 deletions docs/api/useField.md
@@ -0,0 +1,79 @@
---
id: usefield
title: useField()
custom_edit_url: https://github.com/jaredpalmer/formik/edit/master/docs/api/usefield.md
---

`useField` is a custom React hook that will automagically help you hook up inputs to Formik. You can and should use it build your own custom input primitives.

## Example

```jsx
import React from 'react';
import { useField, Formik } from 'formik';

const MyTextField = ({ label, ...props }) => {
const [field, meta] = useField(props.name);
return (
<>
<label>
{label}
<input {...field} {...props} />
</label>
{meta.touch && 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

## `useField(name: string): [FieldProps, FieldMetaProps]`

A custom React Hook that returns a tuple (2 element array) containing `FieldProps` and `FieldMetaProps`.

### `FieldProps`

An object that contains:

* `onChange`
* `onBlur`
* `value`
* `name`

for a given field in Formik state. This is to avoid needing to manually wire up inputs.

### `FieldMeta`

An object that contains relevant computed metadata about a field. More specifically,

* `touch: boolean` - Whether the field has been visited (plucked out of `touched`)
* `error?: string` - The field's error message (plucked out of `errors`)
* `initialValue: any` - The field's initial value if the field is given a value in `initialValues` (plucked out of `initialValues`)
18 changes: 9 additions & 9 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "formik",
"description": "Forms in React, without tears",
"version": "1.4.0",
"version": "2.0.0-canary.effcf9b",
"license": "MIT",
"author": "Jared Palmer <jared@palmer.net>",
"repository": "jaredpalmer/formik",
Expand All @@ -22,7 +22,7 @@
"dist"
],
"peerDependencies": {
"react": ">=15"
"react": "^16.7.0-alpha.0"
},
"scripts": {
"test": "jest --env=jsdom",
Expand Down Expand Up @@ -53,17 +53,17 @@
},
"optionalDependencies": {},
"resolutions": {
"@types/react": "^16.7.6",
"@types/react-dom": "^16.0.9"
"@types/react": "16.7.17",
"@types/react-dom": "16.0.11"
},
"devDependencies": {
"@babel/core": "^7.1.2",
"@storybook/addon-options": "^3.4.8",
"@storybook/react": "^3.4.0",
"@types/jest": "^22.2.3",
"@types/lodash": "^4.14.119",
"@types/react": "^16.7.6",
"@types/react-dom": "^16.0.9",
"@types/react": "^16.7.17",
"@types/react-dom": "^16.0.11",
"@types/warning": "^3.0.0",
"@types/yup": "^0.24.9",
"all-contributors-cli": "^4.4.0",
Expand All @@ -79,8 +79,8 @@
"lint-staged": "4.0.2",
"prettier": "1.11.1",
"raw-loader": "^0.5.1",
"react": "^16.6.3",
"react-dom": "^16.6.3",
"react": "^16.7.0-alpha.2",
"react-dom": "^16.7.0-alpha.2",
"react-testing-library": "^5.2.3",
"rimraf": "^2.6.2",
"rollup": "^0.66.4",
Expand All @@ -96,7 +96,7 @@
"tsc-watch": "^1.0.21",
"tslint": "^5.11.0",
"tslint-react": "^3.6.0",
"typescript": "^3.1.6",
"typescript": "^3.2.1",
"yup": "0.21.3"
},
"lint-staged": {
Expand Down
10 changes: 4 additions & 6 deletions src/ErrorMessage.tsx
@@ -1,5 +1,5 @@
import * as React from 'react';
import { FormikContext } from './types';
import { FormikCtx } from './types';
import { getIn, isFunction } from './utils';
import { connect } from './connect';

Expand All @@ -12,11 +12,9 @@ export interface ErrorMessageProps {
}

class ErrorMessageImpl extends React.Component<
ErrorMessageProps & { formik: FormikContext<any> }
ErrorMessageProps & { formik: FormikCtx<any> }
> {
shouldComponentUpdate(
props: ErrorMessageProps & { formik: FormikContext<any> }
) {
shouldComponentUpdate(props: ErrorMessageProps & { formik: FormikCtx<any> }) {
if (
getIn(this.props.formik.errors, this.props.name) !==
getIn(props.formik.errors, this.props.name) ||
Expand Down Expand Up @@ -50,5 +48,5 @@ class ErrorMessageImpl extends React.Component<

export const ErrorMessage = connect<
ErrorMessageProps,
ErrorMessageProps & { formik: FormikContext<any> }
ErrorMessageProps & { formik: FormikCtx<any> }
>(ErrorMessageImpl);