Skip to content

Commit

Permalink
Accept PropTypes.node for component props (#4041)
Browse files Browse the repository at this point in the history
  • Loading branch information
cherewaty authored and erikras committed May 31, 2018
1 parent ca02205 commit 785edf8
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 33 deletions.
12 changes: 9 additions & 3 deletions src/ConnectedField.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,20 @@ const createConnectedField = (structure: Structure<*, *>) => {
}

ConnectedField.propTypes = {
component: PropTypes.oneOfType([PropTypes.func, PropTypes.string])
.isRequired,
component: PropTypes.oneOfType([
PropTypes.func,
PropTypes.string,
PropTypes.node
]).isRequired,
props: PropTypes.object
}

const connector = connect(
(state, ownProps) => {
const { name, _reduxForm: { initialValues, getFormState } } = ownProps
const {
name,
_reduxForm: { initialValues, getFormState }
} = ownProps
const formState = getFormState(state)
const initialState = getIn(formState, `initial.${name}`)
const initial =
Expand Down
12 changes: 9 additions & 3 deletions src/ConnectedFieldArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,11 @@ const createConnectedFieldArray = (structure: Structure<*, *>) => {
}

ConnectedFieldArray.propTypes = {
component: PropTypes.oneOfType([PropTypes.func, PropTypes.string])
.isRequired,
component: PropTypes.oneOfType([
PropTypes.func,
PropTypes.string,
PropTypes.node
]).isRequired,
props: PropTypes.object,
rerenderOnEveryChange: PropTypes.bool
}
Expand All @@ -138,7 +141,10 @@ const createConnectedFieldArray = (structure: Structure<*, *>) => {

const connector = connect(
(state, ownProps) => {
const { name, _reduxForm: { initialValues, getFormState } } = ownProps
const {
name,
_reduxForm: { initialValues, getFormState }
} = ownProps
const formState = getFormState(state)
const initial =
getIn(formState, `initial.${name}`) ||
Expand Down
49 changes: 28 additions & 21 deletions src/ConnectedFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,25 @@ const createConnectedFields = (structure: Structure<*, *>) => {
render() {
const { component, withRef, _fields, _reduxForm, ...rest } = this.props
const { sectionPrefix, form } = _reduxForm
const { custom, ...props } = Object.keys(
_fields
).reduce((accumulator, name) => {
const connectedProps = _fields[name]
const { custom, ...fieldProps } = createFieldProps(structure, name, {
...connectedProps,
...rest,
form,
onBlur: this.onBlurFns[name],
onChange: this.onChangeFns[name],
onFocus: this.onFocusFns[name]
})
accumulator.custom = custom
const fieldName = sectionPrefix
? name.replace(`${sectionPrefix}.`, '')
: name
return plain.setIn(accumulator, fieldName, fieldProps)
}, {})
const { custom, ...props } = Object.keys(_fields).reduce(
(accumulator, name) => {
const connectedProps = _fields[name]
const { custom, ...fieldProps } = createFieldProps(structure, name, {
...connectedProps,
...rest,
form,
onBlur: this.onBlurFns[name],
onChange: this.onChangeFns[name],
onFocus: this.onFocusFns[name]
})
accumulator.custom = custom
const fieldName = sectionPrefix
? name.replace(`${sectionPrefix}.`, '')
: name
return plain.setIn(accumulator, fieldName, fieldProps)
},
{}
)
if (withRef) {
props.ref = 'renderedComponent'
}
Expand All @@ -152,15 +153,21 @@ const createConnectedFields = (structure: Structure<*, *>) => {
}

ConnectedFields.propTypes = {
component: PropTypes.oneOfType([PropTypes.func, PropTypes.string])
.isRequired,
component: PropTypes.oneOfType([
PropTypes.func,
PropTypes.string,
PropTypes.node
]).isRequired,
_fields: PropTypes.object.isRequired,
props: PropTypes.object
}

const connector = connect(
(state, ownProps) => {
const { names, _reduxForm: { initialValues, getFormState } } = ownProps
const {
names,
_reduxForm: { initialValues, getFormState }
} = ownProps
const formState = getFormState(state)
return {
_fields: names.reduce((accumulator, name) => {
Expand Down
7 changes: 5 additions & 2 deletions src/createField.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,11 @@ const createField = (structure: Structure<*, *>) => {

Field.propTypes = {
name: PropTypes.string.isRequired,
component: PropTypes.oneOfType([PropTypes.func, PropTypes.string])
.isRequired,
component: PropTypes.oneOfType([
PropTypes.func,
PropTypes.string,
PropTypes.node
]).isRequired,
format: PropTypes.func,
normalize: PropTypes.func,
onBlur: PropTypes.func,
Expand Down
6 changes: 5 additions & 1 deletion src/createFieldArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ const createFieldArray = (structure: Structure<*, *>) => {

FieldArray.propTypes = {
name: PropTypes.string.isRequired,
component: PropTypes.func.isRequired,
component: PropTypes.oneOfType([
PropTypes.func,
PropTypes.string,
PropTypes.node
]).isRequired,
props: PropTypes.object,
validate: PropTypes.oneOfType([
PropTypes.func,
Expand Down
11 changes: 8 additions & 3 deletions src/createFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ const createFields = (structure: Structure<*, *>) => {
throw error
}
const { context } = this
const { _reduxForm: { register } } = context
const {
_reduxForm: { register }
} = context
this.names.forEach(name => register(name, 'Field'))
}

Expand Down Expand Up @@ -108,8 +110,11 @@ const createFields = (structure: Structure<*, *>) => {

Fields.propTypes = {
names: (props, propName) => validateNameProp(props[propName]),
component: PropTypes.oneOfType([PropTypes.func, PropTypes.string])
.isRequired,
component: PropTypes.oneOfType([
PropTypes.func,
PropTypes.string,
PropTypes.node
]).isRequired,
format: PropTypes.func,
parse: PropTypes.func,
props: PropTypes.object,
Expand Down

0 comments on commit 785edf8

Please sign in to comment.