1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _defineProperty2 = require('babel-runtime/helpers/defineProperty');
|
8 |
|
9 | var _defineProperty3 = _interopRequireDefault(_defineProperty2);
|
10 |
|
11 | var _extends2 = require('babel-runtime/helpers/extends');
|
12 |
|
13 | var _extends3 = _interopRequireDefault(_extends2);
|
14 |
|
15 | var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
|
16 |
|
17 | var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
|
18 |
|
19 | var _createClass2 = require('babel-runtime/helpers/createClass');
|
20 |
|
21 | var _createClass3 = _interopRequireDefault(_createClass2);
|
22 |
|
23 | var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
|
24 |
|
25 | var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
|
26 |
|
27 | var _inherits2 = require('babel-runtime/helpers/inherits');
|
28 |
|
29 | var _inherits3 = _interopRequireDefault(_inherits2);
|
30 |
|
31 | var _react = require('react');
|
32 |
|
33 | var _react2 = _interopRequireDefault(_react);
|
34 |
|
35 | var _classnames = require('classnames');
|
36 |
|
37 | var _classnames2 = _interopRequireDefault(_classnames);
|
38 |
|
39 | var _base = require('./base');
|
40 |
|
41 | var _base2 = _interopRequireDefault(_base);
|
42 |
|
43 | var _InputHandler = require('./InputHandler');
|
44 |
|
45 | var _InputHandler2 = _interopRequireDefault(_InputHandler);
|
46 |
|
47 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
48 |
|
49 | function noop() {}
|
50 | function preventDefault(e) {
|
51 | e.preventDefault();
|
52 | }
|
53 |
|
54 | var InputNumber = function (_BaseComponent) {
|
55 | (0, _inherits3['default'])(InputNumber, _BaseComponent);
|
56 |
|
57 | function InputNumber() {
|
58 | (0, _classCallCheck3['default'])(this, InputNumber);
|
59 |
|
60 | var _this = (0, _possibleConstructorReturn3['default'])(this, (InputNumber.__proto__ || Object.getPrototypeOf(InputNumber)).apply(this, arguments));
|
61 |
|
62 | _this.setInput = function (input) {
|
63 | _this.input = input;
|
64 | };
|
65 | return _this;
|
66 | }
|
67 |
|
68 | (0, _createClass3['default'])(InputNumber, [{
|
69 | key: 'componentDidMount',
|
70 | value: function componentDidMount() {
|
71 | this.componentDidUpdate();
|
72 | }
|
73 | }, {
|
74 | key: 'componentWillUpdate',
|
75 | value: function componentWillUpdate() {
|
76 | try {
|
77 | this.start = this.input.selectionStart;
|
78 | this.end = this.input.selectionEnd;
|
79 | } catch (e) {
|
80 |
|
81 |
|
82 |
|
83 | }
|
84 | }
|
85 | }, {
|
86 | key: 'componentDidUpdate',
|
87 | value: function componentDidUpdate() {
|
88 | if (this.props.focusOnUpDown && this.state.focused) {
|
89 | var selectionRange = this.input.setSelectionRange;
|
90 | if (selectionRange && typeof selectionRange === 'function' && this.start !== undefined && this.end !== undefined && this.start !== this.end) {
|
91 | this.input.setSelectionRange(this.start, this.end);
|
92 | } else {
|
93 | this.focus();
|
94 | }
|
95 | }
|
96 | }
|
97 | }, {
|
98 | key: 'getRatio',
|
99 | value: function getRatio(e) {
|
100 | var ratio = 1;
|
101 | if (e.metaKey || e.ctrlKey) {
|
102 | ratio = 0.1;
|
103 | } else if (e.shiftKey) {
|
104 | ratio = 10;
|
105 | }
|
106 | return ratio;
|
107 | }
|
108 | }, {
|
109 | key: 'getValueFromEvent',
|
110 | value: function getValueFromEvent(e) {
|
111 | return e.target.value;
|
112 | }
|
113 | }, {
|
114 | key: 'focus',
|
115 | value: function focus() {
|
116 | this.input.focus();
|
117 | }
|
118 | }, {
|
119 | key: 'formatWrapper',
|
120 | value: function formatWrapper(num) {
|
121 | if (this.props.formatter) {
|
122 | return this.props.formatter(num);
|
123 | }
|
124 | return num;
|
125 | }
|
126 | }, {
|
127 | key: 'render',
|
128 | value: function render() {
|
129 | var _classNames;
|
130 |
|
131 | var props = (0, _extends3['default'])({}, this.props);
|
132 | var _props$prefixCls = props.prefixCls,
|
133 | prefixCls = _props$prefixCls === undefined ? '' : _props$prefixCls,
|
134 | disabled = props.disabled,
|
135 | readOnly = props.readOnly,
|
136 | max = props.max,
|
137 | min = props.min;
|
138 |
|
139 | var classes = (0, _classnames2['default'])((_classNames = {}, (0, _defineProperty3['default'])(_classNames, prefixCls, true), (0, _defineProperty3['default'])(_classNames, props.className, !!props.className), (0, _defineProperty3['default'])(_classNames, prefixCls + '-disabled', disabled), (0, _defineProperty3['default'])(_classNames, prefixCls + '-focused', this.state.focused), _classNames));
|
140 | var upDisabledClass = '';
|
141 | var downDisabledClass = '';
|
142 | var value = this.state.value;
|
143 |
|
144 | if (value || value === 0) {
|
145 | if (!isNaN(value)) {
|
146 | var val = Number(value);
|
147 | if (val >= max) {
|
148 | upDisabledClass = prefixCls + '-handler-up-disabled';
|
149 | }
|
150 | if (val <= min) {
|
151 | downDisabledClass = prefixCls + '-handler-down-disabled';
|
152 | }
|
153 | } else {
|
154 | upDisabledClass = prefixCls + '-handler-up-disabled';
|
155 | downDisabledClass = prefixCls + '-handler-down-disabled';
|
156 | }
|
157 | }
|
158 | var editable = !props.readOnly && !props.disabled;
|
159 |
|
160 |
|
161 | var inputDisplayValue = void 0;
|
162 | if (this.state.focused) {
|
163 | inputDisplayValue = this.state.inputValue;
|
164 | } else {
|
165 | inputDisplayValue = this.toPrecisionAsStep(this.state.value);
|
166 | }
|
167 | if (inputDisplayValue === undefined || inputDisplayValue === null) {
|
168 | inputDisplayValue = '';
|
169 | }
|
170 | var upEvents = void 0;
|
171 | var downEvents = void 0;
|
172 | upEvents = {
|
173 | onTouchStart: editable && !upDisabledClass ? this.up : noop,
|
174 | onTouchEnd: this.stop
|
175 | };
|
176 | downEvents = {
|
177 | onTouchStart: editable && !downDisabledClass ? this.down : noop,
|
178 | onTouchEnd: this.stop
|
179 | };
|
180 | var inputDisplayValueFormat = this.formatWrapper(inputDisplayValue);
|
181 | var isUpDisabled = !!upDisabledClass || disabled || readOnly;
|
182 | var isDownDisabled = !!downDisabledClass || disabled || readOnly;
|
183 | return _react2['default'].createElement(
|
184 | 'div',
|
185 | { className: classes, style: props.style },
|
186 | _react2['default'].createElement(
|
187 | 'div',
|
188 | { className: prefixCls + '-handler-wrap' },
|
189 | _react2['default'].createElement(
|
190 | _InputHandler2['default'],
|
191 | (0, _extends3['default'])({ disabled: isUpDisabled, prefixCls: prefixCls, unselectable: 'unselectable' }, upEvents, { role: 'button', 'aria-label': 'Increase Value', 'aria-disabled': !!isUpDisabled, className: prefixCls + '-handler ' + prefixCls + '-handler-up ' + upDisabledClass }),
|
192 | this.props.upHandler || _react2['default'].createElement('span', { unselectable: 'unselectable', className: prefixCls + '-handler-up-inner', onClick: preventDefault })
|
193 | ),
|
194 | _react2['default'].createElement(
|
195 | _InputHandler2['default'],
|
196 | (0, _extends3['default'])({ disabled: isDownDisabled, prefixCls: prefixCls, unselectable: 'unselectable' }, downEvents, { role: 'button', 'aria-label': 'Decrease Value', 'aria-disabled': !!isDownDisabled, className: prefixCls + '-handler ' + prefixCls + '-handler-down ' + downDisabledClass }),
|
197 | this.props.downHandler || _react2['default'].createElement('span', { unselectable: 'unselectable', className: prefixCls + '-handler-down-inner', onClick: preventDefault })
|
198 | )
|
199 | ),
|
200 | _react2['default'].createElement(
|
201 | 'div',
|
202 | { className: prefixCls + '-input-wrap', role: 'spinbutton', 'aria-valuemin': props.min, 'aria-valuemax': props.max, 'aria-valuenow': value },
|
203 | _react2['default'].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 })
|
204 | )
|
205 | );
|
206 | }
|
207 | }]);
|
208 | return InputNumber;
|
209 | }(_base2['default']);
|
210 |
|
211 | exports['default'] = InputNumber;
|
212 |
|
213 | InputNumber.defaultProps = (0, _extends3['default'])({}, _base2['default'].defaultProps, { focusOnUpDown: false, useTouch: false, prefixCls: 'rmc-input-number' });
|
214 | module.exports = exports['default']; |
\ | No newline at end of file |