1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
|
8 |
|
9 | var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
|
10 |
|
11 | var _createClass2 = require('babel-runtime/helpers/createClass');
|
12 |
|
13 | var _createClass3 = _interopRequireDefault(_createClass2);
|
14 |
|
15 | var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
|
16 |
|
17 | var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
|
18 |
|
19 | var _inherits2 = require('babel-runtime/helpers/inherits');
|
20 |
|
21 | var _inherits3 = _interopRequireDefault(_inherits2);
|
22 |
|
23 | var _react = require('react');
|
24 |
|
25 | var _react2 = _interopRequireDefault(_react);
|
26 |
|
27 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
28 |
|
29 | function noop() {}
|
30 | function defaultParser(input) {
|
31 | return input.replace(/[^\w\.-]+/g, '');
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 | var SPEED = 200;
|
37 |
|
38 |
|
39 |
|
40 | var DELAY = 600;
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1;
|
46 |
|
47 | var BaseComponent = function (_React$Component) {
|
48 | (0, _inherits3['default'])(BaseComponent, _React$Component);
|
49 |
|
50 | function BaseComponent(props) {
|
51 | (0, _classCallCheck3['default'])(this, BaseComponent);
|
52 |
|
53 | var _this = (0, _possibleConstructorReturn3['default'])(this, (BaseComponent.__proto__ || Object.getPrototypeOf(BaseComponent)).call(this, props));
|
54 |
|
55 | _this.onChange = function (e) {
|
56 | var _this$props = _this.props,
|
57 | parser = _this$props.parser,
|
58 | onChange = _this$props.onChange;
|
59 |
|
60 | var input = parser && parser(_this.getValueFromEvent(e).trim());
|
61 | _this.setState({ inputValue: input });
|
62 | onChange && onChange(_this.toNumberWhenUserInput(input));
|
63 | };
|
64 | _this.onFocus = function () {
|
65 | _this.setState({
|
66 | focused: true
|
67 | });
|
68 | var onFocus = _this.props.onFocus;
|
69 |
|
70 | onFocus && onFocus.apply(undefined, arguments);
|
71 | };
|
72 | _this.onBlur = function (e) {
|
73 | for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
74 | args[_key - 1] = arguments[_key];
|
75 | }
|
76 |
|
77 | _this.setState({
|
78 | focused: false
|
79 | });
|
80 | var value = _this.getCurrentValidValue(_this.state.inputValue);
|
81 | e.persist();
|
82 | _this.setValue(value, function () {
|
83 | var onBlur = _this.props.onBlur;
|
84 |
|
85 | onBlur && onBlur.apply(undefined, [e].concat(args));
|
86 | });
|
87 | };
|
88 | _this.getCurrentValidValue = function (value) {
|
89 | var val = value;
|
90 | if (val === '') {
|
91 | val = '';
|
92 | } else if (!_this.isNotCompleteNumber(val)) {
|
93 | val = _this.getValidValue(val);
|
94 | } else {
|
95 | val = _this.state.value;
|
96 | }
|
97 | return _this.toNumber(val);
|
98 | };
|
99 | _this.getValidValue = function (value) {
|
100 | var val = parseFloat(value);
|
101 |
|
102 | if (isNaN(val)) {
|
103 | return value;
|
104 | }
|
105 | if (val < _this.props.min) {
|
106 | val = _this.props.min;
|
107 | }
|
108 | if (val > _this.props.max) {
|
109 | val = _this.props.max;
|
110 | }
|
111 | return val;
|
112 | };
|
113 | _this.setValue = function (v, callback) {
|
114 |
|
115 | var newValue = _this.isNotCompleteNumber(parseFloat(v)) ? undefined : parseFloat(v);
|
116 | var changed = newValue !== _this.state.value || '' + newValue !== '' + _this.state.inputValue;
|
117 | if (!('value' in _this.props)) {
|
118 | _this.setState({
|
119 | value: newValue,
|
120 | inputValue: _this.toPrecisionAsStep(v)
|
121 | }, callback);
|
122 | } else {
|
123 |
|
124 | _this.setState({
|
125 | inputValue: _this.toPrecisionAsStep(_this.state.value)
|
126 | }, callback);
|
127 | }
|
128 | if (changed) {
|
129 | var onChange = _this.props.onChange;
|
130 |
|
131 | onChange && onChange(newValue);
|
132 | }
|
133 | };
|
134 | _this.getPrecision = function (value) {
|
135 | if ('precision' in _this.props) {
|
136 | return _this.props.precision;
|
137 | }
|
138 | var valueString = value.toString();
|
139 | if (valueString.indexOf('e-') >= 0) {
|
140 | return parseInt(valueString.slice(valueString.indexOf('e-') + 2), 10);
|
141 | }
|
142 | var precision = 0;
|
143 | if (valueString.indexOf('.') >= 0) {
|
144 | precision = valueString.length - valueString.indexOf('.') - 1;
|
145 | }
|
146 | return precision;
|
147 | };
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | _this.getMaxPrecision = function (currentValue) {
|
154 | var ratio = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
|
155 |
|
156 | if ('precision' in _this.props) {
|
157 | return _this.props.precision;
|
158 | }
|
159 | var step = _this.props.step;
|
160 |
|
161 | var ratioPrecision = _this.getPrecision(ratio);
|
162 | var stepPrecision = _this.getPrecision(step);
|
163 | var currentValuePrecision = _this.getPrecision(currentValue);
|
164 | if (!currentValue) {
|
165 | return ratioPrecision + stepPrecision;
|
166 | }
|
167 | return Math.max(currentValuePrecision, ratioPrecision + stepPrecision);
|
168 | };
|
169 | _this.getPrecisionFactor = function (currentValue) {
|
170 | var ratio = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
|
171 |
|
172 | var precision = _this.getMaxPrecision(currentValue, ratio);
|
173 | return Math.pow(10, precision);
|
174 | };
|
175 | _this.toPrecisionAsStep = function (num) {
|
176 | if (_this.isNotCompleteNumber(num) || num === '') {
|
177 | return num;
|
178 | }
|
179 | var precision = Math.abs(_this.getMaxPrecision(num));
|
180 | if (!isNaN(precision)) {
|
181 | return Number(num).toFixed(precision);
|
182 | }
|
183 | return num.toString();
|
184 | };
|
185 |
|
186 | _this.isNotCompleteNumber = function (num) {
|
187 | return isNaN(num) || num === '' || num === null || num && num.toString().indexOf('.') === num.toString().length - 1;
|
188 | };
|
189 | _this.toNumber = function (num) {
|
190 | if (_this.isNotCompleteNumber(num)) {
|
191 | return num;
|
192 | }
|
193 | if ('precision' in _this.props) {
|
194 | return Number(Number(num).toFixed(_this.props.precision));
|
195 | }
|
196 | return Number(num);
|
197 | };
|
198 |
|
199 | _this.toNumberWhenUserInput = function (num) {
|
200 |
|
201 | if ((/\.\d*0$/.test(num) || num.length > 16) && _this.state.focused) {
|
202 | return num;
|
203 | }
|
204 | return _this.toNumber(num);
|
205 | };
|
206 | _this.stepCompute = function (type, val, rat) {
|
207 | var _this$props2 = _this.props,
|
208 | step = _this$props2.step,
|
209 | min = _this$props2.min;
|
210 |
|
211 | var precisionFactor = _this.getPrecisionFactor(val, rat);
|
212 | var precision = Math.abs(_this.getMaxPrecision(val, rat));
|
213 | var result = void 0;
|
214 | var direct = type === 'up' ? 1 : -1;
|
215 | if (typeof val === 'number') {
|
216 | result = ((precisionFactor * val + direct * precisionFactor * +step * rat) / precisionFactor).toFixed(precision);
|
217 | } else {
|
218 | result = min === -Infinity ? direct * +step : min;
|
219 | }
|
220 | return _this.toNumber(result);
|
221 | };
|
222 | _this.step = function (type, e) {
|
223 | var ratio = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
|
224 |
|
225 | if (e) {
|
226 | e.preventDefault();
|
227 | }
|
228 | var props = _this.props;
|
229 | if (props.disabled) {
|
230 | return false;
|
231 | }
|
232 | var value = _this.getCurrentValidValue(_this.state.inputValue) || 0;
|
233 | if (_this.isNotCompleteNumber(value)) {
|
234 | return false;
|
235 | }
|
236 | var val = _this.stepCompute(type, value, ratio);
|
237 | var outOfRange = val > props.max || val < props.min;
|
238 | if (val > props.max) {
|
239 | val = props.max;
|
240 | } else if (val < props.min) {
|
241 | val = props.min;
|
242 | }
|
243 | _this.setValue(val);
|
244 | _this.setState({
|
245 | focused: true
|
246 | });
|
247 | return !outOfRange;
|
248 | };
|
249 | _this.stop = function () {
|
250 | if (_this.autoStepTimer) {
|
251 | clearTimeout(_this.autoStepTimer);
|
252 | }
|
253 | };
|
254 | _this.action = function (type, e, ratio, recursive) {
|
255 | if (e.persist) {
|
256 | e.persist();
|
257 | }
|
258 | _this.stop();
|
259 | if (_this.step(type, e, ratio)) {
|
260 | _this.autoStepTimer = setTimeout(function () {
|
261 | _this.action(type, e, ratio, true);
|
262 | }, recursive ? SPEED : DELAY);
|
263 | }
|
264 | };
|
265 | _this.down = function (e, ratio, recursive) {
|
266 | _this.action('down', e, ratio, recursive);
|
267 | };
|
268 | _this.up = function (e, ratio, recursive) {
|
269 | _this.action('up', e, ratio, recursive);
|
270 | };
|
271 | var value = void 0;
|
272 | if ('value' in props) {
|
273 | value = props.value;
|
274 | } else {
|
275 | value = props.defaultValue;
|
276 | }
|
277 | value = _this.toNumber(value);
|
278 | _this.state = {
|
279 | inputValue: _this.toPrecisionAsStep(value),
|
280 | value: value,
|
281 | focused: props.autoFocus
|
282 | };
|
283 | return _this;
|
284 | }
|
285 |
|
286 | (0, _createClass3['default'])(BaseComponent, [{
|
287 | key: 'componentWillReceiveProps',
|
288 | value: function componentWillReceiveProps(nextProps) {
|
289 | if ('value' in nextProps) {
|
290 | var value = this.state.focused ? nextProps.value : this.getValidValue(nextProps.value);
|
291 | this.setState({
|
292 | value: value,
|
293 | inputValue: value
|
294 | });
|
295 | }
|
296 | }
|
297 | }, {
|
298 | key: 'componentWillUnmount',
|
299 | value: function componentWillUnmount() {
|
300 | this.stop();
|
301 | }
|
302 | }]);
|
303 | return BaseComponent;
|
304 | }(_react2['default'].Component);
|
305 |
|
306 | exports['default'] = BaseComponent;
|
307 |
|
308 | BaseComponent.defaultProps = {
|
309 | max: MAX_SAFE_INTEGER,
|
310 | min: -MAX_SAFE_INTEGER,
|
311 | step: 1,
|
312 | style: {},
|
313 | onChange: noop,
|
314 | onFocus: noop,
|
315 | onBlur: noop,
|
316 | parser: defaultParser
|
317 | };
|
318 | ;
|
319 | module.exports = exports['default']; |
\ | No newline at end of file |