forked from react-toolbox/react-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInput.js
More file actions
288 lines (244 loc) · 12.4 KB
/
Input.js
File metadata and controls
288 lines (244 loc) · 12.4 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
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
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Input = exports.inputFactory = undefined;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _classnames4 = require('classnames');
var _classnames5 = _interopRequireDefault(_classnames4);
var _reactCssThemr = require('react-css-themr');
var _identifiers = require('../identifiers.js');
var _FontIcon = require('../font_icon/FontIcon.js');
var _FontIcon2 = _interopRequireDefault(_FontIcon);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
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) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var factory = function factory(FontIcon) {
var Input = function (_React$Component) {
_inherits(Input, _React$Component);
function Input() {
var _Object$getPrototypeO;
var _temp, _this, _ret;
_classCallCheck(this, Input);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_Object$getPrototypeO = Object.getPrototypeOf(Input)).call.apply(_Object$getPrototypeO, [this].concat(args))), _this), _this.handleChange = function (event) {
var _this$props = _this.props;
var onChange = _this$props.onChange;
var multiline = _this$props.multiline;
var maxLength = _this$props.maxLength;
var valueFromEvent = event.target.value;
// Trim value to maxLength if that exists (only on multiline inputs).
// Note that this is still required even tho we have the onKeyPress filter
// because the user could paste smt in the textarea.
var haveToTrim = multiline && maxLength && event.target.value.length > maxLength;
var value = haveToTrim ? valueFromEvent.substr(0, maxLength) : valueFromEvent;
// propagate to to store and therefore to the input
if (onChange) onChange(value, event);
}, _this.handleAutoresize = function () {
var element = _this.refs.input;
// compute the height difference between inner height and outer height
var style = getComputedStyle(element, null);
var heightOffset = style.boxSizing === 'content-box' ? -(parseFloat(style.paddingTop) + parseFloat(style.paddingBottom)) : parseFloat(style.borderTopWidth) + parseFloat(style.borderBottomWidth);
// resize the input to its content size
element.style.height = 'auto';
element.style.height = element.scrollHeight + heightOffset + 'px';
}, _this.handleKeyPress = function (event) {
// prevent insertion of more characters if we're a multiline input
// and maxLength exists
var _this$props2 = _this.props;
var multiline = _this$props2.multiline;
var maxLength = _this$props2.maxLength;
var onKeyPress = _this$props2.onKeyPress;
if (multiline && maxLength) {
// check if smt is selected, in which case the newly added charcter would
// replace the selected characters, so the length of value doesn't actually
// increase.
var isReplacing = event.target.selectionEnd - event.target.selectionStart;
var value = event.target.value;
if (!isReplacing && value.length === maxLength) {
event.preventDefault();
event.stopPropagation();
return;
}
}
if (onKeyPress) onKeyPress(event);
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(Input, [{
key: 'componentDidMount',
value: function componentDidMount() {
if (this.props.multiline) {
window.addEventListener('resize', this.handleAutoresize);
this.handleAutoresize();
}
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
if (!this.props.multiline && nextProps.multiline) {
window.addEventListener('resize', this.handleAutoresize);
} else if (this.props.multiline && !nextProps.multiline) {
window.removeEventListener('resize', this.handleAutoresize);
}
}
}, {
key: 'componentDidUpdate',
value: function componentDidUpdate() {
// resize the textarea, if nessesary
if (this.props.multiline) this.handleAutoresize();
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
if (this.props.multiline) window.removeEventListener('resize', this.handleAutoresize);
}
}, {
key: 'blur',
value: function blur() {
this.refs.input.blur();
}
}, {
key: 'focus',
value: function focus() {
this.refs.input.focus();
}
}, {
key: 'render',
value: function render() {
var _classnames2;
var _props = this.props;
var children = _props.children;
var disabled = _props.disabled;
var error = _props.error;
var floating = _props.floating;
var hint = _props.hint;
var icon = _props.icon;
var name = _props.name;
var labelText = _props.label;
var maxLength = _props.maxLength;
var multiline = _props.multiline;
var required = _props.required;
var theme = _props.theme;
var type = _props.type;
var value = _props.value;
var onKeyPress = _props.onKeyPress;
var others = _objectWithoutProperties(_props, ['children', 'disabled', 'error', 'floating', 'hint', 'icon', 'name', 'label', 'maxLength', 'multiline', 'required', 'theme', 'type', 'value', 'onKeyPress']);
var length = maxLength && value ? value.length : 0;
var labelClassName = (0, _classnames5.default)(theme.label, _defineProperty({}, theme.fixed, !floating));
var className = (0, _classnames5.default)(theme.input, (_classnames2 = {}, _defineProperty(_classnames2, theme.disabled, disabled), _defineProperty(_classnames2, theme.errored, error), _defineProperty(_classnames2, theme.hidden, type === 'hidden'), _defineProperty(_classnames2, theme.withIcon, icon), _classnames2), this.props.className);
var valuePresent = value !== null && value !== undefined && value !== '' && !((typeof value === 'undefined' ? 'undefined' : _typeof(value)) === Number && isNaN(value));
var inputElementProps = _extends({}, others, {
className: (0, _classnames5.default)(theme.inputElement, _defineProperty({}, theme.filled, valuePresent)),
onChange: this.handleChange,
ref: 'input',
role: 'input',
name: name,
disabled: disabled,
required: required,
type: type,
value: value
});
if (!multiline) {
inputElementProps.maxLength = maxLength;
inputElementProps.onKeyPress = onKeyPress;
} else {
inputElementProps.rows = 1;
inputElementProps.onKeyPress = this.handleKeyPress;
}
return _react2.default.createElement(
'div',
{ 'data-react-toolbox': 'input', className: className },
_react2.default.createElement(multiline ? 'textarea' : 'input', inputElementProps),
icon ? _react2.default.createElement(FontIcon, { className: theme.icon, value: icon }) : null,
_react2.default.createElement('span', { className: theme.bar }),
labelText ? _react2.default.createElement(
'label',
{ className: labelClassName },
labelText,
required ? _react2.default.createElement(
'span',
{ className: theme.required },
' * '
) : null
) : null,
hint ? _react2.default.createElement(
'span',
{ className: theme.hint },
hint
) : null,
error ? _react2.default.createElement(
'span',
{ className: theme.error },
error
) : null,
maxLength ? _react2.default.createElement(
'span',
{ className: theme.counter },
length,
'/',
maxLength
) : null,
children
);
}
}]);
return Input;
}(_react2.default.Component);
Input.propTypes = {
children: _react2.default.PropTypes.any,
className: _react2.default.PropTypes.string,
disabled: _react2.default.PropTypes.bool,
error: _react2.default.PropTypes.string,
floating: _react2.default.PropTypes.bool,
hint: _react2.default.PropTypes.string,
icon: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.string, _react2.default.PropTypes.element]),
label: _react2.default.PropTypes.string,
maxLength: _react2.default.PropTypes.number,
multiline: _react2.default.PropTypes.bool,
name: _react2.default.PropTypes.string,
onBlur: _react2.default.PropTypes.func,
onChange: _react2.default.PropTypes.func,
onFocus: _react2.default.PropTypes.func,
onKeyPress: _react2.default.PropTypes.func,
required: _react2.default.PropTypes.bool,
theme: _react2.default.PropTypes.shape({
bar: _react2.default.PropTypes.string,
counter: _react2.default.PropTypes.string,
disabled: _react2.default.PropTypes.string,
error: _react2.default.PropTypes.string,
errored: _react2.default.PropTypes.string,
hidden: _react2.default.PropTypes.string,
hint: _react2.default.PropTypes.string,
icon: _react2.default.PropTypes.string,
input: _react2.default.PropTypes.string,
inputElement: _react2.default.PropTypes.string,
required: _react2.default.PropTypes.string,
withIcon: _react2.default.PropTypes.string
}),
type: _react2.default.PropTypes.string,
value: _react2.default.PropTypes.any
};
Input.defaultProps = {
className: '',
hint: '',
disabled: false,
floating: true,
multiline: false,
required: false,
type: 'text'
};
return Input;
};
var Input = factory(_FontIcon2.default);
exports.default = (0, _reactCssThemr.themr)(_identifiers.INPUT, null, { withRef: true })(Input);
exports.inputFactory = factory;
exports.Input = Input;