forked from redux-form/redux-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectedField.js
344 lines (310 loc) · 10 KB
/
ConnectedField.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// @flow
import React, { Component, createElement } from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import createFieldProps from './createFieldProps'
import onChangeValue from './events/onChangeValue'
import { dataKey } from './util/eventConsts'
import plain from './structure/plain'
import isReactNative from './isReactNative'
import type { Structure } from './types.js.flow'
import type { Props } from './ConnectedField.types'
import validateComponentProp from './util/validateComponentProp'
const propsToNotUpdateFor = ['_reduxForm']
const isObject = entity => entity && typeof entity === 'object'
const isFunction = entity => entity && typeof entity === 'function'
const eventPreventDefault = event => {
if (isObject(event) && isFunction(event.preventDefault)) {
event.preventDefault()
}
}
const eventDataTransferGetData = (event, key) => {
if (
isObject(event) &&
isObject(event.dataTransfer) &&
isFunction(event.dataTransfer.getData)
) {
return event.dataTransfer.getData(key)
}
}
const eventDataTransferSetData = (event, key, value) => {
if (
isObject(event) &&
isObject(event.dataTransfer) &&
isFunction(event.dataTransfer.setData)
) {
event.dataTransfer.setData(key, value)
}
}
const createConnectedField = (structure: Structure<*, *>) => {
const { deepEqual, getIn } = structure
const getSyncError = (syncErrors: Object, name: string) => {
const error = plain.getIn(syncErrors, name)
// Because the error for this field might not be at a level in the error structure where
// it can be set directly, it might need to be unwrapped from the _error property
return error && error._error ? error._error : error
}
const getSyncWarning = (syncWarnings: Object, name: string) => {
const warning = getIn(syncWarnings, name)
// Because the warning for this field might not be at a level in the warning structure where
// it can be set directly, it might need to be unwrapped from the _warning property
return warning && warning._warning ? warning._warning : warning
}
class ConnectedField extends Component<Props> {
ref: React.Component<*, *>
shouldComponentUpdate(nextProps: Props) {
const nextPropsKeys = Object.keys(nextProps)
const thisPropsKeys = Object.keys(this.props)
// if we have children, we MUST update in React 16
// https://twitter.com/erikras/status/915866544558788608
return !!(
this.props.children ||
nextProps.children ||
(nextPropsKeys.length !== thisPropsKeys.length ||
nextPropsKeys.some(prop => {
if (~(nextProps.immutableProps || []).indexOf(prop)) {
return this.props[prop] !== nextProps[prop]
}
return (
!~propsToNotUpdateFor.indexOf(prop) &&
!deepEqual(this.props[prop], nextProps[prop])
)
}))
)
}
saveRef = (ref: React.Component<*, *>) => (this.ref = ref)
isPristine = (): boolean => this.props.pristine
getValue = (): any => this.props.value
getRenderedComponent(): React.Component<*, *> {
return this.ref
}
handleChange = (event: any) => {
const {
name,
dispatch,
parse,
normalize,
onChange,
_reduxForm,
value: previousValue
} = this.props
const newValue = onChangeValue(event, { name, parse, normalize })
let defaultPrevented = false
if (onChange) {
// Can't seem to find a way to extend Event in React Native,
// thus I simply avoid adding preventDefault() in a RN environment
// to prevent the following error:
// `One of the sources for assign has an enumerable key on the prototype chain`
// Reference: https://github.com/facebook/react-native/issues/5507
if (!isReactNative) {
onChange(
{
...event,
preventDefault: () => {
defaultPrevented = true
return eventPreventDefault(event)
}
},
newValue,
previousValue,
name
)
} else {
onChange(event, newValue, previousValue, name)
}
}
if (!defaultPrevented) {
// dispatch change action
dispatch(_reduxForm.change(name, newValue))
// call post-change callback
if (_reduxForm.asyncValidate) {
_reduxForm.asyncValidate(name, newValue, 'change')
}
}
}
handleFocus = (event: any) => {
const { name, dispatch, onFocus, _reduxForm } = this.props
let defaultPrevented = false
if (onFocus) {
if (!isReactNative) {
onFocus(
{
...event,
preventDefault: () => {
defaultPrevented = true
return eventPreventDefault(event)
}
},
name
)
} else {
onFocus(event, name)
}
}
if (!defaultPrevented) {
dispatch(_reduxForm.focus(name))
}
}
handleBlur = (event: any) => {
const {
name,
dispatch,
parse,
normalize,
onBlur,
_reduxForm,
_value,
value: previousValue
} = this.props
let newValue = onChangeValue(event, { name, parse, normalize })
// for checkbox and radio, if the value property of checkbox or radio equals
// the value passed by blur event, then fire blur action with previousValue.
if (newValue === _value && _value !== undefined) {
newValue = previousValue
}
let defaultPrevented = false
if (onBlur) {
if (!isReactNative) {
onBlur(
{
...event,
preventDefault: () => {
defaultPrevented = true
return eventPreventDefault(event)
}
},
newValue,
previousValue,
name
)
} else {
onBlur(event, newValue, previousValue, name)
}
}
if (!defaultPrevented) {
// dispatch blur action
dispatch(_reduxForm.blur(name, newValue))
// call post-blur callback
if (_reduxForm.asyncValidate) {
_reduxForm.asyncValidate(name, newValue, 'blur')
}
}
}
handleDragStart = (event: any) => {
const { name, onDragStart, value } = this.props
eventDataTransferSetData(event, dataKey, value == null ? '' : value)
if (onDragStart) {
onDragStart(event, name)
}
}
handleDrop = (event: any) => {
const {
name,
dispatch,
onDrop,
_reduxForm,
value: previousValue
} = this.props
const newValue = eventDataTransferGetData(event, dataKey)
let defaultPrevented = false
if (onDrop) {
onDrop(
{
...event,
preventDefault: () => {
defaultPrevented = true
return eventPreventDefault(event)
}
},
newValue,
previousValue,
name
)
}
if (!defaultPrevented) {
// dispatch change action
dispatch(_reduxForm.change(name, newValue))
eventPreventDefault(event)
}
}
render() {
const {
component,
withRef,
name,
// remove props that are part of redux internals:
_reduxForm, // eslint-disable-line no-unused-vars
normalize, // eslint-disable-line no-unused-vars
onBlur, // eslint-disable-line no-unused-vars
onChange, // eslint-disable-line no-unused-vars
onFocus, // eslint-disable-line no-unused-vars
onDragStart, // eslint-disable-line no-unused-vars
onDrop, // eslint-disable-line no-unused-vars
immutableProps, // eslint-disable-line no-unused-vars
...rest
} = this.props
const { custom, ...props } = createFieldProps(structure, name, {
...rest,
form: _reduxForm.form,
onBlur: this.handleBlur,
onChange: this.handleChange,
onDrop: this.handleDrop,
onDragStart: this.handleDragStart,
onFocus: this.handleFocus
})
if (withRef) {
custom.ref = this.saveRef
}
if (typeof component === 'string') {
const { input, meta } = props // eslint-disable-line no-unused-vars
// flatten input into other props
return createElement(component, { ...input, ...custom })
} else {
return createElement(component, { ...props, ...custom })
}
}
}
ConnectedField.propTypes = {
component: validateComponentProp,
props: PropTypes.object
}
const connector = connect(
(state, ownProps) => {
const {
name,
_reduxForm: { initialValues, getFormState }
} = ownProps
const formState = getFormState(state)
const initialState = getIn(formState, `initial.${name}`)
const initial =
initialState !== undefined
? initialState
: initialValues && getIn(initialValues, name)
const value = getIn(formState, `values.${name}`)
const submitting = getIn(formState, 'submitting')
const syncError = getSyncError(getIn(formState, 'syncErrors'), name)
const syncWarning = getSyncWarning(getIn(formState, 'syncWarnings'), name)
const pristine = deepEqual(value, initial)
return {
asyncError: getIn(formState, `asyncErrors.${name}`),
asyncValidating: getIn(formState, 'asyncValidating') === name,
dirty: !pristine,
pristine,
state: getIn(formState, `fields.${name}`),
submitError: getIn(formState, `submitErrors.${name}`),
submitFailed: getIn(formState, 'submitFailed'),
submitting,
syncError,
syncWarning,
initial,
value,
_value: ownProps.value // save value passed in (for checkboxes)
}
},
undefined,
undefined,
{ withRef: true }
)
return connector(ConnectedField)
}
export default createConnectedField