Skip to content

Commit

Permalink
feat #65 - Fix infinite calls with react hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Aug 25, 2020
1 parent 56bf568 commit 31660d4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/components/Form/FieldContext.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createContext } from 'react'

interface FieldContextProps {
export interface FieldContextProps {
initialValues: Record<string, any>
loading: boolean
onSubmit: (data: any) => void
}
Expand Down
10 changes: 7 additions & 3 deletions src/components/Form/FormInput/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import FieldContext from '../FieldContext'
import FieldLabel from '../FieldLabel'
import { Controller, useFormContext, ValidationRules } from 'react-hook-form'
import FieldContext, { FieldContextProps } from '../FieldContext'
import Input, { InputProps } from '../../Input'
import React, { FC, useContext } from 'react'

Expand All @@ -21,12 +21,16 @@ const FormInput: FC<FormInputProps> = ({
...rest
}: FormInputProps) => {
const { control, errors } = useFormContext()
const { loading } = useContext(FieldContext)
const { initialValues, loading } = useContext<FieldContextProps>(
FieldContext
)

if (required) {
rules.required = true
}

const defaultValue = (initialValues[name] as string) || ''

return (
<div>
{label && (
Expand All @@ -38,7 +42,7 @@ const FormInput: FC<FormInputProps> = ({
)}
<Controller
control={control}
defaultValue=''
defaultValue={defaultValue}
name={name}
render={({ onChange, value }) => (
<Input
Expand Down
14 changes: 6 additions & 8 deletions src/components/Form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import FieldContext from './FieldContext'
import FormButton, { FormButtonProps } from './FormButton'
import FormInput, { FormInputProps } from './FormInput'
import { FormProvider, useForm } from 'react-hook-form'
import React, { FC, ReactNode, useEffect } from 'react'
import React, { FC, ReactNode } from 'react'

const useStyles = createUseStyles({
container: {
Expand All @@ -15,7 +15,7 @@ const useStyles = createUseStyles({

export interface FormProps {
children: ReactNode
initialValues?: object
initialValues?: Record<string, any>
loading?: boolean
onSubmit: (data: any) => void
}
Expand All @@ -34,16 +34,14 @@ const Form: FC<FormProps> & FormSubComponents = ({
const classes = useStyles()
const methods = useForm()

const { handleSubmit, reset } = methods

useEffect(() => {
reset(initialValues)
}, [initialValues, reset])
const { handleSubmit } = methods

return (
<FormProvider {...methods}>
<form onSubmit={handleSubmit(onSubmit)}>
<FieldContext.Provider value={{ loading, onSubmit }}>
<FieldContext.Provider
value={{ initialValues, loading, onSubmit }}
>
<div className={classes.container}>{children}</div>
</FieldContext.Provider>
</form>
Expand Down

0 comments on commit 31660d4

Please sign in to comment.