Skip to content

Commit 12d86f3

Browse files
avocadowastakenerikras
authored andcommitted
feat(useFormState): Implement hook for form subscription. (#15)
* feat(useForm): Create form lazily. * fix(useForm): Use `getForm` in `useEffect`. * feat(useFormState): Implement hook for form subscription. * fix(useFormState): Resubscribe only on subscription props change. * style(useFormState): Make `subscriptionToInputs` and arrow function.
1 parent 3225321 commit 12d86f3

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

src/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ interface FormConfig extends Config {
4141

4242
declare module 'react-final-form-hooks' {
4343
export function useForm(config: FormConfig): FormRenderProps
44+
export function useFormState(
45+
form: FormApi,
46+
subscription?: FormSubscription
47+
): FormRenderProps
4448

4549
export function useField(
4650
name: string,

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { default as useField } from './useField'
22
export { default as useForm } from './useForm'
3+
export { default as useFormState } from './useFormState'

src/useForm.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
import { useState, useEffect, useRef, useCallback } from 'react'
2-
import { createForm, formSubscriptionItems } from 'final-form'
3-
4-
export const all = formSubscriptionItems.reduce((result, key) => {
5-
result[key] = true
6-
return result
7-
}, {})
1+
import { useCallback, useRef } from 'react'
2+
import { createForm } from 'final-form'
3+
import useFormState from './useFormState'
84

95
const useForm = ({ subscription, ...config }) => {
106
const form = useRef()
@@ -16,10 +12,7 @@ const useForm = ({ subscription, ...config }) => {
1612

1713
return form.current
1814
}
19-
const [state, setState] = useState({})
20-
useEffect(() => getForm().subscribe(setState, subscription || all), [
21-
subscription
22-
])
15+
const state = useFormState(getForm(), subscription)
2316
const handleSubmit = useCallback(event => {
2417
if (event) {
2518
if (typeof event.preventDefault === 'function') {

src/useFormState.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { useEffect, useState } from 'react'
2+
import { formSubscriptionItems } from 'final-form'
3+
4+
export const all = formSubscriptionItems.reduce((result, key) => {
5+
result[key] = true
6+
return result
7+
}, {})
8+
9+
/**
10+
* Converts { active: true, data: false, ... } to `[true, false, false, ...]`.
11+
*/
12+
const subscriptionToInputs = subscription =>
13+
formSubscriptionItems.map(key => Boolean(subscription[key]))
14+
15+
const useFormState = (form, subscription = all) => {
16+
const [state, setState] = useState(() => form.getState())
17+
18+
useEffect(() => form.subscribe(setState, subscription), [
19+
form,
20+
...subscriptionToInputs(subscription)
21+
])
22+
23+
return state
24+
}
25+
26+
export default useFormState

0 commit comments

Comments
 (0)