UNPKG

16.3 kBJavaScriptView Raw
1var _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; };
2
3var _class, _temp;
4
5function _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; }
6
7function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8
9function _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; }
10
11function _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; }
12
13import React, { Component } from 'react';
14import PropTypes from 'prop-types';
15
16var InputText = (_temp = _class = function (_Component) {
17 _inherits(InputText, _Component);
18
19 function InputText(props) {
20 _classCallCheck(this, InputText);
21
22 var _this = _possibleConstructorReturn(this, _Component.call(this, props));
23
24 _this.componentDidUpdate = function (prevProps) {
25 if (prevProps.readOnly !== _this.props.readOnly || prevProps.disabled !== _this.props.disabled) {
26 _this.updateShowClear();
27 }
28 };
29
30 _this.onClick = function (e) {
31 e.stopPropagation();
32 var _this$props = _this.props,
33 onClick = _this$props.onClick,
34 onClickInput = _this$props.onClickInput,
35 onClickLicon = _this$props.onClickLicon,
36 onClickRicon = _this$props.onClickRicon;
37
38 if (_this.props.disabled) return;
39 var target = e.target;
40 if (target.classList.contains('clearicon')) {
41 _this.onClear(e);
42 } else if (target.classList.contains('licon')) {
43 if (onClickLicon) onClickLicon(_this.$input.value, Object.getArgs(e, _this.props.args));
44 } else if (target.classList.contains('ricon')) {
45 if (onClickRicon) onClickRicon(_this.$input.value, Object.getArgs(e, _this.props.args));
46 } else if (target.classList.contains('input-text')) {
47 if (onClick) onClick(_this.$input.value, Object.getArgs(e, _this.props.args));
48 if (onClickInput) onClickInput(_this.$input.value, Object.getArgs(e, _this.props.args));
49 } else {
50 if (onClick) onClick(_this.$input.value, Object.getArgs(e, _this.props.args));
51 }
52 };
53
54 _this.preAutoSize = function () {
55 _this.$input.style.height = _this.$pre.clientHeight + 'px';
56 };
57
58 _this.onChange = function (e) {
59 var target = e.target;
60 var value = target.value;
61 var _this$props2 = _this.props,
62 pre = _this$props2.pre,
63 valueBindProp = _this$props2.valueBindProp,
64 type = _this$props2.type,
65 onChange = _this$props2.onChange,
66 maxLength = _this$props2.maxLength;
67 // 自动扩充功能
68
69 if (pre) {
70 _this.$pre.children[0].innerText = value;
71 _this.preAutoSize();
72 }
73 // 最大长度
74 if (maxLength && value && value.length > maxLength) {
75 value = value.substring(0, maxLength);
76 }
77 // 输入时只校验最大值、小数点、最大长度、返回错误
78 if (type === 'number') {
79 var _this$props3 = _this.props,
80 max = _this$props3.max,
81 digits = _this$props3.digits,
82 onError = _this$props3.onError;
83
84 value = Math.Calc.correctNumber(e.target.value, { max: max, digits: digits, maxLength: maxLength, onError: onError });
85 }
86 // onChange
87 if (onChange) onChange(value, Object.getArgs(e, _this.props.args));
88 // 非valueBindProp时, 更新清除按钮状态
89 if (!valueBindProp) _this.updateShowClear(value);
90 };
91
92 _this.onBlur = function (e) {
93 var target = e.target;
94 var value = target.value;
95 var _this$props4 = _this.props,
96 valueBindProp = _this$props4.valueBindProp,
97 min = _this$props4.min,
98 type = _this$props4.type,
99 onBlur = _this$props4.onBlur,
100 onChange = _this$props4.onChange;
101
102 if (type === 'number') {
103 // 失去焦点时只校验非空、最小值
104 value = Math.Calc.correctNumber(value, { min: min });
105 if (!valueBindProp) target.value = value;
106 if (onChange) onChange(value, Object.getArgs(e, _this.props.args));
107 }
108 if (onBlur) onBlur(value, Object.getArgs(e, _this.props.args));
109 };
110
111 _this.onFocus = function (e) {
112 var target = e.target;
113 var value = target.value;
114 var _this$props5 = _this.props,
115 onFocus = _this$props5.onFocus,
116 readOnly = _this$props5.readOnly;
117
118 if (onFocus) {
119 onFocus(value, Object.getArgs(e, _this.props.args));
120 e.stopPropagation();
121 }
122 if (readOnly) {
123 target.blur();
124 }
125 };
126
127 _this.onClear = function (e) {
128 _this.$input.focus();
129 // 赋值
130 var _this$props6 = _this.props,
131 args = _this$props6.args,
132 valueBindProp = _this$props6.valueBindProp,
133 clear = _this$props6.clear,
134 pre = _this$props6.pre,
135 onChange = _this$props6.onChange;
136
137 if (!valueBindProp) _this.$input.value = '';
138 if (clear && typeof clear === 'function') clear('', Object.getArgs(e, args));
139 if (onChange) {
140 onChange('', Object.getArgs(e, args));
141 }
142 // 自动扩充功能
143 if (pre) {
144 _this.preAutoSize();
145 }
146 e.stopPropagation();
147 // 非valueBindProp时, 更新清除按钮状态
148 if (!valueBindProp) _this.updateShowClear('');
149 };
150
151 _this.getInputDOM = function () {
152 var _this$props7 = _this.props,
153 args = _this$props7.args,
154 style = _this$props7.style,
155 className = _this$props7.className,
156 onClick = _this$props7.onClick,
157 onChange = _this$props7.onChange,
158 onClickInput = _this$props7.onClickInput,
159 onBlur = _this$props7.onBlur,
160 onFocus = _this$props7.onFocus,
161 max = _this$props7.max,
162 min = _this$props7.min,
163 digits = _this$props7.digits,
164 licon = _this$props7.licon,
165 onClickLicon = _this$props7.onClickLicon,
166 ricon = _this$props7.ricon,
167 onClickRicon = _this$props7.onClickRicon,
168 clear = _this$props7.clear,
169 clearClassName = _this$props7.clearClassName,
170 clearStyle = _this$props7.clearStyle,
171 rcaption = _this$props7.rcaption,
172 pre = _this$props7.pre,
173 type = _this$props7.type,
174 valueBindProp = _this$props7.valueBindProp,
175 autoFocus = _this$props7.autoFocus,
176 inputClassName = _this$props7.inputClassName,
177 inputStyle = _this$props7.inputStyle,
178 maxLength = _this$props7.maxLength,
179 value = _this$props7.value,
180 placeholder = _this$props7.placeholder,
181 readOnly = _this$props7.readOnly,
182 disabled = _this$props7.disabled,
183 others = _objectWithoutProperties(_this$props7, ['args', 'style', 'className', 'onClick', 'onChange', 'onClickInput', 'onBlur', 'onFocus', 'max', 'min', 'digits', 'licon', 'onClickLicon', 'ricon', 'onClickRicon', 'clear', 'clearClassName', 'clearStyle', 'rcaption', 'pre', 'type', 'valueBindProp', 'autoFocus', 'inputClassName', 'inputStyle', 'maxLength', 'value', 'placeholder', 'readOnly', 'disabled']);
184 // pre类型
185
186
187 if (pre) {
188 // pre的左右padding
189 var preLeft = 0;
190 var preRight = 0;
191 if (inputStyle) {
192 if (inputStyle.padding) {
193 var paddingValues = inputStyle.padding.split(' ');
194 if (paddingValues.length === 1) {
195 preLeft = paddingValues[0];
196 preRight = paddingValues[0];
197 } else if (paddingValues.length === 2) {
198 preLeft = paddingValues[1];
199 preRight = paddingValues[1];
200 } else if (paddingValues.length === 4) {
201 preLeft = paddingValues[1];
202 preRight = paddingValues[3];
203 }
204 } else if (inputStyle.paddingLeft || inputStyle.paddingRight) {
205 preLeft = inputStyle.paddingLeft || '0';
206 preRight = inputStyle.paddingRight || '0';
207 }
208 }
209 return React.createElement(
210 'div',
211 { className: 'input-pre-box' + (inputClassName ? ' ' + inputClassName : ''), style: inputStyle },
212 valueBindProp && React.createElement('textarea', _extends({ autoFocus: autoFocus, ref: function ref(el) {
213 _this.$input = el;
214 }, value: value, maxLength: maxLength, readOnly: readOnly, disabled: disabled, className: 'input-pre', placeholder: placeholder, onChange: _this.onChange, onBlur: _this.onBlur, onFocus: _this.onFocus }, others)),
215 !valueBindProp && React.createElement('textarea', _extends({ autoFocus: autoFocus, ref: function ref(el) {
216 _this.$input = el;
217 }, defaultValue: value, maxLength: maxLength, readOnly: readOnly, disabled: disabled, className: 'input-pre', placeholder: placeholder, onChange: _this.onChange, onBlur: _this.onBlur, onFocus: _this.onFocus }, others)),
218 React.createElement(
219 'pre',
220 { ref: function ref(el) {
221 _this.$pre = el;
222 }, style: { left: preLeft, right: preRight } },
223 React.createElement(
224 'span',
225 null,
226 value
227 )
228 )
229 );
230 }
231 // textarea类型
232 if (type === 'textarea') {
233 // 如果值绑定属性,则只有通过父组件的prop来改变值
234 if (valueBindProp) {
235 return React.createElement('textarea', _extends({ autoFocus: autoFocus, ref: function ref(el) {
236 _this.$input = el;
237 }, value: value, maxLength: maxLength, readOnly: readOnly, disabled: disabled, placeholder: placeholder, onChange: _this.onChange, onBlur: _this.onBlur, onFocus: _this.onFocus, className: 'input-area' + (inputClassName ? ' ' + inputClassName : ''), style: inputStyle }, others));
238 }
239 return React.createElement('textarea', _extends({ autoFocus: autoFocus, ref: function ref(el) {
240 _this.$input = el;
241 }, defaultValue: value, maxLength: maxLength, readOnly: readOnly, disabled: disabled, placeholder: placeholder, onChange: _this.onChange, onBlur: _this.onBlur, onFocus: _this.onFocus, className: 'input-area' + (inputClassName ? ' ' + inputClassName : ''), style: inputStyle }, others));
242 }
243 // 其它类型
244 if (valueBindProp) {
245 // 如果值绑定属性,则只有通过父组件的prop来改变值
246 return React.createElement('input', _extends({ max: max, min: min, autoFocus: autoFocus, ref: function ref(el) {
247 _this.$input = el;
248 }, type: type, value: value, maxLength: maxLength, readOnly: readOnly, disabled: disabled, placeholder: placeholder, onChange: _this.onChange, onBlur: _this.onBlur, onFocus: _this.onFocus, className: 'input-text' + (inputClassName ? ' ' + inputClassName : ''), style: inputStyle }, others));
249 }
250 return React.createElement('input', _extends({ max: max, min: min, autoFocus: autoFocus, ref: function ref(el) {
251 _this.$input = el;
252 }, type: type, defaultValue: value, maxLength: maxLength, readOnly: readOnly, disabled: disabled, placeholder: placeholder, onChange: _this.onChange, onBlur: _this.onBlur, onFocus: _this.onFocus, className: 'input-text' + (inputClassName ? ' ' + inputClassName : ''), style: inputStyle }, others));
253 };
254
255 _this.updateShowClear = function (value) {
256 var _this$props8 = _this.props,
257 readOnly = _this$props8.readOnly,
258 disabled = _this$props8.disabled,
259 clear = _this$props8.clear,
260 valueBindProp = _this$props8.valueBindProp;
261
262 if (valueBindProp) return;
263 if (readOnly || disabled || !clear) {
264 _this.setState({
265 showClear: false
266 });
267 return;
268 }
269 var showClear = false;
270 if (value) {
271 showClear = true;
272 } else {
273 showClear = false;
274 }
275 _this.setState({
276 showClear: showClear
277 });
278 };
279
280 _this.state = {
281 showClear: props.value && props.clear && !props.readOnly && !props.disabled // 用于非valueBindProp
282 };
283 return _this;
284 }
285 // 解决非valueBindProp状态下, readOnly和disabled发生变化时, 不更新清除按钮的问题
286
287 // 点击容器
288
289 // 自动扩充功能
290
291 // 文本框事件
292
293 // 点击清除
294
295 // 非valueBindProp时, 更新清除按钮状态
296
297
298 InputText.prototype.render = function render() {
299 var _this2 = this;
300
301 var _props = this.props,
302 value = _props.value,
303 valueBindProp = _props.valueBindProp,
304 readOnly = _props.readOnly,
305 disabled = _props.disabled,
306 className = _props.className,
307 style = _props.style,
308 licon = _props.licon,
309 ricon = _props.ricon,
310 clear = _props.clear,
311 clearClassName = _props.clearClassName,
312 clearStyle = _props.clearStyle,
313 rcaption = _props.rcaption;
314 // 支持valueBindProp和非valueBindProp两种模式的清空按钮控制
315
316 var showClear = false;
317 if (value && !readOnly && !disabled) {
318 showClear = true;
319 } else {
320 showClear = false;
321 }
322 var isShowClear = valueBindProp ? showClear : this.state.showClear;
323 return React.createElement(
324 'div',
325 { ref: function ref(el) {
326 _this2.$el = el;
327 }, className: 'input-text-box' + (className ? ' ' + className : ''), style: style, onClick: this.onClick },
328 licon && licon,
329 this.getInputDOM(),
330 clear && React.createElement('i', { className: 'icon clearicon ' + (isShowClear ? '' : 'hide') + (clearClassName ? ' ' + clearClassName : ''), style: clearStyle }),
331 ricon && ricon,
332 rcaption && rcaption
333 );
334 };
335
336 return InputText;
337}(Component), _class.defaultProps = {
338 type: 'text',
339 value: '',
340 readOnly: false,
341 disabled: false,
342 clearClassName: 'ricon close-icon-clear size18'
343}, _temp);
344export { InputText as default };
345InputText.propTypes = process.env.NODE_ENV !== "production" ? {
346 type: PropTypes.string, // 类型: text, number, tel, password
347 valueBindProp: PropTypes.bool, // 值是否绑定属性
348 pre: PropTypes.bool, // 自动扩充功能
349 // 容器
350 args: PropTypes.any,
351 style: PropTypes.object,
352 className: PropTypes.string,
353 onClick: PropTypes.func,
354 // 文本框
355 autoFocus: PropTypes.bool,
356 maxLength: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
357 max: PropTypes.oneOfType([// 日期或者数字框
358 PropTypes.string, PropTypes.number]),
359 min: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
360 digits: PropTypes.oneOfType([PropTypes.bool, PropTypes.string, PropTypes.number]),
361 readOnly: PropTypes.bool,
362 disabled: PropTypes.bool,
363
364 inputStyle: PropTypes.object,
365 inputClassName: PropTypes.string,
366 value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
367 placeholder: PropTypes.string,
368 onChange: PropTypes.func,
369 onClickInput: PropTypes.func,
370 onBlur: PropTypes.func,
371 onFocus: PropTypes.func,
372 // 左右图标
373 licon: PropTypes.node,
374 onClickLicon: PropTypes.func, // 点击样式包含licon触发
375 ricon: PropTypes.node,
376 onClickRicon: PropTypes.func, // 点击样式包含ricon触发
377 // 清除按键
378 clear: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
379 clearClassName: PropTypes.string,
380 clearStyle: PropTypes.object,
381 // 右侧内容
382 rcaption: PropTypes.node
383} : {};
\No newline at end of file