UNPKG

7.93 kBJavaScriptView Raw
1import _defineProperty from 'babel-runtime/helpers/defineProperty';
2import _extends from 'babel-runtime/helpers/extends';
3import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
4import _createClass from 'babel-runtime/helpers/createClass';
5import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
6import _inherits from 'babel-runtime/helpers/inherits';
7import React from 'react';
8import classNames from 'classnames';
9import BaseComponent from './base';
10import InputHandler from './InputHandler';
11function noop() {}
12function preventDefault(e) {
13 e.preventDefault();
14}
15
16var InputNumber = function (_BaseComponent) {
17 _inherits(InputNumber, _BaseComponent);
18
19 function InputNumber() {
20 _classCallCheck(this, InputNumber);
21
22 var _this = _possibleConstructorReturn(this, (InputNumber.__proto__ || Object.getPrototypeOf(InputNumber)).apply(this, arguments));
23
24 _this.setInput = function (input) {
25 _this.input = input;
26 };
27 return _this;
28 }
29
30 _createClass(InputNumber, [{
31 key: 'componentDidMount',
32 value: function componentDidMount() {
33 this.componentDidUpdate();
34 }
35 }, {
36 key: 'componentWillUpdate',
37 value: function componentWillUpdate() {
38 try {
39 this.start = this.input.selectionStart;
40 this.end = this.input.selectionEnd;
41 } catch (e) {
42 // Fix error in Chrome:
43 // Failed to read the 'selectionStart' property from 'HTMLInputElement'
44 // http://stackoverflow.com/q/21177489/3040605
45 }
46 }
47 }, {
48 key: 'componentDidUpdate',
49 value: function componentDidUpdate() {
50 if (this.props.focusOnUpDown && this.state.focused) {
51 var selectionRange = this.input.setSelectionRange;
52 if (selectionRange && typeof selectionRange === 'function' && this.start !== undefined && this.end !== undefined && this.start !== this.end) {
53 this.input.setSelectionRange(this.start, this.end);
54 } else {
55 this.focus();
56 }
57 }
58 }
59 }, {
60 key: 'getRatio',
61 value: function getRatio(e) {
62 var ratio = 1;
63 if (e.metaKey || e.ctrlKey) {
64 ratio = 0.1;
65 } else if (e.shiftKey) {
66 ratio = 10;
67 }
68 return ratio;
69 }
70 }, {
71 key: 'getValueFromEvent',
72 value: function getValueFromEvent(e) {
73 return e.target.value;
74 }
75 }, {
76 key: 'focus',
77 value: function focus() {
78 this.input.focus();
79 }
80 }, {
81 key: 'formatWrapper',
82 value: function formatWrapper(num) {
83 if (this.props.formatter) {
84 return this.props.formatter(num);
85 }
86 return num;
87 }
88 }, {
89 key: 'render',
90 value: function render() {
91 var _classNames;
92
93 var props = _extends({}, this.props);
94 var _props$prefixCls = props.prefixCls,
95 prefixCls = _props$prefixCls === undefined ? '' : _props$prefixCls,
96 disabled = props.disabled,
97 readOnly = props.readOnly,
98 max = props.max,
99 min = props.min;
100
101 var classes = classNames((_classNames = {}, _defineProperty(_classNames, prefixCls, true), _defineProperty(_classNames, props.className, !!props.className), _defineProperty(_classNames, prefixCls + '-disabled', disabled), _defineProperty(_classNames, prefixCls + '-focused', this.state.focused), _classNames));
102 var upDisabledClass = '';
103 var downDisabledClass = '';
104 var value = this.state.value;
105
106 if (value || value === 0) {
107 if (!isNaN(value)) {
108 var val = Number(value);
109 if (val >= max) {
110 upDisabledClass = prefixCls + '-handler-up-disabled';
111 }
112 if (val <= min) {
113 downDisabledClass = prefixCls + '-handler-down-disabled';
114 }
115 } else {
116 upDisabledClass = prefixCls + '-handler-up-disabled';
117 downDisabledClass = prefixCls + '-handler-down-disabled';
118 }
119 }
120 var editable = !props.readOnly && !props.disabled;
121 // focus state, show input value
122 // unfocus state, show valid value
123 var inputDisplayValue = void 0;
124 if (this.state.focused) {
125 inputDisplayValue = this.state.inputValue;
126 } else {
127 inputDisplayValue = this.toPrecisionAsStep(this.state.value);
128 }
129 if (inputDisplayValue === undefined || inputDisplayValue === null) {
130 inputDisplayValue = '';
131 }
132 var upEvents = void 0;
133 var downEvents = void 0;
134 upEvents = {
135 onTouchStart: editable && !upDisabledClass ? this.up : noop,
136 onTouchEnd: this.stop
137 };
138 downEvents = {
139 onTouchStart: editable && !downDisabledClass ? this.down : noop,
140 onTouchEnd: this.stop
141 };
142 var inputDisplayValueFormat = this.formatWrapper(inputDisplayValue);
143 var isUpDisabled = !!upDisabledClass || disabled || readOnly;
144 var isDownDisabled = !!downDisabledClass || disabled || readOnly;
145 return React.createElement(
146 'div',
147 { className: classes, style: props.style },
148 React.createElement(
149 'div',
150 { className: prefixCls + '-handler-wrap' },
151 React.createElement(
152 InputHandler,
153 _extends({ disabled: isUpDisabled, prefixCls: prefixCls, unselectable: 'unselectable' }, upEvents, { role: 'button', 'aria-label': 'Increase Value', 'aria-disabled': !!isUpDisabled, className: prefixCls + '-handler ' + prefixCls + '-handler-up ' + upDisabledClass }),
154 this.props.upHandler || React.createElement('span', { unselectable: 'unselectable', className: prefixCls + '-handler-up-inner', onClick: preventDefault })
155 ),
156 React.createElement(
157 InputHandler,
158 _extends({ disabled: isDownDisabled, prefixCls: prefixCls, unselectable: 'unselectable' }, downEvents, { role: 'button', 'aria-label': 'Decrease Value', 'aria-disabled': !!isDownDisabled, className: prefixCls + '-handler ' + prefixCls + '-handler-down ' + downDisabledClass }),
159 this.props.downHandler || React.createElement('span', { unselectable: 'unselectable', className: prefixCls + '-handler-down-inner', onClick: preventDefault })
160 )
161 ),
162 React.createElement(
163 'div',
164 { className: prefixCls + '-input-wrap', role: 'spinbutton', 'aria-valuemin': props.min, 'aria-valuemax': props.max, 'aria-valuenow': value },
165 React.createElement('input', { className: prefixCls + '-input', tabIndex: props.tabIndex, autoComplete: 'off', onFocus: this.onFocus, onBlur: this.onBlur, autoFocus: props.autoFocus, readOnly: props.readOnly, disabled: props.disabled, max: props.max, min: props.min, step: props.step, onChange: this.onChange, ref: this.setInput, value: inputDisplayValueFormat })
166 )
167 );
168 }
169 }]);
170
171 return InputNumber;
172}(BaseComponent);
173
174export default InputNumber;
175
176InputNumber.defaultProps = _extends({}, BaseComponent.defaultProps, { focusOnUpDown: false, useTouch: false, prefixCls: 'rmc-input-number' });
\No newline at end of file