UNPKG

13.6 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
13// require PrototypeMath.js, 用于解决加减法精度丢失的问题
14import React, { Component } from 'react';
15import PropTypes from 'prop-types';
16
17var NumBox = (_temp = _class = function (_Component) {
18 _inherits(NumBox, _Component);
19
20 function NumBox(props) {
21 _classCallCheck(this, NumBox);
22
23 var _this = _possibleConstructorReturn(this, _Component.call(this, props));
24
25 _this.onBlur = function (e) {
26 var _this$props = _this.props,
27 readOnly = _this$props.readOnly,
28 disabled = _this$props.disabled,
29 required = _this$props.required,
30 min = _this$props.min,
31 onChange = _this$props.onChange,
32 onBlur = _this$props.onBlur;
33
34 if (readOnly || disabled) {
35 return;
36 }
37 // 失去焦点时只校验非空、最小值
38 var value = Math.Calc.correctNumber(_this.props.value, { required: required, min: min });
39 if (onChange && '' + value !== '' + _this.props.value) onChange(value, Object.getArgs(e, _this.props.args));
40 if (onBlur) onBlur(value, Object.getArgs(e, _this.props.args));
41 };
42
43 _this.onFocus = function (e) {
44 var _this$props2 = _this.props,
45 readOnly = _this$props2.readOnly,
46 disabled = _this$props2.disabled,
47 onFocus = _this$props2.onFocus;
48
49 if (readOnly || disabled) {
50 e.target.blur();
51 return;
52 }
53 if (onFocus) onFocus(_this.props.value, Object.getArgs(e, _this.props.args));
54 };
55
56 _this.autoFocus = function () {
57 if (_this.props.disabled || _this.props.readOnly || !_this.$input) return;
58 _this.$input.focus();
59 if (_this.props.autoSelect) _this.$input.select();
60 // 修复兼容ios12的bug, 与全局的回弹并不冲突, 这里主要解决点击加减号时获取焦点, 不回弹的问题
61 var iosExp = navigator.userAgent.toLowerCase().match(/cpu iphone os (.*?) like mac os/);
62 if (iosExp && iosExp[1] && iosExp[1] > '12') {
63 // 兼容输入法把页面顶上去, 不回弹的问题
64 if (window.inputToggleTimeout) {
65 window.clearTimeout(window.inputToggleTimeout);
66 }
67 if (!_this.$input.getAttribute('ios-bug-blur')) {
68 _this.$input.setAttribute('ios-bug-blur', '1');
69 _this.$input.addEventListener('blur', function () {
70 window.inputToggleTimeout = window.setTimeout(function () {
71 document.getElementById('root').scrollIntoView();
72 }, 100);
73 }, false);
74 }
75 }
76 };
77
78 _this.onClickInput = function (e) {
79 var value = e.target.value;
80 if (value - 0 === 0) {
81 e.target.value = '';
82 }
83 if (_this.props.onClickInput) _this.props.onClickInput(value, Object.getArgs(e, _this.props.args));
84 };
85
86 _this.onChange = function (e) {
87 if (e.target.validity.badInput) {
88 e.target.value = '';
89 }
90 // 输入时只校验最大值、小数点、最大长度、返回错误
91 var _this$props3 = _this.props,
92 max = _this$props3.max,
93 digits = _this$props3.digits,
94 maxLength = _this$props3.maxLength,
95 onError = _this$props3.onError;
96
97 var value = Math.Calc.correctNumber(e.target.value, { max: max, digits: digits, maxLength: maxLength, onError: onError });
98 if (_this.props.onChange) _this.props.onChange(value, Object.getArgs(e, _this.props.args));
99 };
100
101 _this.onClickMinus = function (e) {
102 var value = Math.Calc.correctNumber(Math.Calc.subtract(_this.$input.value, 1), _this.props);
103 // Callback
104 if (_this.props.onChange) _this.props.onChange(value, Object.getArgs(e, _this.props.args));
105 if (_this.props.onClickMinus) _this.props.onClickMinus(value, Object.getArgs(e, _this.props.args));
106 _this.autoFocus();
107 };
108
109 _this.onClickPlus = function (e) {
110 var value = Math.Calc.correctNumber(Math.Calc.add(_this.$input.value, 1), _this.props);
111 // Callback
112 if (_this.props.onChange) _this.props.onChange(value, Object.getArgs(e, _this.props.args));
113 if (_this.props.onClickPlus) _this.props.onClickPlus(value, Object.getArgs(e, _this.props.args));
114 _this.autoFocus();
115 };
116
117 _this.onClick = function (e) {
118 e.stopPropagation();
119 var _this$props4 = _this.props,
120 clear = _this$props4.clear,
121 onClick = _this$props4.onClick,
122 onClickLicon = _this$props4.onClickLicon,
123 onClickRicon = _this$props4.onClickRicon;
124
125 if (_this.props.disabled) return;
126 var target = e.target;
127 if (clear && target.classList.contains('clearicon')) {
128 _this.onClear(e);
129 }
130 if (onClickLicon && target.classList.contains('licon')) {
131 onClickLicon(_this.$input.value, Object.getArgs(e, _this.props.args));
132 return;
133 }
134 if (onClickRicon && target.classList.contains('ricon')) {
135 onClickLicon(_this.$input.value, Object.getArgs(e, _this.props.args));
136 return;
137 }
138 if (target.classList.contains('numbox-input')) {
139 _this.onClickInput(e);
140 return;
141 }
142 if (target.classList.contains('numbox-button-plus-flag')) {
143 _this.onClickPlus(e);
144 return;
145 }
146 if (target.classList.contains('numbox-button-minus-flag')) {
147 _this.onClickMinus(e);
148 return;
149 }
150 if (onClick) onClick(_this.$input.value, Object.getArgs(e, _this.props.args));
151 };
152
153 _this.onClear = function (e) {
154 _this.autoFocus();
155 // 赋值
156 if (_this.props.clear && typeof _this.props.clear === 'function') _this.props.clear('', Object.getArgs(e, _this.props.args));
157 if (_this.props.onChange) {
158 _this.props.onChange('', Object.getArgs(e, _this.props.args));
159 }
160 e.stopPropagation();
161 };
162
163 _this.getInputDOM = function () {
164 var _this$props5 = _this.props,
165 args = _this$props5.args,
166 style = _this$props5.style,
167 className = _this$props5.className,
168 disabled = _this$props5.disabled,
169 onClick = _this$props5.onClick,
170 plusStyle = _this$props5.plusStyle,
171 plusClassName = _this$props5.plusClassName,
172 minusStyle = _this$props5.minusStyle,
173 minusClassName = _this$props5.minusClassName,
174 onClickMinus = _this$props5.onClickMinus,
175 onClickPlus = _this$props5.onClickPlus,
176 licon = _this$props5.licon,
177 onClickLicon = _this$props5.onClickLicon,
178 ricon = _this$props5.ricon,
179 onClickRicon = _this$props5.onClickRicon,
180 clear = _this$props5.clear,
181 clearClassName = _this$props5.clearClassName,
182 clearStyle = _this$props5.clearStyle,
183 inputStyle = _this$props5.inputStyle,
184 inputClassName = _this$props5.inputClassName,
185 value = _this$props5.value,
186 placeholder = _this$props5.placeholder,
187 maxLength = _this$props5.maxLength,
188 readOnly = _this$props5.readOnly,
189 onClickInput = _this$props5.onClickInput,
190 onChange = _this$props5.onChange,
191 onError = _this$props5.onError,
192 onBlur = _this$props5.onBlur,
193 onFocus = _this$props5.onFocus,
194 digits = _this$props5.digits,
195 max = _this$props5.max,
196 min = _this$props5.min,
197 autoFocus = _this$props5.autoFocus,
198 autoSelect = _this$props5.autoSelect,
199 others = _objectWithoutProperties(_this$props5, ['args', 'style', 'className', 'disabled', 'onClick', 'plusStyle', 'plusClassName', 'minusStyle', 'minusClassName', 'onClickMinus', 'onClickPlus', 'licon', 'onClickLicon', 'ricon', 'onClickRicon', 'clear', 'clearClassName', 'clearStyle', 'inputStyle', 'inputClassName', 'value', 'placeholder', 'maxLength', 'readOnly', 'onClickInput', 'onChange', 'onError', 'onBlur', 'onFocus', 'digits', 'max', 'min', 'autoFocus', 'autoSelect']);
200
201 return React.createElement('input', _extends({
202 ref: function ref(el) {
203 _this.$input = el;
204 },
205 type: 'number',
206 value: value,
207 min: min,
208 max: max,
209 disabled: disabled,
210 readOnly: readOnly,
211 placeholder: placeholder,
212 onChange: _this.onChange,
213 onFocus: _this.onFocus,
214 onBlur: _this.onBlur,
215 className: 'numbox-input' + (inputClassName ? ' ' + inputClassName : ''),
216 style: inputStyle
217 }, others));
218 };
219
220 return _this;
221 }
222
223 NumBox.prototype.componentDidMount = function componentDidMount() {
224 if (this.props.autoFocus) {
225 this.autoFocus();
226 }
227 };
228 // 失去焦点
229
230 // 获取焦点
231
232 // 点击加减号清除时获取焦点
233
234 // 点击文本框, 逢0清空
235
236 // 修改值
237
238 // 点击减
239
240 // 点击加
241
242 // 点击容器
243
244 // 点击清除
245
246 // render
247
248
249 NumBox.prototype.render = function render() {
250 var _this2 = this;
251
252 var _props = this.props,
253 min = _props.min,
254 max = _props.max,
255 value = _props.value,
256 style = _props.style,
257 className = _props.className,
258 disabled = _props.disabled,
259 plusStyle = _props.plusStyle,
260 plusClassName = _props.plusClassName,
261 minusStyle = _props.minusStyle,
262 minusClassName = _props.minusClassName,
263 licon = _props.licon,
264 ricon = _props.ricon,
265 clear = _props.clear,
266 clearClassName = _props.clearClassName,
267 clearStyle = _props.clearStyle;
268
269 return React.createElement(
270 'div',
271 { ref: function ref(el) {
272 _this2.$el = el;
273 }, disabled: min >= max || disabled, style: style, className: 'numbox' + (className ? ' ' + className : ''), onClick: this.onClick },
274 React.createElement('input', {
275 ref: function ref(el) {
276 _this2.$minus = el;
277 },
278 type: 'button',
279 className: 'numbox-button numbox-button-minus-flag' + (plusClassName ? ' ' + plusClassName : ''),
280 style: plusStyle,
281 value: '-',
282 disabled: !isNaN(min) ? min - value >= 0 : false
283 }),
284 licon && licon,
285 this.getInputDOM(),
286 clear && value && React.createElement('i', { className: 'icon clearicon' + (clearClassName ? ' ' + clearClassName : ''), style: clearStyle, onClick: this.onClear }),
287 ricon && ricon,
288 React.createElement('input', {
289 ref: function ref(el) {
290 _this2.$plus = el;
291 },
292 type: 'button',
293 className: 'numbox-button numbox-button-plus-flag' + (minusClassName ? ' ' + minusClassName : ''),
294 style: minusStyle, value: '+',
295 disabled: !isNaN(max) ? max - value <= 0 : false
296 })
297 );
298 };
299
300 return NumBox;
301}(Component), _class.defaultProps = {
302 maxLength: '16',
303 clearClassName: 'ricon close-icon-clear size18'
304}, _temp);
305export { NumBox as default };
306NumBox.propTypes = process.env.NODE_ENV !== "production" ? {
307 args: PropTypes.any,
308 // 容器
309 style: PropTypes.object,
310 className: PropTypes.string,
311 disabled: PropTypes.bool,
312 // 加减号
313 plusStyle: PropTypes.object,
314 plusClassName: PropTypes.string,
315 minusStyle: PropTypes.object,
316 minusClassName: PropTypes.string,
317 // 文本框
318 inputStyle: PropTypes.object,
319 inputClassName: PropTypes.string,
320 value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
321 digits: PropTypes.oneOfType([PropTypes.bool, PropTypes.string, PropTypes.number]),
322 max: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
323 min: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
324 placeholder: PropTypes.string,
325 name: PropTypes.string,
326 maxLength: PropTypes.string,
327 readOnly: PropTypes.bool,
328 required: PropTypes.bool,
329 // 自动获取焦点
330 autoFocus: PropTypes.bool,
331 autoSelect: PropTypes.bool,
332 // 左右图标
333 licon: PropTypes.node,
334 onClickLicon: PropTypes.func,
335 ricon: PropTypes.node,
336 onClickRicon: PropTypes.func,
337 // 清除按键
338 clear: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
339 clearClassName: PropTypes.string,
340 clearStyle: PropTypes.object,
341 // events
342 onClick: PropTypes.func,
343 onClickMinus: PropTypes.func,
344 onClickPlus: PropTypes.func,
345 onClickInput: PropTypes.func,
346 onChange: PropTypes.func,
347 onBlur: PropTypes.func,
348 onFocus: PropTypes.func,
349 onError: PropTypes.func
350} : {};
\No newline at end of file