-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathComponent.js
More file actions
82 lines (68 loc) · 1.89 KB
/
Component.js
File metadata and controls
82 lines (68 loc) · 1.89 KB
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
/**
* @copyright 2015-present, Prometheus Research, LLC
*/
import React, {PropTypes} from 'react';
import invariant from 'invariant';
import keyPath from './keyPath';
export const ContextTypes = {
formValue: PropTypes.object
};
let selectPropType = PropTypes.oneOfType([
PropTypes.array,
PropTypes.string,
PropTypes.number,
PropTypes.bool
]);
/**
* Base class for form components.
*
* It exposes form value via `this.formValue` which is provided either via
* `this.props.formValue` or via context.
*/
export default class Component extends React.Component {
static propTypes = {
/**
* Form value passed as a prop.
*/
formValue: PropTypes.object,
/**
* Selector for form value.
*
* Used to select a part from a form value passed via context.
*/
select: selectPropType,
/**
* Same as `select`.
*
* Deprecated.
*/
selectFormValue: selectPropType
};
static contextTypes = ContextTypes;
static childContextTypes = ContextTypes;
getChildContext() {
return {formValue: this.formValue};
}
get formValue() {
invariant(
this.props.formValue || this.context.formValue,
'A form component <%s /> should receive form value via props ' +
'or be used as a part of element hierarchy which ' +
'provides form value via context.',
this.constructor.displayName || this.constructor.name
);
if (this.props.formValue) {
return this.props.formValue;
}
let formValue = this.context.formValue;
let select = this.props.select || this.props.selectFormValue;
// We check for select !== true to keep compatability we eariler
// versions of React Forms where we needed to rebuild element tree to
// propagate values to form.
if (select && select !== true) {
select = keyPath(select);
formValue = formValue.select(select);
}
return formValue;
}
}