Skip to content

Commit

Permalink
Improved PropTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
florent-tedesco-agrosup committed Mar 28, 2022
1 parent dc97933 commit 16a0486
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 170 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.0",
"version": "1.3.1",
"description": "This package is a simple bridge between react-hook-form and material-ui",
"author": "BigChicChicken",
"license": "MIT",
Expand Down
29 changes: 15 additions & 14 deletions src/components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,7 @@ import PropTypes from 'prop-types'
import { FormProvider } from 'react-hook-form'
import withForm from '../wrappers/withForm'

export const propTypes = {
onSubmit: PropTypes.func,
parameters: PropTypes.object
}

export const defaultProps = {
onSubmit: () => {},
parameters: {}
}

class Form extends Component {
static propTypes = propTypes
static defaultProps = defaultProps

render() {
const { onSubmit, form, children } = this.props

Expand All @@ -28,9 +15,23 @@ class Form extends Component {
}
}

export default React.forwardRef(({ parameters, ...props }, ref) =>
const FormComponent = React.forwardRef(({ parameters, ...props }, ref) =>
React.createElement(withForm(Form, parameters), {
ref,
...props
})
)

FormComponent.propTypes = {
onSubmit: PropTypes.func,
parameters: PropTypes.object
}

FormComponent.defaultProps = {
onSubmit: () => {},
parameters: {}
}

FormComponent.displayName = 'Form'

export default FormComponent
31 changes: 15 additions & 16 deletions src/components/fields/Checkbox/Checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,7 @@ import { AbstractDefaultProps, AbstractPropTypes } from '../AbstractFieldProps'
import PropTypes from 'prop-types'
import { Controller } from 'react-hook-form'

export const propTypes = {
...AbstractPropTypes,
formControlProps: PropTypes.object,
checkboxProps: PropTypes.object
}

export const defaultProps = {
...AbstractDefaultProps,
formControlProps: {},
checkboxProps: {}
}

class Checkbox extends Component {
static propTypes = propTypes
static defaultProps = defaultProps

render() {
const {
formControlProps,
Expand Down Expand Up @@ -62,4 +47,18 @@ class Checkbox extends Component {
}
}

export default withFormContext(Checkbox)
const CheckboxComponent = withFormContext(Checkbox)

CheckboxComponent.propTypes = {
...AbstractPropTypes,
formControlProps: PropTypes.object,
checkboxProps: PropTypes.object
}

CheckboxComponent.defaultProps = {
...AbstractDefaultProps,
formControlProps: {},
checkboxProps: {}
}

export default CheckboxComponent
18 changes: 9 additions & 9 deletions src/components/fields/Collection/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@ import React, { Component } from 'react'
import withFieldArray from '../../../wrappers/withFieldArray'
import PropTypes from 'prop-types'

export const propTypes = {
name: PropTypes.string.isRequired,
fieldsRender: PropTypes.func.isRequired,
buttonsRender: PropTypes.func.isRequired
}

class Collection extends Component {
static propTypes = propTypes

render() {
const { fieldsRender, buttonsRender, fieldArray } = this.props

Expand All @@ -23,4 +15,12 @@ class Collection extends Component {
}
}

export default withFieldArray(Collection)
const CollectionComponent = withFieldArray(Collection)

CollectionComponent.propTypes = {
name: PropTypes.string.isRequired,
fieldsRender: PropTypes.func.isRequired,
buttonsRender: PropTypes.func.isRequired
}

export default CollectionComponent
35 changes: 17 additions & 18 deletions src/components/fields/RadioGroup/RadioGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,7 @@ import { AbstractDefaultProps, AbstractPropTypes } from '../AbstractFieldProps'
import PropTypes from 'prop-types'
import { Controller } from 'react-hook-form'

export const propTypes = {
...AbstractPropTypes,
formControlProps: PropTypes.object,
radioGroupProps: PropTypes.object,
options: PropTypes.array
}

export const defaultProps = {
...AbstractDefaultProps,
formControlProps: {},
radioGroupProps: {},
options: []
}

class RadioGroup extends Component {
static propTypes = propTypes
static defaultProps = defaultProps

render() {
const {
formControlProps,
Expand Down Expand Up @@ -70,4 +53,20 @@ class RadioGroup extends Component {
}
}

export default withFormContext(RadioGroup)
const RadioGroupComponent = withFormContext(RadioGroup)

RadioGroupComponent.propTypes = {
...AbstractPropTypes,
formControlProps: PropTypes.object,
radioGroupProps: PropTypes.object,
options: PropTypes.array
}

RadioGroupComponent.defaultProps = {
...AbstractDefaultProps,
formControlProps: {},
radioGroupProps: {},
options: []
}

export default RadioGroupComponent
31 changes: 15 additions & 16 deletions src/components/fields/Select/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,7 @@ import { AbstractDefaultProps, AbstractPropTypes } from '../AbstractFieldProps'
import PropTypes from 'prop-types'
import { Controller } from 'react-hook-form'

export const propTypes = {
...AbstractPropTypes,
formControlProps: PropTypes.object,
selectProps: PropTypes.object
}

export const defaultProps = {
...AbstractDefaultProps,
formControlProps: {},
selectProps: {}
}

class Select extends Component {
static propTypes = propTypes
static defaultProps = defaultProps

render() {
const {
formControlProps,
Expand Down Expand Up @@ -62,4 +47,18 @@ class Select extends Component {
}
}

export default withFormContext(Select)
const SelectComponent = withFormContext(Select)

SelectComponent.propTypes = {
...AbstractPropTypes,
formControlProps: PropTypes.object,
selectProps: PropTypes.object
}

SelectComponent.defaultProps = {
...AbstractDefaultProps,
formControlProps: {},
selectProps: {}
}

export default SelectComponent
31 changes: 15 additions & 16 deletions src/components/fields/Slider/Slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,7 @@ import { AbstractDefaultProps, AbstractPropTypes } from '../AbstractFieldProps'
import PropTypes from 'prop-types'
import { Controller } from 'react-hook-form'

export const propTypes = {
...AbstractPropTypes,
formControlProps: PropTypes.object,
sliderProps: PropTypes.object
}

export const defaultProps = {
...AbstractDefaultProps,
formControlProps: {},
sliderProps: {}
}

class Slider extends Component {
static propTypes = propTypes
static defaultProps = defaultProps

render() {
const {
formControlProps,
Expand Down Expand Up @@ -58,4 +43,18 @@ class Slider extends Component {
}
}

export default withFormContext(Slider)
const SliderComponent = withFormContext(Slider)

SliderComponent.propTypes = {
...AbstractPropTypes,
formControlProps: PropTypes.object,
sliderProps: PropTypes.object
}

SliderComponent.defaultProps = {
...AbstractDefaultProps,
formControlProps: {},
sliderProps: {}
}

export default SliderComponent
31 changes: 15 additions & 16 deletions src/components/fields/Switch/Switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,7 @@ import { AbstractDefaultProps, AbstractPropTypes } from '../AbstractFieldProps'
import PropTypes from 'prop-types'
import { Controller } from 'react-hook-form'

export const propTypes = {
...AbstractPropTypes,
formControlProps: PropTypes.object,
checkboxProps: PropTypes.object
}

export const defaultProps = {
...AbstractDefaultProps,
formControlProps: {},
switchProps: {}
}

class Switch extends Component {
static propTypes = propTypes
static defaultProps = defaultProps

render() {
const {
formControlProps,
Expand Down Expand Up @@ -62,4 +47,18 @@ class Switch extends Component {
}
}

export default withFormContext(Switch)
const SwitchComponent = withFormContext(Switch)

SwitchComponent.propTypes = {
...AbstractPropTypes,
formControlProps: PropTypes.object,
checkboxProps: PropTypes.object
}

SwitchComponent.defaultProps = {
...AbstractDefaultProps,
formControlProps: {},
switchProps: {}
}

export default SwitchComponent
28 changes: 12 additions & 16 deletions src/components/fields/TextField/EmailTextField.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
import React, { Component } from 'react'
import TextField, {
propTypes as TextFieldPropTypes,
defaultProps as TextFieldDefaultProps
} from './TextField'
import TextField from './TextField'
import { isEmail } from '../../../validators/validators'

export const propTypes = {
...TextFieldPropTypes
}

export const defaultProps = {
...TextFieldDefaultProps
}

class EmailTextField extends Component {
static propTypes = propTypes
static defaultProps = defaultProps

render() {
const { RegisterOptions } = this.props
RegisterOptions.validate = {
Expand All @@ -28,4 +14,14 @@ class EmailTextField extends Component {
}
}

export default EmailTextField
const EmailTextFieldComponent = EmailTextField

EmailTextFieldComponent.propTypes = {
...TextField.propTypes
}

EmailTextFieldComponent.defaultProps = {
...TextField.defaultProps
}

export default EmailTextFieldComponent
32 changes: 14 additions & 18 deletions src/components/fields/TextField/PasswordTextField.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import React, { Component } from 'react'
import TextField, {
propTypes as TextFieldPropTypes,
defaultProps as TextFieldDefaultProps
} from './TextField'
import TextField from './TextField'
import {
hasDigit,
hasLowercase,
Expand All @@ -11,20 +8,7 @@ import {
} from '../../../validators/validators'
import PropTypes from 'prop-types'

export const propTypes = {
...TextFieldPropTypes,
passwordDifficulty: PropTypes.oneOf(['complex', 'basic', 'simplest', 'null'])
.isRequired
}

export const defaultProps = {
...TextFieldDefaultProps
}

class PasswordTextField extends Component {
static propTypes = propTypes
static defaultProps = defaultProps

render() {
const { textFieldProps, RegisterOptions, passwordDifficulty } = this.props

Expand Down Expand Up @@ -63,4 +47,16 @@ class PasswordTextField extends Component {
}
}

export default PasswordTextField
const PasswordTextFieldComponent = PasswordTextField

PasswordTextFieldComponent.propTypes = {
...TextField.propTypes,
passwordDifficulty: PropTypes.oneOf(['complex', 'basic', 'simplest', 'null'])
.isRequired
}

PasswordTextFieldComponent.defaultProps = {
...TextField.defaultProps
}

export default PasswordTextFieldComponent
Loading

0 comments on commit 16a0486

Please sign in to comment.