UNPKG

16.6 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
5var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports["default"] = void 0;
11
12var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
13
14var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
15
16var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
17
18var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
19
20var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
21
22var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
23
24var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
25
26var _react = _interopRequireWildcard(require("react"));
27
28var _propTypes = _interopRequireDefault(require("prop-types"));
29
30var _classnames = _interopRequireDefault(require("classnames"));
31
32var _omit = _interopRequireDefault(require("lodash/omit"));
33
34var _icon = _interopRequireDefault(require("../icon"));
35
36var _enum = require("../_util/enum");
37
38var _configure = require("../configure");
39
40function _createSuper(Derived) {
41 function isNativeReflectConstruct() {
42 if (typeof Reflect === "undefined" || !Reflect.construct) return false;
43 if (Reflect.construct.sham) return false;
44 if (typeof Proxy === "function") return true;
45
46 try {
47 Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
48 return true;
49 } catch (e) {
50 return false;
51 }
52 }
53
54 return function () {
55 var Super = (0, _getPrototypeOf2["default"])(Derived),
56 result;
57
58 if (isNativeReflectConstruct()) {
59 var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor;
60 result = Reflect.construct(Super, arguments, NewTarget);
61 } else {
62 result = Super.apply(this, arguments);
63 }
64
65 return (0, _possibleConstructorReturn2["default"])(this, result);
66 };
67}
68
69function fixControlledValue(value) {
70 if (typeof value === 'undefined' || value === null) {
71 return '';
72 }
73
74 return value;
75}
76
77var Input =
78/*#__PURE__*/
79function (_Component) {
80 (0, _inherits2["default"])(Input, _Component);
81
82 var _super = _createSuper(Input);
83
84 function Input(props) {
85 var _this;
86
87 (0, _classCallCheck2["default"])(this, Input);
88 _this = _super.call(this, props);
89
90 _this.handleKeyDown = function (e) {
91 var _this$props = _this.props,
92 onPressEnter = _this$props.onPressEnter,
93 onKeyDown = _this$props.onKeyDown;
94
95 if (e.keyCode === 13 && onPressEnter) {
96 onPressEnter(e);
97 }
98
99 if (onKeyDown) {
100 onKeyDown(e);
101 }
102 };
103
104 _this.handleFocus = function (e) {
105 var _this$props2 = _this.props,
106 onFocus = _this$props2.onFocus,
107 readOnly = _this$props2.readOnly;
108
109 if (!readOnly) {
110 _this.setState({
111 focused: true
112 });
113 }
114
115 if (onFocus) {
116 onFocus(e);
117 }
118 };
119
120 _this.handleBlur = function (e) {
121 var _this$props3 = _this.props,
122 onBlur = _this$props3.onBlur,
123 readOnly = _this$props3.readOnly;
124
125 if (!readOnly) {
126 _this.setState({
127 focused: false
128 });
129 }
130
131 if (onBlur) {
132 onBlur(e);
133 }
134 };
135
136 _this.handleChange = function (e) {
137 var onChange = _this.props.onChange;
138
139 if (!('value' in _this.props)) {
140 _this.setState({
141 value: e.target.value
142 });
143 }
144
145 if (onChange) {
146 onChange(e);
147 }
148 };
149
150 _this.handleCopy = function () {
151 var onCopy = _this.props.onCopy;
152
153 _this.input.select();
154
155 document.execCommand('Copy');
156
157 if (onCopy) {
158 onCopy(_this.input.value);
159 }
160 };
161
162 _this.handleTogglePassword = function () {
163 var showPassword = _this.state.showPassword;
164
165 _this.setState({
166 showPassword: !showPassword
167 });
168 };
169
170 _this.saveInput = function (node) {
171 _this.input = node;
172 };
173
174 _this.saveRenderedRef = function (node) {
175 _this.rendered = node;
176 };
177
178 _this.savePrefix = function (node) {
179 _this.prefix = node;
180 };
181
182 _this.saveSuffix = function (node) {
183 _this.suffix = node;
184 };
185
186 _this.state = {
187 value: typeof props.value === 'undefined' ? props.defaultValue : props.value,
188 focused: false,
189 showPassword: false
190 };
191 return _this;
192 }
193
194 (0, _createClass2["default"])(Input, [{
195 key: "componentDidMount",
196 value: function componentDidMount() {
197 var _this$props4 = this.props,
198 focused = _this$props4.focused,
199 autoFocus = _this$props4.autoFocus;
200
201 if (autoFocus) {
202 this.setState({
203 focused: true
204 });
205 }
206
207 if (typeof focused === 'boolean') {
208 this.setState({
209 focused: focused
210 });
211 }
212
213 this.setRenderedStyle();
214 }
215 }, {
216 key: "componentWillReceiveProps",
217 value: function componentWillReceiveProps(nextProps) {
218 var value = this.state.value;
219
220 if ('value' in nextProps && value !== nextProps.value) {
221 this.setState({
222 value: nextProps.value
223 });
224 }
225
226 if (nextProps.autoFocus) {
227 this.setState({
228 focused: true
229 });
230 }
231
232 if (typeof nextProps.focused === 'boolean') {
233 this.setState({
234 focused: nextProps.focused
235 });
236 }
237
238 if (nextProps.type !== 'password') {
239 this.setState({
240 showPassword: false
241 });
242 }
243 }
244 }, {
245 key: "componentDidUpdate",
246 value: function componentDidUpdate() {
247 this.setRenderedStyle();
248 }
249 }, {
250 key: "setRenderedStyle",
251 value: function setRenderedStyle() {
252 var rendered = this.rendered,
253 suffix = this.suffix,
254 prefix = this.prefix;
255 var suffixWidth;
256 var prefixWidth;
257 var margin = '0';
258 var width = '100%';
259
260 if (suffix && prefix) {
261 suffixWidth = "".concat((suffix.clientWidth || -2) + 2, "px");
262 prefixWidth = "".concat((prefix.clientWidth || -2) + 2, "px");
263 margin = "0 ".concat(suffixWidth, " 0 ").concat(prefixWidth);
264 width = "calc(100% - ".concat(suffixWidth, " - ").concat(prefixWidth, ")");
265 } else if (suffix) {
266 suffixWidth = "".concat((suffix.clientWidth || -2) + 2, "px");
267 margin = "0 ".concat(suffixWidth, " 0 0");
268 width = "calc(100% - ".concat(suffixWidth, ")");
269 } else if (prefix) {
270 prefixWidth = "".concat((prefix.clientWidth || -2) + 2, "px");
271 margin = "0 0 0 ".concat(prefixWidth);
272 width = "calc(100% - ".concat(prefixWidth, ")");
273 }
274
275 rendered.style.margin = margin;
276 rendered.style.width = width;
277 }
278 }, {
279 key: "focus",
280 value: function focus() {
281 this.input.focus();
282 }
283 }, {
284 key: "blur",
285 value: function blur() {
286 this.input.blur();
287 }
288 }, {
289 key: "getPrefixCls",
290 value: function getPrefixCls() {
291 var prefixCls = this.props.prefixCls;
292 return (0, _configure.getPrefixCls)('input', prefixCls);
293 }
294 }, {
295 key: "getInputClassName",
296 value: function getInputClassName() {
297 var _classNames;
298
299 var _this$props5 = this.props,
300 size = _this$props5.size,
301 copy = _this$props5.copy;
302 var prefixCls = this.getPrefixCls();
303 return (0, _classnames["default"])(prefixCls, (_classNames = {}, (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-sm"), size === _enum.Size.small), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-lg"), size === _enum.Size.large), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-has-copy"), copy), _classNames));
304 }
305 }, {
306 key: "renderCopyIcon",
307 value: function renderCopyIcon() {
308 var copy = this.props.copy;
309 var prefixCls = this.getPrefixCls();
310 return copy ? _react["default"].createElement("span", {
311 className: "".concat(prefixCls, "-icon"),
312 onClick: this.handleCopy
313 }, _react["default"].createElement(_icon["default"], {
314 className: "".concat(prefixCls, "-icon-copy"),
315 type: "library_books"
316 })) : null;
317 }
318 }, {
319 key: "renderShowPassword",
320 value: function renderShowPassword() {
321 var type = this.props.type;
322 var prefixCls = this.getPrefixCls();
323 var showPassword = this.state.showPassword;
324 return type === 'password' ? _react["default"].createElement("span", {
325 className: "".concat(prefixCls, "-icon"),
326 onClick: this.handleTogglePassword
327 }, _react["default"].createElement(_icon["default"], {
328 className: "".concat(prefixCls, "-icon-copy"),
329 type: showPassword ? 'visibility' : 'visibility_off'
330 })) : null;
331 }
332 }, {
333 key: "renderInput",
334 value: function renderInput() {
335 var _this$props6 = this.props,
336 className = _this$props6.className,
337 type = _this$props6.type;
338 var _this$state = this.state,
339 showPassword = _this$state.showPassword,
340 value = _this$state.value; // Fix https://fb.me/react-unknown-prop
341
342 var otherProps = (0, _omit["default"])(this.props, ['placeholder', 'prefixCls', 'onPressEnter', 'addonBefore', 'addonAfter', 'prefix', 'suffix', 'label', 'copy', 'style', 'focused', 'showLengthInfo', 'showPasswordEye', 'size', 'border']);
343 return _react["default"].createElement("input", (0, _extends2["default"])({}, otherProps, {
344 value: fixControlledValue(value),
345 className: (0, _classnames["default"])(this.getInputClassName(), className),
346 onKeyDown: this.handleKeyDown,
347 ref: this.saveInput,
348 onFocus: this.handleFocus,
349 onBlur: this.handleBlur,
350 onChange: this.handleChange,
351 type: showPassword ? 'text' : type
352 }));
353 }
354 }, {
355 key: "getLengthInfo",
356 value: function getLengthInfo() {
357 var _this$props7 = this.props,
358 maxLength = _this$props7.maxLength,
359 showLengthInfo = _this$props7.showLengthInfo;
360 var prefixCls = this.getPrefixCls();
361 var value = this.state.value;
362 var inputLength = value ? value.length : 0;
363 return maxLength && showLengthInfo || maxLength && maxLength > 0 && inputLength === maxLength ? _react["default"].createElement("div", {
364 className: "".concat(prefixCls, "-length-info")
365 }, "".concat(inputLength, "/").concat(maxLength)) : null;
366 }
367 }, {
368 key: "getLabel",
369 value: function getLabel() {
370 var focused = this.state.focused;
371 var _this$props8 = this.props,
372 placeholder = _this$props8.placeholder,
373 label = _this$props8.label;
374
375 if (!this.hasValue() && !focused && placeholder) {
376 return placeholder;
377 }
378
379 return label;
380 }
381 }, {
382 key: "renderFloatLabel",
383 value: function renderFloatLabel() {
384 var label = this.getLabel();
385 var border = this.props.border;
386
387 if (label && border) {
388 var prefixCls = this.getPrefixCls();
389 return _react["default"].createElement("div", {
390 className: "".concat(prefixCls, "-label-wrapper")
391 }, _react["default"].createElement("div", {
392 className: "".concat(prefixCls, "-label")
393 }, label));
394 }
395 }
396 }, {
397 key: "getSizeClassName",
398 value: function getSizeClassName(name) {
399 var _classNames2;
400
401 var size = this.props.size;
402 var prefixCls = this.getPrefixCls();
403 return (0, _classnames["default"])("".concat(prefixCls, "-").concat(name), (_classNames2 = {}, (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-").concat(name, "-sm"), size === _enum.Size.small), (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-").concat(name, "-lg"), size === _enum.Size.large), _classNames2));
404 }
405 }, {
406 key: "hasValue",
407 value: function hasValue() {
408 var value = this.state.value;
409 return value && value.length !== 0;
410 }
411 }, {
412 key: "renderPlaceholder",
413 value: function renderPlaceholder() {
414 var _this$props9 = this.props,
415 placeholder = _this$props9.placeholder,
416 border = _this$props9.border;
417
418 if (!border && placeholder) {
419 var prefixCls = this.getPrefixCls();
420 return _react["default"].createElement("div", {
421 className: "".concat(prefixCls, "-placeholder")
422 }, placeholder);
423 }
424 }
425 }, {
426 key: "render",
427 value: function render() {
428 var _classNames3;
429
430 var props = this.props;
431 var _this$props10 = this.props,
432 disabled = _this$props10.disabled,
433 label = _this$props10.label,
434 style = _this$props10.style,
435 showPasswordEye = _this$props10.showPasswordEye,
436 border = _this$props10.border;
437 var prefixCls = this.getPrefixCls();
438 var focused = this.state.focused;
439 var prefix = props.prefix ? _react["default"].createElement("span", {
440 ref: this.savePrefix,
441 className: this.getSizeClassName('prefix')
442 }, props.prefix) : null;
443 var suffix = props.suffix ? _react["default"].createElement("span", {
444 ref: this.saveSuffix,
445 className: this.getSizeClassName('suffix')
446 }, props.suffix) : null;
447 var className = (0, _classnames["default"])("".concat(prefixCls, "-wrapper"), (_classNames3 = {}, (0, _defineProperty2["default"])(_classNames3, "".concat(prefixCls, "-has-value"), this.hasValue()), (0, _defineProperty2["default"])(_classNames3, "".concat(prefixCls, "-focused"), focused), (0, _defineProperty2["default"])(_classNames3, "".concat(prefixCls, "-disabled"), disabled), (0, _defineProperty2["default"])(_classNames3, "".concat(prefixCls, "-has-label"), !!label), (0, _defineProperty2["default"])(_classNames3, "".concat(prefixCls, "-has-prefix"), !!prefix), (0, _defineProperty2["default"])(_classNames3, "".concat(prefixCls, "-has-suffix"), !!suffix), (0, _defineProperty2["default"])(_classNames3, "".concat(prefixCls, "-has-border"), border), _classNames3));
448 return _react["default"].createElement("span", {
449 className: className,
450 style: style
451 }, _react["default"].createElement("div", {
452 className: "".concat(prefixCls, "-content")
453 }, _react["default"].createElement("div", {
454 className: "".concat(prefixCls, "-rendered-wrapper")
455 }, prefix, _react["default"].createElement("div", {
456 className: this.getSizeClassName('rendered'),
457 ref: this.saveRenderedRef
458 }, this.renderPlaceholder(), this.renderInput(), this.renderFloatLabel(), this.renderCopyIcon(), showPasswordEye ? this.renderShowPassword() : null), suffix), this.getLengthInfo()));
459 }
460 }]);
461 return Input;
462}(_react.Component);
463
464exports["default"] = Input;
465Input.displayName = 'Input';
466Input.defaultProps = {
467 type: 'text',
468 disabled: false,
469 readOnly: false,
470 showLengthInfo: true,
471 showPasswordEye: false,
472 border: true
473};
474Input.propTypes = {
475 type: _propTypes["default"].string,
476 id: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].number]),
477 label: _propTypes["default"].node,
478 size: _propTypes["default"].oneOf([_enum.Size.small, _enum.Size["default"], _enum.Size.large]),
479 maxLength: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].number]),
480 disabled: _propTypes["default"].bool,
481 value: _propTypes["default"].any,
482 defaultValue: _propTypes["default"].any,
483 className: _propTypes["default"].string,
484 addonBefore: _propTypes["default"].node,
485 addonAfter: _propTypes["default"].node,
486 prefixCls: _propTypes["default"].string,
487 autosize: _propTypes["default"].oneOfType([_propTypes["default"].bool, _propTypes["default"].object]),
488 onPressEnter: _propTypes["default"].func,
489 onKeyDown: _propTypes["default"].func,
490 onKeyUp: _propTypes["default"].func,
491 onFocus: _propTypes["default"].func,
492 onBlur: _propTypes["default"].func,
493 prefix: _propTypes["default"].node,
494 suffix: _propTypes["default"].node,
495 copy: _propTypes["default"].bool,
496 onCopy: _propTypes["default"].func,
497 readOnly: _propTypes["default"].bool,
498 focused: _propTypes["default"].bool,
499 border: _propTypes["default"].bool,
500 showLengthInfo: _propTypes["default"].bool,
501 showPasswordEye: _propTypes["default"].bool
502};
503//# sourceMappingURL=Input.js.map