Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80517 views
1
/**
2
* Copyright 2013-2015, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule LinkedValueUtils
10
* @typechecks static-only
11
*/
12
13
'use strict';
14
15
var ReactPropTypes = require("./ReactPropTypes");
16
17
var invariant = require("./invariant");
18
19
var hasReadOnlyValue = {
20
'button': true,
21
'checkbox': true,
22
'image': true,
23
'hidden': true,
24
'radio': true,
25
'reset': true,
26
'submit': true
27
};
28
29
function _assertSingleLink(input) {
30
("production" !== process.env.NODE_ENV ? invariant(
31
input.props.checkedLink == null || input.props.valueLink == null,
32
'Cannot provide a checkedLink and a valueLink. If you want to use ' +
33
'checkedLink, you probably don\'t want to use valueLink and vice versa.'
34
) : invariant(input.props.checkedLink == null || input.props.valueLink == null));
35
}
36
function _assertValueLink(input) {
37
_assertSingleLink(input);
38
("production" !== process.env.NODE_ENV ? invariant(
39
input.props.value == null && input.props.onChange == null,
40
'Cannot provide a valueLink and a value or onChange event. If you want ' +
41
'to use value or onChange, you probably don\'t want to use valueLink.'
42
) : invariant(input.props.value == null && input.props.onChange == null));
43
}
44
45
function _assertCheckedLink(input) {
46
_assertSingleLink(input);
47
("production" !== process.env.NODE_ENV ? invariant(
48
input.props.checked == null && input.props.onChange == null,
49
'Cannot provide a checkedLink and a checked property or onChange event. ' +
50
'If you want to use checked or onChange, you probably don\'t want to ' +
51
'use checkedLink'
52
) : invariant(input.props.checked == null && input.props.onChange == null));
53
}
54
55
/**
56
* @param {SyntheticEvent} e change event to handle
57
*/
58
function _handleLinkedValueChange(e) {
59
/*jshint validthis:true */
60
this.props.valueLink.requestChange(e.target.value);
61
}
62
63
/**
64
* @param {SyntheticEvent} e change event to handle
65
*/
66
function _handleLinkedCheckChange(e) {
67
/*jshint validthis:true */
68
this.props.checkedLink.requestChange(e.target.checked);
69
}
70
71
/**
72
* Provide a linked `value` attribute for controlled forms. You should not use
73
* this outside of the ReactDOM controlled form components.
74
*/
75
var LinkedValueUtils = {
76
Mixin: {
77
propTypes: {
78
value: function(props, propName, componentName) {
79
if (!props[propName] ||
80
hasReadOnlyValue[props.type] ||
81
props.onChange ||
82
props.readOnly ||
83
props.disabled) {
84
return null;
85
}
86
return new Error(
87
'You provided a `value` prop to a form field without an ' +
88
'`onChange` handler. This will render a read-only field. If ' +
89
'the field should be mutable use `defaultValue`. Otherwise, ' +
90
'set either `onChange` or `readOnly`.'
91
);
92
},
93
checked: function(props, propName, componentName) {
94
if (!props[propName] ||
95
props.onChange ||
96
props.readOnly ||
97
props.disabled) {
98
return null;
99
}
100
return new Error(
101
'You provided a `checked` prop to a form field without an ' +
102
'`onChange` handler. This will render a read-only field. If ' +
103
'the field should be mutable use `defaultChecked`. Otherwise, ' +
104
'set either `onChange` or `readOnly`.'
105
);
106
},
107
onChange: ReactPropTypes.func
108
}
109
},
110
111
/**
112
* @param {ReactComponent} input Form component
113
* @return {*} current value of the input either from value prop or link.
114
*/
115
getValue: function(input) {
116
if (input.props.valueLink) {
117
_assertValueLink(input);
118
return input.props.valueLink.value;
119
}
120
return input.props.value;
121
},
122
123
/**
124
* @param {ReactComponent} input Form component
125
* @return {*} current checked status of the input either from checked prop
126
* or link.
127
*/
128
getChecked: function(input) {
129
if (input.props.checkedLink) {
130
_assertCheckedLink(input);
131
return input.props.checkedLink.value;
132
}
133
return input.props.checked;
134
},
135
136
/**
137
* @param {ReactComponent} input Form component
138
* @return {function} change callback either from onChange prop or link.
139
*/
140
getOnChange: function(input) {
141
if (input.props.valueLink) {
142
_assertValueLink(input);
143
return _handleLinkedValueChange;
144
} else if (input.props.checkedLink) {
145
_assertCheckedLink(input);
146
return _handleLinkedCheckChange;
147
}
148
return input.props.onChange;
149
}
150
};
151
152
module.exports = LinkedValueUtils;
153
154