Skip to content

feat(useFormState): Implement hook for form subscription. #15

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

Merged
merged 6 commits into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ interface FormConfig extends Config {

declare module 'react-final-form-hooks' {
export function useForm(config: FormConfig): FormRenderProps
export function useFormState(
form: FormApi,
subscription?: FormSubscription
): FormRenderProps

export function useField(
name: string,
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as useField } from './useField'
export { default as useForm } from './useForm'
export { default as useFormState } from './useFormState'
15 changes: 4 additions & 11 deletions src/useForm.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { useState, useEffect, useRef, useCallback } from 'react'
import { createForm, formSubscriptionItems } from 'final-form'

export const all = formSubscriptionItems.reduce((result, key) => {
result[key] = true
return result
}, {})
import { useCallback, useRef } from 'react'
import { createForm } from 'final-form'
import useFormState from './useFormState'

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

return form.current
}
const [state, setState] = useState({})
useEffect(() => getForm().subscribe(setState, subscription || all), [
subscription
])
const state = useFormState(getForm(), subscription)
const handleSubmit = useCallback(event => {
if (event) {
if (typeof event.preventDefault === 'function') {
Expand Down
26 changes: 26 additions & 0 deletions src/useFormState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useEffect, useState } from 'react'
import { formSubscriptionItems } from 'final-form'

export const all = formSubscriptionItems.reduce((result, key) => {
result[key] = true
return result
}, {})

/**
* Converts { active: true, data: false, ... } to `[true, false, false, ...]`.
*/
const subscriptionToInputs = subscription =>
formSubscriptionItems.map(key => Boolean(subscription[key]))

const useFormState = (form, subscription = all) => {
const [state, setState] = useState(() => form.getState())

useEffect(() => form.subscribe(setState, subscription), [
form,
...subscriptionToInputs(subscription)
])

return state
}

export default useFormState