Skip to content

Commit

Permalink
Fixed bug related to using state around the Form's component
Browse files Browse the repository at this point in the history
  • Loading branch information
florent-tedesco-agrosup committed Mar 29, 2022
1 parent 16a0486 commit 1bf447b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-hook-form-material-ui",
"version": "1.3.1",
"version": "1.3.2",
"description": "This package is a simple bridge between react-hook-form and material-ui",
"author": "BigChicChicken",
"license": "MIT",
Expand Down
32 changes: 23 additions & 9 deletions src/components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import withForm from '../wrappers/withForm'

class Form extends Component {
render() {
const { onSubmit, form, children } = this.props
const { form, onSubmit, children } = this.props

return (
<FormProvider {...form}>
Expand All @@ -15,12 +15,28 @@ class Form extends Component {
}
}

const FormComponent = React.forwardRef(({ parameters, ...props }, ref) =>
React.createElement(withForm(Form, parameters), {
ref,
...props
})
)
const FormWrapper = class extends Component {
constructor(props) {
super(props)

this.Component = withForm(props.parameters)(Form)
}

render() {
const { parameters, innerRef, ...props } = this.props

return React.createElement(this.Component, {
ref: innerRef,
...props
})
}
}

const FormComponent = React.forwardRef((props, ref) => (
<FormWrapper innerRef={ref} {...props} />
))

FormComponent.displayName = 'FormWrapper'

FormComponent.propTypes = {
onSubmit: PropTypes.func,
Expand All @@ -32,6 +48,4 @@ FormComponent.defaultProps = {
parameters: {}
}

FormComponent.displayName = 'Form'

export default FormComponent
4 changes: 1 addition & 3 deletions src/wrappers/withForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import { useForm } from 'react-hook-form'
import hoistNonReactStatics from 'hoist-non-react-statics'

function withForm(Component, parameters = {}) {
export default (parameters) => (Component) => {
const componentName = Component.displayName || Component.name || 'Component'

const render = (props, ref) => {
Expand All @@ -16,5 +16,3 @@ function withForm(Component, parameters = {}) {

return hoistNonReactStatics(WithForm, Component)
}

export default withForm

0 comments on commit 1bf447b

Please sign in to comment.