Skip to content

Commit 90ef7f1

Browse files
authored
Validating flag support (#541)
* Added support for [email protected] `validating` flag * linting
1 parent b6a7917 commit 90ef7f1

File tree

5 files changed

+60
-5
lines changed

5 files changed

+60
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,10 @@ The current value of the field.
705705

706706
[See the 🏁 Final Form docs on `valid`](https://github.com/final-form/final-form#valid-boolean).
707707

708+
#### `meta.validating?: boolean`
709+
710+
[See the 🏁 Final Form docs on `validating`](https://github.com/final-form/final-form#validating-boolean).
711+
708712
#### `meta.visited?: boolean`
709713

710714
[See the 🏁 Final Form docs on `visited`](https://github.com/final-form/final-form#visited-boolean).

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"eslint-plugin-react": "^7.13.0",
6363
"eslint-plugin-react-hooks": "^1.6.0",
6464
"fast-deep-equal": "^2.0.1",
65-
"final-form": "^4.16.0",
65+
"final-form": "^4.16.1",
6666
"flow-bin": "^0.98.1",
6767
"glow": "^1.2.2",
6868
"husky": "^2.4.1",
@@ -89,7 +89,7 @@
8989
"typescript": "^3.5.2"
9090
},
9191
"peerDependencies": {
92-
"final-form": "^4.16.0",
92+
"final-form": "^4.16.1",
9393
"react": "^16.8.0"
9494
},
9595
"lint-staged": {

src/Field.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Form from './ReactFinalForm'
66
import Field from './Field'
77

88
const onSubmitMock = values => {}
9+
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
910

1011
describe('Field', () => {
1112
afterEach(cleanup)
@@ -951,4 +952,53 @@ describe('Field', () => {
951952
expect(onSubmit).not.toHaveBeenCalled()
952953
expect(beforeSubmit).toHaveBeenCalled()
953954
})
955+
956+
it('update validating flag on async field-level validation', async () => {
957+
const { getByTestId } = render(
958+
<Form onSubmit={onSubmitMock}>
959+
{({ handleSubmit }) => (
960+
<form onSubmit={handleSubmit}>
961+
<Field
962+
name="name"
963+
component="input"
964+
validate={async value => {
965+
await sleep(5)
966+
return value === 'erikras' ? 'Username taken' : undefined
967+
}}
968+
data-testid="name"
969+
/>
970+
<Field name="name" subscription={{ validating: true }}>
971+
{({ meta: { validating } }) => (
972+
<div data-testid="validating">
973+
{validating === true ? 'Spinner' : 'Not Validating'}
974+
</div>
975+
)}
976+
</Field>
977+
<button type="submit">Submit</button>
978+
</form>
979+
)}
980+
</Form>
981+
)
982+
expect(getByTestId('validating')).toHaveTextContent('Spinner')
983+
984+
await sleep(6)
985+
986+
expect(getByTestId('name').value).toBe('')
987+
fireEvent.focus(getByTestId('name'))
988+
expect(getByTestId('validating')).toHaveTextContent('Not Validating')
989+
990+
fireEvent.change(getByTestId('name'), { target: { value: 'erik' } })
991+
expect(getByTestId('validating')).toHaveTextContent('Spinner')
992+
993+
await sleep(6)
994+
995+
expect(getByTestId('validating')).toHaveTextContent('Not Validating')
996+
997+
fireEvent.change(getByTestId('name'), { target: { value: 'erikras' } })
998+
expect(getByTestId('validating')).toHaveTextContent('Spinner')
999+
1000+
await sleep(6)
1001+
1002+
expect(getByTestId('validating')).toHaveTextContent('Not Validating')
1003+
})
9541004
})

src/useField.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ function useField<FormValues: FormValuesShape>(
195195
submitting: otherState.submitting,
196196
touched: otherState.touched,
197197
valid: otherState.valid,
198+
validating: otherState.validating,
198199
visited: otherState.visited
199200
}
200201
if (formatOnBlur) {

0 commit comments

Comments
 (0)