UNPKG

11 kBJavaScriptView Raw
1import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
2import _createClass from 'babel-runtime/helpers/createClass';
3import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
4import _inherits from 'babel-runtime/helpers/inherits';
5import React from 'react';
6function noop() {}
7function defaultParser(input) {
8 return input.replace(/[^\w\.-]+/g, '');
9}
10/**
11 * When click and hold on a button - the speed of auto changin the value.
12 */
13var SPEED = 200;
14/**
15 * When click and hold on a button - the delay before auto changin the value.
16 */
17var DELAY = 600;
18/**
19 * Max Safe Integer -- on IE this is not available, so manually set the number in that case.
20 * The reason this is used, instead of Infinity is because numbers above the MSI are unstable
21 */
22var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1;
23
24var BaseComponent = function (_React$Component) {
25 _inherits(BaseComponent, _React$Component);
26
27 function BaseComponent(props) {
28 _classCallCheck(this, BaseComponent);
29
30 var _this = _possibleConstructorReturn(this, (BaseComponent.__proto__ || Object.getPrototypeOf(BaseComponent)).call(this, props));
31
32 _this.onChange = function (e) {
33 var _this$props = _this.props,
34 parser = _this$props.parser,
35 onChange = _this$props.onChange;
36
37 var input = parser && parser(_this.getValueFromEvent(e).trim());
38 _this.setState({ inputValue: input });
39 onChange && onChange(_this.toNumberWhenUserInput(input)); // valid number or invalid string
40 };
41 _this.onFocus = function () {
42 _this.setState({
43 focused: true
44 });
45 var onFocus = _this.props.onFocus;
46
47 onFocus && onFocus.apply(undefined, arguments);
48 };
49 _this.onBlur = function (e) {
50 for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
51 args[_key - 1] = arguments[_key];
52 }
53
54 _this.setState({
55 focused: false
56 });
57 var value = _this.getCurrentValidValue(_this.state.inputValue);
58 e.persist(); // fix https://github.com/react-component/input-number/issues/51
59 _this.setValue(value, function () {
60 var onBlur = _this.props.onBlur;
61
62 onBlur && onBlur.apply(undefined, [e].concat(args));
63 });
64 };
65 _this.getCurrentValidValue = function (value) {
66 var val = value;
67 if (val === '') {
68 val = '';
69 } else if (!_this.isNotCompleteNumber(val)) {
70 val = _this.getValidValue(val);
71 } else {
72 val = _this.state.value;
73 }
74 return _this.toNumber(val);
75 };
76 _this.getValidValue = function (value) {
77 var val = parseFloat(value);
78 // https://github.com/ant-design/ant-design/issues/7358
79 if (isNaN(val)) {
80 return value;
81 }
82 if (val < _this.props.min) {
83 val = _this.props.min;
84 }
85 if (val > _this.props.max) {
86 val = _this.props.max;
87 }
88 return val;
89 };
90 _this.setValue = function (v, callback) {
91 // trigger onChange
92 var newValue = _this.isNotCompleteNumber(parseFloat(v)) ? undefined : parseFloat(v);
93 var changed = newValue !== _this.state.value || '' + newValue !== '' + _this.state.inputValue; // https://github.com/ant-design/ant-design/issues/7363
94 if (!('value' in _this.props)) {
95 _this.setState({
96 value: newValue,
97 inputValue: _this.toPrecisionAsStep(v)
98 }, callback);
99 } else {
100 // always set input value same as value
101 _this.setState({
102 inputValue: _this.toPrecisionAsStep(_this.state.value)
103 }, callback);
104 }
105 if (changed) {
106 var onChange = _this.props.onChange;
107
108 onChange && onChange(newValue);
109 }
110 };
111 _this.getPrecision = function (value) {
112 if ('precision' in _this.props) {
113 return _this.props.precision;
114 }
115 var valueString = value.toString();
116 if (valueString.indexOf('e-') >= 0) {
117 return parseInt(valueString.slice(valueString.indexOf('e-') + 2), 10);
118 }
119 var precision = 0;
120 if (valueString.indexOf('.') >= 0) {
121 precision = valueString.length - valueString.indexOf('.') - 1;
122 }
123 return precision;
124 };
125 // step={1.0} value={1.51}
126 // press +
127 // then value should be 2.51, rather than 2.5
128 // if this.props.precision is undefined
129 // https://github.com/react-component/input-number/issues/39
130 _this.getMaxPrecision = function (currentValue) {
131 var ratio = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
132
133 if ('precision' in _this.props) {
134 return _this.props.precision;
135 }
136 var step = _this.props.step;
137
138 var ratioPrecision = _this.getPrecision(ratio);
139 var stepPrecision = _this.getPrecision(step);
140 var currentValuePrecision = _this.getPrecision(currentValue);
141 if (!currentValue) {
142 return ratioPrecision + stepPrecision;
143 }
144 return Math.max(currentValuePrecision, ratioPrecision + stepPrecision);
145 };
146 _this.getPrecisionFactor = function (currentValue) {
147 var ratio = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
148
149 var precision = _this.getMaxPrecision(currentValue, ratio);
150 return Math.pow(10, precision);
151 };
152 _this.toPrecisionAsStep = function (num) {
153 if (_this.isNotCompleteNumber(num) || num === '') {
154 return num;
155 }
156 var precision = Math.abs(_this.getMaxPrecision(num));
157 if (!isNaN(precision)) {
158 return Number(num).toFixed(precision);
159 }
160 return num.toString();
161 };
162 // '1.' '1x' 'xx' '' => are not complete numbers
163 _this.isNotCompleteNumber = function (num) {
164 return isNaN(num) || num === '' || num === null || num && num.toString().indexOf('.') === num.toString().length - 1;
165 };
166 _this.toNumber = function (num) {
167 if (_this.isNotCompleteNumber(num)) {
168 return num;
169 }
170 if ('precision' in _this.props) {
171 return Number(Number(num).toFixed(_this.props.precision));
172 }
173 return Number(num);
174 };
175 // '1.0' '1.00' => may be a inputing number
176 _this.toNumberWhenUserInput = function (num) {
177 // num.length > 16 => prevent input large number will became Infinity
178 if ((/\.\d*0$/.test(num) || num.length > 16) && _this.state.focused) {
179 return num;
180 }
181 return _this.toNumber(num);
182 };
183 _this.stepCompute = function (type, val, rat) {
184 var _this$props2 = _this.props,
185 step = _this$props2.step,
186 min = _this$props2.min;
187
188 var precisionFactor = _this.getPrecisionFactor(val, rat);
189 var precision = Math.abs(_this.getMaxPrecision(val, rat));
190 var result = void 0;
191 var direct = type === 'up' ? 1 : -1;
192 if (typeof val === 'number') {
193 result = ((precisionFactor * val + direct * precisionFactor * +step * rat) / precisionFactor).toFixed(precision);
194 } else {
195 result = min === -Infinity ? direct * +step : min;
196 }
197 return _this.toNumber(result);
198 };
199 _this.step = function (type, e) {
200 var ratio = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
201
202 if (e) {
203 e.preventDefault();
204 }
205 var props = _this.props;
206 if (props.disabled) {
207 return false;
208 }
209 var value = _this.getCurrentValidValue(_this.state.inputValue) || 0;
210 if (_this.isNotCompleteNumber(value)) {
211 return false;
212 }
213 var val = _this.stepCompute(type, value, ratio);
214 var outOfRange = val > props.max || val < props.min;
215 if (val > props.max) {
216 val = props.max;
217 } else if (val < props.min) {
218 val = props.min;
219 }
220 _this.setValue(val);
221 _this.setState({
222 focused: true
223 });
224 return !outOfRange;
225 };
226 _this.stop = function () {
227 if (_this.autoStepTimer) {
228 clearTimeout(_this.autoStepTimer);
229 }
230 };
231 _this.action = function (type, e, ratio, recursive) {
232 if (e.persist) {
233 e.persist();
234 }
235 _this.stop();
236 if (_this.step(type, e, ratio)) {
237 _this.autoStepTimer = setTimeout(function () {
238 _this.action(type, e, ratio, true);
239 }, recursive ? SPEED : DELAY);
240 }
241 };
242 _this.down = function (e, ratio, recursive) {
243 _this.action('down', e, ratio, recursive);
244 };
245 _this.up = function (e, ratio, recursive) {
246 _this.action('up', e, ratio, recursive);
247 };
248 var value = void 0;
249 if ('value' in props) {
250 value = props.value;
251 } else {
252 value = props.defaultValue;
253 }
254 value = _this.toNumber(value);
255 _this.state = {
256 inputValue: _this.toPrecisionAsStep(value),
257 value: value,
258 focused: props.autoFocus
259 };
260 return _this;
261 }
262
263 _createClass(BaseComponent, [{
264 key: 'componentWillReceiveProps',
265 value: function componentWillReceiveProps(nextProps) {
266 if ('value' in nextProps) {
267 var value = this.state.focused ? nextProps.value : this.getValidValue(nextProps.value);
268 this.setState({
269 value: value,
270 inputValue: value
271 });
272 }
273 }
274 }, {
275 key: 'componentWillUnmount',
276 value: function componentWillUnmount() {
277 this.stop();
278 }
279 }]);
280
281 return BaseComponent;
282}(React.Component);
283
284export default BaseComponent;
285
286BaseComponent.defaultProps = {
287 max: MAX_SAFE_INTEGER,
288 min: -MAX_SAFE_INTEGER,
289 step: 1,
290 style: {},
291 onChange: noop,
292 onFocus: noop,
293 onBlur: noop,
294 parser: defaultParser
295};
296;
\No newline at end of file