forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
165 lines (145 loc) · 4.74 KB
/
index.js
File metadata and controls
165 lines (145 loc) · 4.74 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
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
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var React = require('react');
if (typeof React === 'undefined') {
throw Error(
'react-linked-input could not find the React object. If you are using script tags, ' +
'make sure that React is being loaded before react-linked-input.'
);
}
var invariant = require('fbjs/lib/invariant');
function _assertSingleLink(inputProps) {
invariant(
inputProps.checkedLink == null || inputProps.valueLink == null,
'Cannot provide a checkedLink and a valueLink. If you want to use ' +
"checkedLink, you probably don't want to use valueLink and vice versa."
);
}
function _assertValueLink(inputProps) {
_assertSingleLink(inputProps);
invariant(
inputProps.value == null && inputProps.onChange == null,
'Cannot provide a valueLink and a value or onChange event. If you want ' +
"to use value or onChange, you probably don't want to use valueLink."
);
}
function _assertCheckedLink(inputProps) {
_assertSingleLink(inputProps);
invariant(
inputProps.checked == null && inputProps.onChange == null,
'Cannot provide a checkedLink and a checked property or onChange event. ' +
"If you want to use checked or onChange, you probably don't want to " +
'use checkedLink'
);
}
/**
* Provide a linked `value` attribute for controlled forms. You should not use
* this outside of the ReactDOM controlled form components.
*/
var LinkedValueUtils = {
/**
* @param {object} inputProps Props for form component
* @return {*} current value of the input either from value prop or link.
*/
getValue: function(inputProps) {
if (inputProps.valueLink) {
_assertValueLink(inputProps);
return inputProps.valueLink.value;
}
return inputProps.value;
},
/**
* @param {object} inputProps Props for form component
* @return {*} current checked status of the input either from checked prop
* or link.
*/
getChecked: function(inputProps) {
if (inputProps.checkedLink) {
_assertCheckedLink(inputProps);
return inputProps.checkedLink.value;
}
return inputProps.checked;
},
/**
* @param {object} inputProps Props for form component
* @param {SyntheticEvent} event change event to handle
*/
executeOnChange: function(inputProps, event) {
if (inputProps.valueLink) {
_assertValueLink(inputProps);
return inputProps.valueLink.requestChange(event.target.value);
} else if (inputProps.checkedLink) {
_assertCheckedLink(inputProps);
return inputProps.checkedLink.requestChange(event.target.checked);
} else if (inputProps.onChange) {
return inputProps.onChange.call(undefined, event);
}
}
};
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function');
}
}
function _possibleConstructorReturn(self, call) {
if (!self) {
throw new ReferenceError(
"this hasn't been initialised - super() hasn't been called"
);
}
return call && (typeof call === 'object' || typeof call === 'function')
? call
: self;
}
function _inherits(subClass, superClass) {
if (typeof superClass !== 'function' && superClass !== null) {
throw new TypeError(
'Super expression must either be null or a function, not ' +
typeof superClass
);
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true
}
});
if (superClass) {
if (Object.setPrototypeOf) {
Object.setPrototypeOf(subClass, superClass);
} else {
// eslint-disable-next-line no-proto
subClass.__proto__ = superClass;
}
}
}
var LI = (function(_React$Component) {
_inherits(LinkedInput, _React$Component);
function LinkedInput() {
_classCallCheck(this, LinkedInput);
var _this = _possibleConstructorReturn(this, _React$Component.call(this));
_this.handleChange = _this.handleChange.bind(_this);
return _this;
}
LinkedInput.prototype.handleChange = function handleChange(e) {
LinkedValueUtils.executeOnChange(this.props, e);
};
LinkedInput.prototype.render = function render() {
var newProps = Object.assign({}, this.props);
newProps.value = LinkedValueUtils.getValue(this.props);
newProps.checked = LinkedValueUtils.getChecked(this.props);
newProps.onChange = this.handleChange;
delete newProps.valueLink;
delete newProps.checkedLink;
return React.createElement('input', newProps);
};
return LinkedInput;
})(React.Component);
module.exports = LI;