1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | 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; };
|
8 |
|
9 | var _react = require('react');
|
10 |
|
11 | var _react2 = _interopRequireDefault(_react);
|
12 |
|
13 | var _classnames = require('classnames');
|
14 |
|
15 | var _classnames2 = _interopRequireDefault(_classnames);
|
16 |
|
17 | var _beeInputGroup = require('bee-input-group');
|
18 |
|
19 | var _beeInputGroup2 = _interopRequireDefault(_beeInputGroup);
|
20 |
|
21 | var _beeFormControl = require('bee-form-control');
|
22 |
|
23 | var _beeFormControl2 = _interopRequireDefault(_beeFormControl);
|
24 |
|
25 | var _beeMessage = require('bee-message');
|
26 |
|
27 | var _beeMessage2 = _interopRequireDefault(_beeMessage);
|
28 |
|
29 | var _propTypes = require('prop-types');
|
30 |
|
31 | var _propTypes2 = _interopRequireDefault(_propTypes);
|
32 |
|
33 | var _i18n = require('./i18n');
|
34 |
|
35 | var _i18n2 = _interopRequireDefault(_i18n);
|
36 |
|
37 | var _tool = require('bee-locale/build/tool');
|
38 |
|
39 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
40 |
|
41 | function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
|
42 |
|
43 | 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; }
|
44 |
|
45 | 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; }
|
46 |
|
47 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
48 |
|
49 | 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; }
|
50 |
|
51 | 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) : _defaults(subClass, superClass); }
|
52 |
|
53 | var propTypes = {
|
54 | max: _propTypes2["default"].number,
|
55 | min: _propTypes2["default"].number,
|
56 | step: _propTypes2["default"].number,
|
57 | autoWidth: _propTypes2["default"].bool,
|
58 | precision: _propTypes2["default"].number,
|
59 | format: _propTypes2["default"].func,
|
60 | delay: _propTypes2["default"].number,
|
61 | disabled: _propTypes2["default"].bool,
|
62 | toThousands: _propTypes2["default"].bool,
|
63 | locale: _propTypes2["default"].object,
|
64 | toNumber: _propTypes2["default"].bool,
|
65 | displayCheckPrompt: _propTypes2["default"].bool,
|
66 | minusRight: _propTypes2["default"].bool,
|
67 | handleBtnClick: _propTypes2["default"].func
|
68 | };
|
69 |
|
70 | var defaultProps = {
|
71 | value: "",
|
72 | step: 1,
|
73 | clsPrefix: 'u-input-number',
|
74 | iconStyle: 'double',
|
75 | autoWidth: false,
|
76 | delay: 300,
|
77 | toNumber: false,
|
78 | displayCheckPrompt: false,
|
79 | locale: {},
|
80 | handleBtnClick: function handleBtnClick() {}
|
81 | };
|
82 |
|
83 |
|
84 | function prompt(content) {
|
85 | _beeMessage2["default"].destroy();
|
86 | _beeMessage2["default"].create({ content: content, color: 'warninglight' });
|
87 | }
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | function toThousands(number) {
|
94 | if (number === '') return '';
|
95 | if (number === '0') return '0';
|
96 | var num = (number || 0).toString();
|
97 | var integer = num.split('.')[0];
|
98 | var decimal = num.split('.')[1] || '';
|
99 | var result = '';
|
100 | while (integer.length > 3) {
|
101 | result = ',' + integer.slice(-3) + result;
|
102 | integer = integer.slice(0, integer.length - 3);
|
103 | }
|
104 | if (integer) {
|
105 | result = integer + result;
|
106 | if (num == '.' || num.indexOf('.') == num.length - 1) {
|
107 | result = result + '.' + decimal;
|
108 | } else if (decimal) {
|
109 | result = result + '.' + decimal;
|
110 | }
|
111 | }
|
112 | if (result[0] == '-') {
|
113 | result = result.replace('-,', '-');
|
114 | }
|
115 | return result;
|
116 | }
|
117 |
|
118 | function setCaretPosition(ctrl, pos, need) {
|
119 |
|
120 | if (ctrl && need) {
|
121 | if (ctrl.setSelectionRange) {
|
122 | ctrl.focus();
|
123 | ctrl.setSelectionRange(pos, pos);
|
124 |
|
125 | } else if (ctrl.createTextRange) {
|
126 | var range = ctrl.createTextRange();
|
127 | range.collapse(true);
|
128 | range.moveEnd('character', pos);
|
129 | range.moveStart('character', pos);
|
130 | range.select();
|
131 | }
|
132 | }
|
133 | }
|
134 |
|
135 | var InputNumber = function (_Component) {
|
136 | _inherits(InputNumber, _Component);
|
137 |
|
138 | function InputNumber(props) {
|
139 | _classCallCheck(this, InputNumber);
|
140 |
|
141 |
|
142 |
|
143 | var _this = _possibleConstructorReturn(this, _Component.call(this, props));
|
144 |
|
145 | _initialiseProps.call(_this);
|
146 |
|
147 | var data = _this.judgeValue(props);
|
148 | _this.state = {
|
149 | value: data.value,
|
150 | minusDisabled: data.minusDisabled,
|
151 | plusDisabled: data.plusDisabled,
|
152 | showValue: toThousands(data.value)
|
153 | };
|
154 |
|
155 | _this.timer = null;
|
156 | _this.focus = false;
|
157 | _this.selectionStart = 0;
|
158 | return _this;
|
159 | }
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | |
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | InputNumber.prototype.componentDidMount = function componentDidMount() {
|
176 | this.setState({
|
177 | value: this.props.value,
|
178 | showValue: toThousands(this.props.value)
|
179 | });
|
180 | };
|
181 |
|
182 | InputNumber.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
|
183 | if (this.focus) {
|
184 | if (nextProps.value == Infinity || nextProps.value == -Infinity) {} else {
|
185 | this.setState({
|
186 | value: nextProps.value,
|
187 | showValue: toThousands(nextProps.value)
|
188 | });
|
189 | }
|
190 | } else {
|
191 | var data = this.judgeValue(nextProps, this.state.value);
|
192 | this.setState({
|
193 | value: data.value,
|
194 | showValue: toThousands(data.value),
|
195 | minusDisabled: data.minusDisabled,
|
196 | plusDisabled: data.plusDisabled
|
197 | });
|
198 | }
|
199 | };
|
200 |
|
201 | InputNumber.prototype.ComponentWillUnMount = function ComponentWillUnMount() {
|
202 | this.clear();
|
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 | InputNumber.prototype.render = function render() {
|
235 | var _classes,
|
236 | _this2 = this;
|
237 |
|
238 | var _props = this.props,
|
239 | toThousands = _props.toThousands,
|
240 | minusRight = _props.minusRight,
|
241 | max = _props.max,
|
242 | min = _props.min,
|
243 | step = _props.step,
|
244 | disabled = _props.disabled,
|
245 | clsPrefix = _props.clsPrefix,
|
246 | className = _props.className,
|
247 | delay = _props.delay,
|
248 | onBlur = _props.onBlur,
|
249 | onFocus = _props.onFocus,
|
250 | iconStyle = _props.iconStyle,
|
251 | autoWidth = _props.autoWidth,
|
252 | onChange = _props.onChange,
|
253 | format = _props.format,
|
254 | precision = _props.precision,
|
255 | toNumber = _props.toNumber,
|
256 | others = _objectWithoutProperties(_props, ['toThousands', 'minusRight', 'max', 'min', 'step', 'disabled', 'clsPrefix', 'className', 'delay', 'onBlur', 'onFocus', 'iconStyle', 'autoWidth', 'onChange', 'format', 'precision', 'toNumber']);
|
257 |
|
258 | var classes = (_classes = {}, _defineProperty(_classes, clsPrefix + '-auto', autoWidth), _defineProperty(_classes, '' + clsPrefix, true), _defineProperty(_classes, clsPrefix + '-lg', others.size === "lg"), _defineProperty(_classes, clsPrefix + '-sm', others.size === "sm"), _classes);
|
259 |
|
260 | var _state = this.state,
|
261 | value = _state.value,
|
262 | minusDisabled = _state.minusDisabled,
|
263 | plusDisabled = _state.plusDisabled,
|
264 | showValue = _state.showValue;
|
265 |
|
266 | value = precision != null && !this.focus ? this.getPrecision(value) : value;
|
267 | value = format && !this.focus ? format(value) : value;
|
268 | value = String(value).indexOf("e") !== -1 ? this.getFullNum(value) : value;
|
269 | if (minusRight && String(value).indexOf('-') != -1) {
|
270 | value = String(value).replace("-", "") + "-";
|
271 | }
|
272 | var disabledCursor = disabled ? ' disabled-cursor' : '';
|
273 | var disabledCon = disabled ? ' disabled-con' : '';
|
274 | return _react2["default"].createElement(
|
275 | 'div',
|
276 | { className: clsPrefix + '-out' },
|
277 | iconStyle === 'double' ? _react2["default"].createElement(
|
278 | _beeInputGroup2["default"],
|
279 | { className: (0, _classnames2["default"])(className, classes, disabledCon) },
|
280 | _react2["default"].createElement(
|
281 | _beeInputGroup2["default"].Addon,
|
282 | {
|
283 |
|
284 | className: (minusDisabled && 'disabled') + disabledCursor,
|
285 | onMouseDown: this.handleReduceMouseDown,
|
286 | onMouseLeave: this.clear,
|
287 | onMouseUp: this.clear },
|
288 | '-'
|
289 | ),
|
290 | _react2["default"].createElement(_beeFormControl2["default"], _extends({}, others, {
|
291 | value: toThousands ? showValue : value,
|
292 | disabled: disabled,
|
293 | onBlur: this.handleBlur,
|
294 | onFocus: this.handleFocus,
|
295 | onChange: this.handleChange,
|
296 | ref: function ref(_ref) {
|
297 | return _this2.input = _ref;
|
298 | }
|
299 | })),
|
300 | _react2["default"].createElement(
|
301 | _beeInputGroup2["default"].Addon,
|
302 | {
|
303 |
|
304 | className: (plusDisabled && 'disabled') + disabledCursor,
|
305 | onMouseDown: this.handlePlusMouseDown,
|
306 | onMouseLeave: this.clear,
|
307 | onMouseUp: this.clear },
|
308 | '+'
|
309 | )
|
310 | ) : _react2["default"].createElement(
|
311 | _beeInputGroup2["default"],
|
312 | {
|
313 | className: (0, _classnames2["default"])(className, classes, disabledCon),
|
314 | simple: true
|
315 | },
|
316 | _react2["default"].createElement(_beeFormControl2["default"], _extends({}, others, {
|
317 | value: toThousands ? showValue : value,
|
318 | disabled: disabled,
|
319 | onBlur: this.handleBlur,
|
320 | onFocus: this.handleFocus,
|
321 | onChange: this.handleChange,
|
322 | ref: function ref(_ref2) {
|
323 | return _this2.input = _ref2;
|
324 | }
|
325 | })),
|
326 | _react2["default"].createElement(
|
327 | _beeInputGroup2["default"].Button,
|
328 | null,
|
329 | _react2["default"].createElement(
|
330 | 'div',
|
331 | { className: (0, _classnames2["default"])("icon-group") },
|
332 | _react2["default"].createElement(
|
333 | 'span',
|
334 | {
|
335 |
|
336 | onMouseDown: this.handlePlusMouseDown,
|
337 | onMouseLeave: this.clear,
|
338 | onMouseUp: this.clear,
|
339 | className: (0, _classnames2["default"])('plus', { 'disabled': plusDisabled, 'disabled-cursor': disabledCursor }) },
|
340 | _react2["default"].createElement('span', { className: 'uf uf-arrow-up' })
|
341 | ),
|
342 | _react2["default"].createElement(
|
343 | 'span',
|
344 | {
|
345 |
|
346 | onMouseDown: this.handleReduceMouseDown,
|
347 | onMouseLeave: this.clear,
|
348 | onMouseUp: this.clear,
|
349 | className: (0, _classnames2["default"])("reduce", { 'disabled': minusDisabled, 'disabled-cursor': disabledCursor }) },
|
350 | _react2["default"].createElement('span', { className: ' uf uf-arrow-down' })
|
351 | )
|
352 | )
|
353 | )
|
354 | )
|
355 | );
|
356 | };
|
357 |
|
358 | return InputNumber;
|
359 | }(_react.Component);
|
360 |
|
361 | var _initialiseProps = function _initialiseProps() {
|
362 | var _this3 = this;
|
363 |
|
364 | this.judgeValue = function (props, oldValue) {
|
365 | var currentValue = void 0;
|
366 | var currentMinusDisabled = false;
|
367 | var currentPlusDisabled = false;
|
368 | var value = props.value,
|
369 | min = props.min,
|
370 | max = props.max,
|
371 | precision = props.precision,
|
372 | onChange = props.onChange,
|
373 | displayCheckPrompt = props.displayCheckPrompt;
|
374 |
|
375 | if (props.minusRight) {
|
376 | value = value.toString();
|
377 | if (value.indexOf('-') != -1) {
|
378 |
|
379 | value = value.replace('-', '');
|
380 | value = '-' + value;
|
381 | }
|
382 | value = Number(value);
|
383 | }
|
384 | if (value != undefined && value != null) {
|
385 | if (value === '') {
|
386 | currentValue = '';
|
387 | return {
|
388 | value: '',
|
389 | minusDisabled: false,
|
390 | plusDisabled: false
|
391 | };
|
392 | } else {
|
393 | currentValue = Number(value) || 0;
|
394 | }
|
395 | }
|
396 |
|
397 |
|
398 | else if (value === '0' || value === 0) {
|
399 | currentValue = 0;
|
400 | } else {
|
401 |
|
402 | if (oldValue || oldValue === 0 || oldValue === '0') {
|
403 | currentValue = oldValue;
|
404 | } else {
|
405 |
|
406 | return {
|
407 | value: '',
|
408 | minusDisabled: false,
|
409 | plusDisabled: false
|
410 | };
|
411 | }
|
412 | }
|
413 | if (currentValue == -Infinity) {
|
414 | return {
|
415 | value: min,
|
416 | minusDisabled: true,
|
417 | plusDisabled: false
|
418 | };
|
419 | }
|
420 | if (currentValue == Infinity) {
|
421 | return {
|
422 | value: max,
|
423 | minusDisabled: false,
|
424 | plusDisabled: true
|
425 | };
|
426 | }
|
427 | var local = (0, _tool.getComponentLocale)(props, _this3.context, 'InputNumber', function () {
|
428 | return _i18n2["default"];
|
429 | });
|
430 | if (currentValue <= min) {
|
431 | if (displayCheckPrompt) prompt(local['msgMin']);
|
432 | currentMinusDisabled = true;
|
433 | currentValue = min;
|
434 | }
|
435 | if (currentValue >= max) {
|
436 | if (displayCheckPrompt) prompt(local['msgMax']);
|
437 | currentPlusDisabled = true;
|
438 | currentValue = max;
|
439 | }
|
440 |
|
441 | if (props.hasOwnProperty('precision')) {
|
442 |
|
443 | currentValue = _this3.getPrecision(currentValue);
|
444 | }
|
445 | if (props.minusRight) {
|
446 | currentValue = currentValue.toString();
|
447 | if (currentValue.indexOf('-') != -1) {
|
448 |
|
449 | currentValue = currentValue.replace('-', '');
|
450 | currentValue = currentValue + '-';
|
451 | }
|
452 | }
|
453 |
|
454 | return {
|
455 | value: currentValue,
|
456 | minusDisabled: currentMinusDisabled,
|
457 | plusDisabled: currentPlusDisabled
|
458 | };
|
459 | };
|
460 |
|
461 | this.numToFixed = function (value, fixed, type) {
|
462 | value = String(value);
|
463 | if (!value && value !== "0") return value;
|
464 | if (!fixed && String(fixed) !== "0") return value;
|
465 | var preIndex = value.indexOf(".");
|
466 | if (value.indexOf(".") === -1) return value;
|
467 | preIndex++;
|
468 | var endIndex = preIndex + fixed;
|
469 | var precValue = value.substr(preIndex, endIndex) + "0000000000";
|
470 | if (type) {
|
471 | return Number(value).toFixed(fixed);
|
472 | }
|
473 | return value.split(".")[0] + "." + precValue.substr(0, fixed);
|
474 | };
|
475 |
|
476 | this.handleChange = function (value) {
|
477 | var selectionStart = _this3.input.selectionStart == undefined ? _this3.input.input.selectionStart : _this3.input.selectionStart;
|
478 | _this3.selectionStart = selectionStart;
|
479 | var _props2 = _this3.props,
|
480 | onChange = _props2.onChange,
|
481 | toNumber = _props2.toNumber,
|
482 | minusRight = _props2.minusRight;
|
483 |
|
484 | if (value === '') {
|
485 | onChange && onChange(value);
|
486 | _this3.setState({
|
487 | value: value,
|
488 | showValue: ''
|
489 | });
|
490 | return;
|
491 | }
|
492 |
|
493 | if (minusRight) {
|
494 | if (value.match(/-/g) && value.match(/-/g).length > 1) return;
|
495 | }
|
496 | if (isNaN(value) && value !== '.' && value !== '-') return;
|
497 | if (value.indexOf(".") !== -1) {
|
498 |
|
499 | var prec = String(value.split(".")[1]).replace("-", "");
|
500 | if (_this3.props.precision === 0 && (prec === "" || prec != "")) return;
|
501 | if (_this3.props.precision && prec.length > _this3.props.precision) return;
|
502 | if (prec.length > 8) return;
|
503 | }
|
504 | _this3.setState({
|
505 | value: value,
|
506 | showValue: toThousands(value)
|
507 | });
|
508 | if (value === '-') {
|
509 | onChange && onChange(value);
|
510 | } else if (value == '.' || value.indexOf('.') == value.length - 1) {
|
511 |
|
512 | onChange && onChange(value);
|
513 | } else if (value[value.indexOf('.') + 1] == 0) {
|
514 |
|
515 | onChange && onChange(value);
|
516 | } else {
|
517 | toNumber ? onChange && onChange(Number(value)) : onChange && onChange(value);
|
518 | }
|
519 | if (_this3.props.toThousands) {
|
520 | var stateShowValue = toThousands(_this3.state.value);
|
521 | var showValue = toThousands(value);
|
522 | var addNumber = 0;
|
523 | var delNumber = 0;
|
524 | var reg = /[0-9]/;
|
525 | for (var i = 0; i < selectionStart; i++) {
|
526 | if (!reg.test(showValue[i])) addNumber += 1;
|
527 | }
|
528 | for (var j = 0; j < selectionStart; j++) {
|
529 | if (stateShowValue[j]) {
|
530 | if (!reg.test(stateShowValue[j])) delNumber += 1;
|
531 | }
|
532 | }
|
533 | var position = selectionStart + addNumber - delNumber;
|
534 | setCaretPosition(_this3.input && _this3.input.input, position, true);
|
535 | }
|
536 | };
|
537 |
|
538 | this.handleFocus = function (value, e) {
|
539 | _this3.focus = true;
|
540 | var _props3 = _this3.props,
|
541 | onFocus = _props3.onFocus,
|
542 | min = _props3.min,
|
543 | max = _props3.max;
|
544 |
|
545 | onFocus && onFocus(_this3.getPrecision(_this3.state.value), e);
|
546 | };
|
547 |
|
548 | this.getFullNum = function (num) {
|
549 |
|
550 | if (isNaN(num)) {
|
551 | return num;
|
552 | };
|
553 |
|
554 |
|
555 | var str = '' + num;
|
556 | if (!/e/i.test(str)) {
|
557 | return num;
|
558 | };
|
559 | var _precision = _this3.props.precision ? _this3.props.precision : 18;
|
560 | return Number(num).toFixed(_precision).replace(/\.?0+$/, "");
|
561 | };
|
562 |
|
563 | this.handleBlur = function (v, e) {
|
564 | _this3.focus = false;
|
565 | var _props4 = _this3.props,
|
566 | onBlur = _props4.onBlur,
|
567 | precision = _props4.precision,
|
568 | onChange = _props4.onChange,
|
569 | toNumber = _props4.toNumber,
|
570 | max = _props4.max,
|
571 | min = _props4.min,
|
572 | displayCheckPrompt = _props4.displayCheckPrompt,
|
573 | minusRight = _props4.minusRight,
|
574 | round = _props4.round;
|
575 |
|
576 | var local = (0, _tool.getComponentLocale)(_this3.props, _this3.context, 'InputNumber', function () {
|
577 | return _i18n2["default"];
|
578 | });
|
579 | v = _this3.state.value;
|
580 | if (v === '' || !v) {
|
581 | _this3.setState({
|
582 | value: v
|
583 | });
|
584 | onChange && onChange(v);
|
585 | onBlur && onBlur(v, e);
|
586 | return;
|
587 | }
|
588 |
|
589 | var value = _this3.numToFixed(v, precision, round);
|
590 | if (minusRight) {
|
591 | if (value.indexOf('-') != -1) {
|
592 |
|
593 | value = value.replace('-', '');
|
594 | value = '-' + value;
|
595 | }
|
596 | }
|
597 | value = isNaN(Number(value)) ? 0 : Number(value);
|
598 | if (value > max) {
|
599 | if (displayCheckPrompt) prompt(local['msgMax']);
|
600 | value = max;
|
601 | }
|
602 | if (value < min) {
|
603 | if (displayCheckPrompt) prompt(local['msgMin']);
|
604 | value = min;
|
605 | }
|
606 | if (_this3.props.hasOwnProperty('precision')) {
|
607 |
|
608 | value = _this3.getPrecision(value);
|
609 | }
|
610 | value = value.toString();
|
611 | if (minusRight && value.indexOf('-') != -1) {
|
612 |
|
613 | value = value.replace('-', '');
|
614 | value = value + '-';
|
615 | }
|
616 | _this3.setState({
|
617 | value: value,
|
618 | showValue: toThousands(value)
|
619 | });
|
620 | _this3.detailDisable(value);
|
621 | if (toNumber && !minusRight) {
|
622 | onChange && onChange(value);
|
623 | onBlur && onBlur(value, e);
|
624 | } else {
|
625 | onChange && onChange(value);
|
626 | onBlur && onBlur(value, e);
|
627 | }
|
628 | };
|
629 |
|
630 | this.detailDisable = function (value) {
|
631 | var _props5 = _this3.props,
|
632 | max = _props5.max,
|
633 | min = _props5.min,
|
634 | step = _props5.step;
|
635 |
|
636 |
|
637 | if (value >= max || Number(value) + Number(step) > max) {
|
638 | _this3.setState({
|
639 | plusDisabled: true
|
640 | });
|
641 | } else {
|
642 | _this3.setState({
|
643 | plusDisabled: false
|
644 | });
|
645 | }
|
646 | if (value <= min || value - step < min) {
|
647 | _this3.setState({
|
648 | minusDisabled: true
|
649 | });
|
650 | } else {
|
651 | _this3.setState({
|
652 | minusDisabled: false
|
653 | });
|
654 | }
|
655 | };
|
656 |
|
657 | this.minus = function (value) {
|
658 | var _props6 = _this3.props,
|
659 | min = _props6.min,
|
660 | max = _props6.max,
|
661 | step = _props6.step,
|
662 | onChange = _props6.onChange,
|
663 | toNumber = _props6.toNumber;
|
664 |
|
665 | value = value === '-' ? 0 : value;
|
666 | if (typeof min === "undefined") {
|
667 | value = _this3.detail(value, step, 'reduce');
|
668 | } else {
|
669 | if (value < min) {
|
670 | value = min;
|
671 | } else {
|
672 | var reducedValue = _this3.detail(value, step, 'reduce');
|
673 | if (reducedValue >= min) {
|
674 | value = reducedValue;
|
675 | }
|
676 | }
|
677 | }
|
678 |
|
679 | if (value > max) {
|
680 | value = max;
|
681 | }
|
682 |
|
683 | _this3.setState({
|
684 | value: value,
|
685 | showValue: toThousands(value)
|
686 | }, function () {
|
687 | _this3.input.input.focus && _this3.input.input.focus();
|
688 | });
|
689 | toNumber ? onChange && onChange(Number(value)) : onChange && onChange(value);
|
690 | _this3.handleBtnClick('down', value);
|
691 | _this3.detailDisable(value);
|
692 | };
|
693 |
|
694 | this.plus = function (value) {
|
695 | var _props7 = _this3.props,
|
696 | max = _props7.max,
|
697 | min = _props7.min,
|
698 | step = _props7.step,
|
699 | onChange = _props7.onChange,
|
700 | toNumber = _props7.toNumber;
|
701 |
|
702 | value = value === '-' ? 0 : value;
|
703 | if (typeof max === "undefined") {
|
704 | value = _this3.detail(value, step, 'add');
|
705 | } else {
|
706 | if (value > max) {
|
707 | value = max;
|
708 | } else {
|
709 | var addedValue = _this3.detail(value, step, 'add');
|
710 | if (addedValue <= max) {
|
711 | value = addedValue;
|
712 | }
|
713 | }
|
714 | }
|
715 | if (value < min) {
|
716 | value = min;
|
717 | }
|
718 | _this3.setState({
|
719 | value: value,
|
720 | showValue: toThousands(value)
|
721 | }, function () {
|
722 | _this3.input.input.focus && _this3.input.input.focus();
|
723 | });
|
724 | toNumber ? onChange && onChange(Number(value)) : onChange && onChange(value);
|
725 | _this3.handleBtnClick('up', value);
|
726 | _this3.detailDisable(value);
|
727 | };
|
728 |
|
729 | this.detail = function (value, step, type) {
|
730 | var precision = _this3.props.precision;
|
731 |
|
732 |
|
733 | var valueFloat = _this3.separate(value);
|
734 | var stepFloat = _this3.separate(step);
|
735 |
|
736 | var ans = void 0;
|
737 | var stepFloatLength = stepFloat.toString().length;
|
738 | var valueFloatLength = valueFloat.toString().length;
|
739 |
|
740 | if (typeof precision === 'undefined') {
|
741 | precision = Math.max(stepFloatLength, valueFloatLength);
|
742 | }
|
743 | var coefficient = Math.pow(10, Math.abs(stepFloatLength - valueFloatLength));
|
744 | if (type === 'add') {
|
745 | ans = (value * coefficient + step * coefficient) / coefficient;
|
746 | } else {
|
747 | ans = (value * coefficient - step * coefficient) / coefficient;
|
748 | }
|
749 |
|
750 | return ans.toFixed(precision);
|
751 | };
|
752 |
|
753 | this.separate = function (value) {
|
754 | if (value == null || value == undefined) {
|
755 | return "";
|
756 | } else {
|
757 | value = value.toString();
|
758 | if (value.indexOf('.') > -1) {
|
759 | return value.split('.')[1];
|
760 | } else {
|
761 | return "";
|
762 | }
|
763 | }
|
764 | };
|
765 |
|
766 | this.clear = function () {
|
767 | if (_this3.timer) {
|
768 | clearTimeout(_this3.timer);
|
769 | }
|
770 | };
|
771 |
|
772 | this.handlePlusMouseDown = function (e) {
|
773 | e.preventDefault && e.preventDefault();
|
774 | var _props8 = _this3.props,
|
775 | delay = _props8.delay,
|
776 | disabled = _props8.disabled;
|
777 | var value = _this3.state.value;
|
778 |
|
779 | if (disabled) return;
|
780 | _this3.plus(value);
|
781 | _this3.clear();
|
782 | _this3.timer = setTimeout(function () {
|
783 | _this3.handlePlusMouseDown(e);
|
784 | }, delay);
|
785 | };
|
786 |
|
787 | this.handleReduceMouseDown = function (e) {
|
788 | e.preventDefault && e.preventDefault();
|
789 | var _props9 = _this3.props,
|
790 | delay = _props9.delay,
|
791 | disabled = _props9.disabled;
|
792 | var value = _this3.state.value;
|
793 |
|
794 | if (disabled) return;
|
795 | _this3.minus(value);
|
796 | _this3.clear();
|
797 | _this3.timer = setTimeout(function () {
|
798 | _this3.handleReduceMouseDown(e);
|
799 | }, delay);
|
800 | };
|
801 |
|
802 | this.getPrecision = function (value) {
|
803 | if (value == null || value == undefined) return value;
|
804 | if (!value && value === "") return value;
|
805 | value = String(value);
|
806 | value = value.indexOf("e") !== -1 ? _this3.getFullNum(value) : value;
|
807 | var precision = _this3.props.precision;
|
808 |
|
809 | if (precision === 0) return value;
|
810 | if (precision == undefined || value.indexOf(".") !== -1 && String(value.split(".")[1]).length === precision) {
|
811 | return value;
|
812 | }
|
813 | var before = value.substring(0, 1),
|
814 | len = value.length,
|
815 | after = value.substring(len - 1, len);
|
816 | before = before === "-" ? before : "";
|
817 | after = after === "-" ? after : "";
|
818 |
|
819 | if (before) value = value.substring(1, len - 1);
|
820 | if (after) value = value.substring(0, len - 1);
|
821 |
|
822 | var precV = "000000000000000000000000000000000000000000000000000000000000000000000000";
|
823 | if (value.indexOf(".") === -1) {
|
824 | precV = precV.substr(0, precision);
|
825 | precV = precV ? "." + precV : precV;
|
826 | if (!isNaN(value) && (value.indexOf('-') != -1 || value.indexOf('+') != -1) && value.indexOf('e') != -1) {
|
827 |
|
828 | } else {
|
829 | value = value + precV;
|
830 | }
|
831 | }
|
832 | return before + Number(value).toFixed(precision) + after;
|
833 | };
|
834 |
|
835 | this.handleBtnClick = function (type, value) {
|
836 | _this3.props.handleBtnClick(type, value);
|
837 | };
|
838 | };
|
839 |
|
840 | ;
|
841 |
|
842 | InputNumber.defaultProps = defaultProps;
|
843 | InputNumber.propTypes = propTypes;
|
844 | InputNumber.contextTypes = {
|
845 | beeLocale: _propTypes2["default"].object
|
846 | };
|
847 | exports["default"] = InputNumber;
|
848 | module.exports = exports['default']; |
\ | No newline at end of file |