1 | const _excluded = ["className", "containerClassName", "disabled", "readOnly", "value", "min", "max", "incrementIcon", "decrementIcon", "placeholder", "autoFocus", "tabIndex", "parse", "name", "onChange", "messages", "format", "onKeyDown", "onKeyPress", "onKeyUp", "inputProps", "precision", "step"];
|
2 |
|
3 | function _extends() { _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; }; return _extends.apply(this, arguments); }
|
4 |
|
5 | function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
6 |
|
7 | import cn from 'classnames';
|
8 | import PropTypes from 'prop-types';
|
9 | import React, { useRef } from 'react';
|
10 | import { useUncontrolled } from 'uncontrollable';
|
11 | import Button from './Button';
|
12 | import { caretDown, caretUp } from './Icon';
|
13 | import { useLocalizer } from './Localization';
|
14 | import NumberInput from './NumberInput';
|
15 | import Widget from './Widget';
|
16 | import WidgetPicker from './WidgetPicker';
|
17 | import * as CustomPropTypes from './PropTypes';
|
18 | import useFocusManager from './useFocusManager';
|
19 | import { notify } from './WidgetHelpers';
|
20 | import useEventCallback from '@restart/hooks/useEventCallback';
|
21 |
|
22 |
|
23 | function createInterval(callback) {
|
24 | let fn;
|
25 | let id;
|
26 |
|
27 | const cancel = () => clearTimeout(id);
|
28 |
|
29 | id = window.setTimeout(fn = () => {
|
30 | id = window.setTimeout(fn, 35);
|
31 | callback();
|
32 | }, 500);
|
33 | return cancel;
|
34 | }
|
35 |
|
36 | function clamp(value, min, max) {
|
37 | max = max == null ? Infinity : max;
|
38 | min = min == null ? -Infinity : min;
|
39 | if (value == null || value === '') return null;
|
40 | return Math.max(Math.min(typeof value == 'string' ? parseInt(value) : value, max), min);
|
41 | }
|
42 |
|
43 | const propTypes = {
|
44 | |
45 |
|
46 |
|
47 | value: PropTypes.number,
|
48 |
|
49 | |
50 |
|
51 |
|
52 | onChange: PropTypes.func,
|
53 |
|
54 | |
55 |
|
56 |
|
57 |
|
58 | min: PropTypes.number,
|
59 |
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 | max: PropTypes.number,
|
66 |
|
67 | |
68 |
|
69 |
|
70 |
|
71 |
|
72 | step: PropTypes.number,
|
73 |
|
74 | |
75 |
|
76 |
|
77 |
|
78 | precision: PropTypes.oneOfType([PropTypes.number, PropTypes.oneOf(['auto'])]),
|
79 |
|
80 | |
81 |
|
82 |
|
83 |
|
84 |
|
85 | format: PropTypes.any,
|
86 | parse: PropTypes.func,
|
87 | incrementIcon: PropTypes.node,
|
88 | decrementIcon: PropTypes.node,
|
89 |
|
90 |
|
91 | tabIndex: PropTypes.any,
|
92 | name: PropTypes.string,
|
93 | placeholder: PropTypes.string,
|
94 | onKeyDown: PropTypes.func,
|
95 | onKeyPress: PropTypes.func,
|
96 | onKeyUp: PropTypes.func,
|
97 | autoFocus: PropTypes.bool,
|
98 |
|
99 | |
100 |
|
101 |
|
102 | disabled: CustomPropTypes.disabled,
|
103 |
|
104 | |
105 |
|
106 |
|
107 | readOnly: CustomPropTypes.disabled,
|
108 |
|
109 |
|
110 | containerClassName: PropTypes.string,
|
111 | inputProps: PropTypes.object,
|
112 | messages: PropTypes.shape({
|
113 | increment: PropTypes.string,
|
114 | decrement: PropTypes.string
|
115 | }),
|
116 |
|
117 |
|
118 | localizer: PropTypes.object
|
119 | };
|
120 | const defaultProps = {
|
121 | incrementIcon: caretUp,
|
122 | decrementIcon: caretDown,
|
123 | min: -Infinity,
|
124 | max: Infinity,
|
125 | step: 1,
|
126 | precision: 'auto'
|
127 | };
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | function NumberPicker(uncontrolledProps) {
|
142 | const _useUncontrolled = useUncontrolled(uncontrolledProps, {
|
143 | value: 'onChange'
|
144 | }),
|
145 | {
|
146 | className,
|
147 | containerClassName,
|
148 | disabled,
|
149 | readOnly,
|
150 | value,
|
151 | min,
|
152 | max,
|
153 | incrementIcon,
|
154 | decrementIcon,
|
155 | placeholder,
|
156 | autoFocus,
|
157 | tabIndex,
|
158 | parse,
|
159 | name,
|
160 | onChange,
|
161 | messages,
|
162 | format,
|
163 | onKeyDown,
|
164 | onKeyPress,
|
165 | onKeyUp,
|
166 | inputProps,
|
167 | precision,
|
168 | step: pStep
|
169 | } = _useUncontrolled,
|
170 | elementProps = _objectWithoutPropertiesLoose(_useUncontrolled, _excluded);
|
171 |
|
172 | const localizer = useLocalizer(messages, {
|
173 | number: format
|
174 | });
|
175 | const ref = useRef(null);
|
176 | const inputRef = useRef(null);
|
177 | const repeaterRef = useRef(null);
|
178 | const [focusEvents, focused] = useFocusManager(ref, uncontrolledProps, {
|
179 | willHandle(focused) {
|
180 | if (focused) focus();
|
181 | }
|
182 |
|
183 | });
|
184 | const handleMouseDown = useEventCallback((direction, event) => {
|
185 | if (event) event.persist();
|
186 | let method = direction === 'UP' ? increment : decrement;
|
187 | let value = method(event),
|
188 | atTop = direction === 'UP' && value === max,
|
189 | atBottom = direction === 'DOWN' && value === min;
|
190 | if (atTop || atBottom) handleMouseUp();else if (!repeaterRef.current) {
|
191 | repeaterRef.current = createInterval(() => {
|
192 | handleMouseDown(direction, event);
|
193 | });
|
194 | }
|
195 | });
|
196 | const handleMouseUp = useEventCallback(() => {
|
197 | if (!repeaterRef.current) return;
|
198 | repeaterRef.current();
|
199 | repeaterRef.current = null;
|
200 | });
|
201 | const handleKeyDown = useEventCallback(event => {
|
202 | if (readOnly) return;
|
203 | let key = event.key;
|
204 | notify(onKeyDown, [event]);
|
205 | if (event.defaultPrevented) return;
|
206 | if (key === 'End' && isFinite(max)) handleChange(max, event);else if (key === 'Home' && isFinite(min)) handleChange(min, event);else if (key === 'ArrowDown') {
|
207 | event.preventDefault();
|
208 | decrement(event);
|
209 | } else if (key === 'ArrowUp') {
|
210 | event.preventDefault();
|
211 | increment(event);
|
212 | }
|
213 | });
|
214 |
|
215 | const handleChange = (rawValue, originalEvent = null) => {
|
216 | let nextValue = clamp(rawValue, min, max);
|
217 | if (value !== nextValue) notify(onChange, [nextValue, {
|
218 | rawValue,
|
219 | originalEvent,
|
220 | lastValue: value
|
221 | }]);
|
222 | };
|
223 |
|
224 | function focus() {
|
225 | var _inputRef$current;
|
226 |
|
227 | (_inputRef$current = inputRef.current) == null ? void 0 : _inputRef$current.focus();
|
228 | }
|
229 |
|
230 | function increment(event) {
|
231 | return step(pStep, event);
|
232 | }
|
233 |
|
234 | function decrement(event) {
|
235 | return step(-pStep, event);
|
236 | }
|
237 |
|
238 | function step(amount, event) {
|
239 | const nextValue = (value || 0) + amount;
|
240 | let p = precision === 'auto' ? Math.max(getPrecision(value || 0), getPrecision(amount)) : precision;
|
241 | handleChange(p != null ? parseFloat(nextValue.toFixed(p)) : nextValue, event);
|
242 | return nextValue;
|
243 | }
|
244 |
|
245 | const clampedValue = clamp(value, min, max);
|
246 | return React.createElement(Widget, _extends({}, elementProps, {
|
247 | focused: focused,
|
248 | disabled: disabled,
|
249 | readOnly: readOnly,
|
250 | onKeyDown: handleKeyDown
|
251 | }, focusEvents, {
|
252 | ref: ref,
|
253 | className: cn(className, 'rw-number-picker')
|
254 | }), React.createElement(WidgetPicker, {
|
255 | className: containerClassName
|
256 | }, React.createElement(NumberInput, _extends({}, inputProps, {
|
257 | role: "spinbutton",
|
258 | tabIndex: tabIndex,
|
259 | value: clampedValue,
|
260 | placeholder: placeholder,
|
261 | autoFocus: autoFocus,
|
262 | editing: focused,
|
263 | localizer: localizer,
|
264 | parse: parse,
|
265 | name: name,
|
266 | min: min,
|
267 | max: max,
|
268 | disabled: disabled,
|
269 | readOnly: readOnly,
|
270 | onChange: handleChange,
|
271 | onKeyPress: onKeyPress,
|
272 | onKeyUp: onKeyUp,
|
273 | innerRef: inputRef
|
274 | })), React.createElement("span", {
|
275 | className: "rw-input-addon rw-number-picker-spinners"
|
276 | }, React.createElement(Button, {
|
277 | icon: incrementIcon,
|
278 | className: "rw-picker-btn",
|
279 | disabled: clampedValue === max || disabled || readOnly,
|
280 | label: localizer.messages.increment({
|
281 | value: clampedValue,
|
282 | min,
|
283 | max
|
284 | }),
|
285 | onMouseUp: () => handleMouseUp(),
|
286 | onMouseDown: e => handleMouseDown('UP', e),
|
287 | onMouseLeave: () => handleMouseUp()
|
288 | }), React.createElement(Button, {
|
289 | icon: decrementIcon,
|
290 | className: "rw-picker-btn",
|
291 | disabled: clampedValue === min || disabled || readOnly,
|
292 | label: localizer.messages.decrement({
|
293 | value: clampedValue,
|
294 | min,
|
295 | max
|
296 | }),
|
297 | onMouseUp: () => handleMouseUp(),
|
298 | onMouseDown: e => handleMouseDown('DOWN', e),
|
299 | onMouseLeave: () => handleMouseUp()
|
300 | }))));
|
301 | }
|
302 |
|
303 | ;
|
304 | NumberPicker.propTypes = propTypes;
|
305 | NumberPicker.defaultProps = defaultProps;
|
306 | export default NumberPicker;
|
307 |
|
308 | function getPrecision(a) {
|
309 | if (!isFinite(a)) return 0;
|
310 | let e = 1;
|
311 | let p = 0;
|
312 |
|
313 | while (Math.round(a * e) / e !== a) {
|
314 | e *= 10;
|
315 | p++;
|
316 | }
|
317 |
|
318 | return p;
|
319 | } |
\ | No newline at end of file |