Skip to content

Commit

Permalink
fix: fix blur/change behavior again
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 committed Dec 11, 2020
1 parent 8e07dad commit b2b460e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ after it.

Demo CodeSandbox: https://codesandbox.io/s/react-final-form-graceful-field-example-n1onw?file=/src/index.js

# Bundle size note

You might be alarmed that this depends on `lodash`, but it only imports `lodash/get`, so with webpack or rollup tree shaking it will not weigh much.

# Important

**Important**: `react-final-form-graceful-field` requires you to use the [`setFieldData` mutator](https://github.com/final-form/final-form-set-field-data) on your form:
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@
"dependencies": {
"@babel/runtime": "^7.1.5",
"final-form-set-field-data": "^1.0.2",
"lodash": "^4.17.20",
"prop-types": "^15.0.0"
},
"peerDependencies": {
Expand Down
33 changes: 22 additions & 11 deletions src/useGracefulField.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
} from 'final-form'
import { useForm, type FieldRenderProps, useField } from 'react-final-form'
import * as React from 'react'
import get from 'lodash/get'

export type UseGracefulFieldProps = {
afterSubmit?: () => void,
Expand Down Expand Up @@ -66,6 +65,8 @@ export default function useGracefulField(
): FieldRenderProps {
const form = useForm('useField')
const [lastFormValue, setLastFormValue] = React.useState()
const field: FieldRenderProps = useField(name, {
afterSubmit,
allowNull,
Expand All @@ -74,12 +75,14 @@ export default function useGracefulField(
component,
data,
defaultValue,
format: (value, name) =>
raw && (field.meta.active || raw.parseError)
format: (value: any, name: string): any => {
if (value !== lastFormValue) setLastFormValue(value)
return raw && (field.meta.active || raw.parseError)
? raw.rawValue
: format
? format(value, name)
: value,
: value
},
formatOnBlur: false,
initialValue,
isEqual,
Expand Down Expand Up @@ -116,20 +119,28 @@ export default function useGracefulField(
})
const raw = getRaw(field.meta)
const curValue = get(form.getState().values, name)
React.useEffect(
() => {
if (
!field.meta.active &&
raw &&
(!raw.parseError || !Object.is(raw.parsedValue, curValue))
) {
if (field.meta.active) return
if (raw && !raw.parseError) {
form.mutators.setFieldData(name, {
'react-final-form-graceful-field': null,
})
}
},
[field.meta.active, raw, curValue]
[field.meta.active]
)
React.useEffect(
() => {
if (field.meta.active) return
form.mutators.setFieldData(name, {
'react-final-form-graceful-field': null,
})
},
[lastFormValue]
)

return field
}
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6990,11 +6990,6 @@ lodash@^4.17.11, lodash@^4.17.14, lodash@^4.17.19:
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.20.tgz#b44a9b6297bcb698f1c51a3545a2b3b368d59c52"
integrity sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==

lodash@^4.17.20:
version "4.17.20"
resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz#b44a9b6297bcb698f1c51a3545a2b3b368d59c52"
integrity sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==

lodash@^4.17.4, lodash@^4.17.5, lodash@^4.2.1, lodash@^4.3.0:
version "4.17.11"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d"
Expand Down

0 comments on commit b2b460e

Please sign in to comment.