forked from redux-form/redux-form
/
createFieldArray.js
138 lines (118 loc) · 3.61 KB
/
createFieldArray.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// @flow
import React, { Component, createElement } from 'react'
import { polyfill } from 'react-lifecycles-compat'
import PropTypes from 'prop-types'
import invariant from 'invariant'
import createConnectedFieldArray from './ConnectedFieldArray'
import prefixName from './util/prefixName'
import type {
ConnectedComponent,
Structure,
ReactContext
} from './types.js.flow'
import type { InstanceApi as ConnectedFieldArrayInstanceApi } from './ConnectedFieldArray.types'
import type { Props } from './FieldArrayProps.types'
import validateComponentProp from './util/validateComponentProp'
const toArray = (value: any): Array<*> =>
Array.isArray(value) ? value : [value]
const wrapError = (fn: ?Function, key: string): ?Function =>
fn &&
((...args) => {
const validators = toArray(fn)
for (let i = 0; i < validators.length; i++) {
const result = validators[i](...args)
if (result) {
return { [key]: result }
}
}
})
const createFieldArray = (structure: Structure<*, *>) => {
const ConnectedFieldArray = createConnectedFieldArray(structure)
class FieldArray extends Component<Props> {
context: ReactContext
name: string
ref: ?ConnectedComponent<ConnectedFieldArrayInstanceApi>
constructor(props: Props, context: ReactContext) {
super(props, context)
if (!context._reduxForm) {
throw new Error(
'FieldArray must be inside a component decorated with reduxForm()'
)
}
}
componentDidMount() {
this.context._reduxForm.register(
this.name,
'FieldArray',
() => wrapError(this.props.validate, '_error'),
() => wrapError(this.props.warn, '_warning')
)
}
componentWillReceiveProps(nextProps: Props, nextContext: any) {
const oldName = prefixName(this.context, this.props.name)
const newName = prefixName(nextContext, nextProps.name)
if (oldName !== newName) {
// unregister old name
this.context._reduxForm.unregister(oldName)
// register new name
this.context._reduxForm.register(newName, 'FieldArray')
}
}
componentWillUnmount() {
this.context._reduxForm.unregister(this.name)
}
saveRef = (ref: ?React.Component<*, *>) => {
this.ref = ((ref: any): ?ConnectedComponent<
ConnectedFieldArrayInstanceApi
>)
}
get name(): string {
return prefixName(this.context, this.props.name)
}
get dirty(): boolean {
return !this.ref || this.ref.dirty
}
get pristine(): boolean {
return !!(this.ref && this.ref.pristine)
}
get value(): ?(any[]) {
return this.ref ? this.ref.value : undefined
}
getRenderedComponent() {
invariant(
this.props.withRef,
'If you want to access getRenderedComponent(), ' +
'you must specify a withRef prop to FieldArray'
)
return this.ref && this.ref.getRenderedComponent()
}
render() {
return createElement(ConnectedFieldArray, {
...this.props,
name: this.name,
_reduxForm: this.context._reduxForm,
ref: this.saveRef
})
}
}
FieldArray.propTypes = {
name: PropTypes.string.isRequired,
component: validateComponentProp,
props: PropTypes.object,
validate: PropTypes.oneOfType([
PropTypes.func,
PropTypes.arrayOf(PropTypes.func)
]),
warn: PropTypes.oneOfType([
PropTypes.func,
PropTypes.arrayOf(PropTypes.func)
]),
withRef: PropTypes.bool
}
FieldArray.contextTypes = {
_reduxForm: PropTypes.object
}
polyfill(FieldArray)
return FieldArray
}
export default createFieldArray