UNPKG

19.9 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
3import _typeof from "@babel/runtime/helpers/esm/typeof";
4import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
5import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
6var _excluded = ["prefixCls", "className", "style", "min", "max", "step", "defaultValue", "value", "disabled", "readOnly", "upHandler", "downHandler", "keyboard", "controls", "classNames", "stringMode", "parser", "formatter", "precision", "decimalSeparator", "onChange", "onInput", "onPressEnter", "onStep"],
7 _excluded2 = ["disabled", "style", "prefixCls", "value", "prefix", "suffix", "addonBefore", "addonAfter", "classes", "className", "classNames"];
8import getMiniDecimal, { getNumberPrecision, num2str, toFixed, validateNumber } from '@rc-component/mini-decimal';
9import clsx from 'classnames';
10import { BaseInput } from 'rc-input';
11import { useLayoutUpdateEffect } from "rc-util/es/hooks/useLayoutEffect";
12import { composeRef } from "rc-util/es/ref";
13import * as React from 'react';
14import useCursor from "./hooks/useCursor";
15import StepHandler from "./StepHandler";
16import { getDecupleSteps } from "./utils/numberUtil";
17import { triggerFocus } from "rc-input/es/utils/commonUtils";
18import useFrame from "./hooks/useFrame";
19/**
20 * We support `stringMode` which need handle correct type when user call in onChange
21 * format max or min value
22 * 1. if isInvalid return null
23 * 2. if precision is undefined, return decimal
24 * 3. format with precision
25 * I. if max > 0, round down with precision. Example: max= 3.5, precision=0 afterFormat: 3
26 * II. if max < 0, round up with precision. Example: max= -3.5, precision=0 afterFormat: -4
27 * III. if min > 0, round up with precision. Example: min= 3.5, precision=0 afterFormat: 4
28 * IV. if min < 0, round down with precision. Example: max= -3.5, precision=0 afterFormat: -3
29 */
30var getDecimalValue = function getDecimalValue(stringMode, decimalValue) {
31 if (stringMode || decimalValue.isEmpty()) {
32 return decimalValue.toString();
33 }
34 return decimalValue.toNumber();
35};
36var getDecimalIfValidate = function getDecimalIfValidate(value) {
37 var decimal = getMiniDecimal(value);
38 return decimal.isInvalidate() ? null : decimal;
39};
40var InternalInputNumber = /*#__PURE__*/React.forwardRef(function (props, ref) {
41 var _clsx;
42 var _props$prefixCls = props.prefixCls,
43 prefixCls = _props$prefixCls === void 0 ? 'rc-input-number' : _props$prefixCls,
44 className = props.className,
45 style = props.style,
46 min = props.min,
47 max = props.max,
48 _props$step = props.step,
49 step = _props$step === void 0 ? 1 : _props$step,
50 defaultValue = props.defaultValue,
51 value = props.value,
52 disabled = props.disabled,
53 readOnly = props.readOnly,
54 upHandler = props.upHandler,
55 downHandler = props.downHandler,
56 keyboard = props.keyboard,
57 _props$controls = props.controls,
58 controls = _props$controls === void 0 ? true : _props$controls,
59 classNames = props.classNames,
60 stringMode = props.stringMode,
61 parser = props.parser,
62 formatter = props.formatter,
63 precision = props.precision,
64 decimalSeparator = props.decimalSeparator,
65 onChange = props.onChange,
66 onInput = props.onInput,
67 onPressEnter = props.onPressEnter,
68 onStep = props.onStep,
69 inputProps = _objectWithoutProperties(props, _excluded);
70 var inputClassName = "".concat(prefixCls, "-input");
71 var inputRef = React.useRef(null);
72 var _React$useState = React.useState(false),
73 _React$useState2 = _slicedToArray(_React$useState, 2),
74 focus = _React$useState2[0],
75 setFocus = _React$useState2[1];
76 var userTypingRef = React.useRef(false);
77 var compositionRef = React.useRef(false);
78 var shiftKeyRef = React.useRef(false);
79
80 // ============================ Value =============================
81 // Real value control
82 var _React$useState3 = React.useState(function () {
83 return getMiniDecimal(value !== null && value !== void 0 ? value : defaultValue);
84 }),
85 _React$useState4 = _slicedToArray(_React$useState3, 2),
86 decimalValue = _React$useState4[0],
87 setDecimalValue = _React$useState4[1];
88 function setUncontrolledDecimalValue(newDecimal) {
89 if (value === undefined) {
90 setDecimalValue(newDecimal);
91 }
92 }
93
94 // ====================== Parser & Formatter ======================
95 /**
96 * `precision` is used for formatter & onChange.
97 * It will auto generate by `value` & `step`.
98 * But it will not block user typing.
99 *
100 * Note: Auto generate `precision` is used for legacy logic.
101 * We should remove this since we already support high precision with BigInt.
102 *
103 * @param number Provide which number should calculate precision
104 * @param userTyping Change by user typing
105 */
106 var getPrecision = React.useCallback(function (numStr, userTyping) {
107 if (userTyping) {
108 return undefined;
109 }
110 if (precision >= 0) {
111 return precision;
112 }
113 return Math.max(getNumberPrecision(numStr), getNumberPrecision(step));
114 }, [precision, step]);
115
116 // >>> Parser
117 var mergedParser = React.useCallback(function (num) {
118 var numStr = String(num);
119 if (parser) {
120 return parser(numStr);
121 }
122 var parsedStr = numStr;
123 if (decimalSeparator) {
124 parsedStr = parsedStr.replace(decimalSeparator, '.');
125 }
126
127 // [Legacy] We still support auto convert `$ 123,456` to `123456`
128 return parsedStr.replace(/[^\w.-]+/g, '');
129 }, [parser, decimalSeparator]);
130
131 // >>> Formatter
132 var inputValueRef = React.useRef('');
133 var mergedFormatter = React.useCallback(function (number, userTyping) {
134 if (formatter) {
135 return formatter(number, {
136 userTyping: userTyping,
137 input: String(inputValueRef.current)
138 });
139 }
140 var str = typeof number === 'number' ? num2str(number) : number;
141
142 // User typing will not auto format with precision directly
143 if (!userTyping) {
144 var mergedPrecision = getPrecision(str, userTyping);
145 if (validateNumber(str) && (decimalSeparator || mergedPrecision >= 0)) {
146 // Separator
147 var separatorStr = decimalSeparator || '.';
148 str = toFixed(str, separatorStr, mergedPrecision);
149 }
150 }
151 return str;
152 }, [formatter, getPrecision, decimalSeparator]);
153
154 // ========================== InputValue ==========================
155 /**
156 * Input text value control
157 *
158 * User can not update input content directly. It updates with follow rules by priority:
159 * 1. controlled `value` changed
160 * * [SPECIAL] Typing like `1.` should not immediately convert to `1`
161 * 2. User typing with format (not precision)
162 * 3. Blur or Enter trigger revalidate
163 */
164 var _React$useState5 = React.useState(function () {
165 var initValue = defaultValue !== null && defaultValue !== void 0 ? defaultValue : value;
166 if (decimalValue.isInvalidate() && ['string', 'number'].includes(_typeof(initValue))) {
167 return Number.isNaN(initValue) ? '' : initValue;
168 }
169 return mergedFormatter(decimalValue.toString(), false);
170 }),
171 _React$useState6 = _slicedToArray(_React$useState5, 2),
172 inputValue = _React$useState6[0],
173 setInternalInputValue = _React$useState6[1];
174 inputValueRef.current = inputValue;
175
176 // Should always be string
177 function setInputValue(newValue, userTyping) {
178 setInternalInputValue(mergedFormatter(
179 // Invalidate number is sometime passed by external control, we should let it go
180 // Otherwise is controlled by internal interactive logic which check by userTyping
181 // You can ref 'show limited value when input is not focused' test for more info.
182 newValue.isInvalidate() ? newValue.toString(false) : newValue.toString(!userTyping), userTyping));
183 }
184
185 // >>> Max & Min limit
186 var maxDecimal = React.useMemo(function () {
187 return getDecimalIfValidate(max);
188 }, [max, precision]);
189 var minDecimal = React.useMemo(function () {
190 return getDecimalIfValidate(min);
191 }, [min, precision]);
192 var upDisabled = React.useMemo(function () {
193 if (!maxDecimal || !decimalValue || decimalValue.isInvalidate()) {
194 return false;
195 }
196 return maxDecimal.lessEquals(decimalValue);
197 }, [maxDecimal, decimalValue]);
198 var downDisabled = React.useMemo(function () {
199 if (!minDecimal || !decimalValue || decimalValue.isInvalidate()) {
200 return false;
201 }
202 return decimalValue.lessEquals(minDecimal);
203 }, [minDecimal, decimalValue]);
204
205 // Cursor controller
206 var _useCursor = useCursor(inputRef.current, focus),
207 _useCursor2 = _slicedToArray(_useCursor, 2),
208 recordCursor = _useCursor2[0],
209 restoreCursor = _useCursor2[1];
210
211 // ============================= Data =============================
212 /**
213 * Find target value closet within range.
214 * e.g. [11, 28]:
215 * 3 => 11
216 * 23 => 23
217 * 99 => 28
218 */
219 var getRangeValue = function getRangeValue(target) {
220 // target > max
221 if (maxDecimal && !target.lessEquals(maxDecimal)) {
222 return maxDecimal;
223 }
224
225 // target < min
226 if (minDecimal && !minDecimal.lessEquals(target)) {
227 return minDecimal;
228 }
229 return null;
230 };
231
232 /**
233 * Check value is in [min, max] range
234 */
235 var isInRange = function isInRange(target) {
236 return !getRangeValue(target);
237 };
238
239 /**
240 * Trigger `onChange` if value validated and not equals of origin.
241 * Return the value that re-align in range.
242 */
243 var triggerValueUpdate = function triggerValueUpdate(newValue, userTyping) {
244 var updateValue = newValue;
245 var isRangeValidate = isInRange(updateValue) || updateValue.isEmpty();
246
247 // Skip align value when trigger value is empty.
248 // We just trigger onChange(null)
249 // This should not block user typing
250 if (!updateValue.isEmpty() && !userTyping) {
251 // Revert value in range if needed
252 updateValue = getRangeValue(updateValue) || updateValue;
253 isRangeValidate = true;
254 }
255 if (!readOnly && !disabled && isRangeValidate) {
256 var numStr = updateValue.toString();
257 var mergedPrecision = getPrecision(numStr, userTyping);
258 if (mergedPrecision >= 0) {
259 updateValue = getMiniDecimal(toFixed(numStr, '.', mergedPrecision));
260
261 // When to fixed. The value may out of min & max range.
262 // 4 in [0, 3.8] => 3.8 => 4 (toFixed)
263 if (!isInRange(updateValue)) {
264 updateValue = getMiniDecimal(toFixed(numStr, '.', mergedPrecision, true));
265 }
266 }
267
268 // Trigger event
269 if (!updateValue.equals(decimalValue)) {
270 setUncontrolledDecimalValue(updateValue);
271 onChange === null || onChange === void 0 ? void 0 : onChange(updateValue.isEmpty() ? null : getDecimalValue(stringMode, updateValue));
272
273 // Reformat input if value is not controlled
274 if (value === undefined) {
275 setInputValue(updateValue, userTyping);
276 }
277 }
278 return updateValue;
279 }
280 return decimalValue;
281 };
282
283 // ========================== User Input ==========================
284 var onNextPromise = useFrame();
285
286 // >>> Collect input value
287 var collectInputValue = function collectInputValue(inputStr) {
288 recordCursor();
289
290 // Update inputValue in case input can not parse as number
291 // Refresh ref value immediately since it may used by formatter
292 inputValueRef.current = inputStr;
293 setInternalInputValue(inputStr);
294
295 // Parse number
296 if (!compositionRef.current) {
297 var finalValue = mergedParser(inputStr);
298 var finalDecimal = getMiniDecimal(finalValue);
299 if (!finalDecimal.isNaN()) {
300 triggerValueUpdate(finalDecimal, true);
301 }
302 }
303
304 // Trigger onInput later to let user customize value if they want to handle something after onChange
305 onInput === null || onInput === void 0 ? void 0 : onInput(inputStr);
306
307 // optimize for chinese input experience
308 // https://github.com/ant-design/ant-design/issues/8196
309 onNextPromise(function () {
310 var nextInputStr = inputStr;
311 if (!parser) {
312 nextInputStr = inputStr.replace(/。/g, '.');
313 }
314 if (nextInputStr !== inputStr) {
315 collectInputValue(nextInputStr);
316 }
317 });
318 };
319
320 // >>> Composition
321 var onCompositionStart = function onCompositionStart() {
322 compositionRef.current = true;
323 };
324 var onCompositionEnd = function onCompositionEnd() {
325 compositionRef.current = false;
326 collectInputValue(inputRef.current.value);
327 };
328
329 // >>> Input
330 var onInternalInput = function onInternalInput(e) {
331 collectInputValue(e.target.value);
332 };
333
334 // ============================= Step =============================
335 var onInternalStep = function onInternalStep(up) {
336 var _inputRef$current;
337 // Ignore step since out of range
338 if (up && upDisabled || !up && downDisabled) {
339 return;
340 }
341
342 // Clear typing status since it may be caused by up & down key.
343 // We should sync with input value.
344 userTypingRef.current = false;
345 var stepDecimal = getMiniDecimal(shiftKeyRef.current ? getDecupleSteps(step) : step);
346 if (!up) {
347 stepDecimal = stepDecimal.negate();
348 }
349 var target = (decimalValue || getMiniDecimal(0)).add(stepDecimal.toString());
350 var updatedValue = triggerValueUpdate(target, false);
351 onStep === null || onStep === void 0 ? void 0 : onStep(getDecimalValue(stringMode, updatedValue), {
352 offset: shiftKeyRef.current ? getDecupleSteps(step) : step,
353 type: up ? 'up' : 'down'
354 });
355 (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 ? void 0 : _inputRef$current.focus();
356 };
357
358 // ============================ Flush =============================
359 /**
360 * Flush current input content to trigger value change & re-formatter input if needed.
361 * This will always flush input value for update.
362 * If it's invalidate, will fallback to last validate value.
363 */
364 var flushInputValue = function flushInputValue(userTyping) {
365 var parsedValue = getMiniDecimal(mergedParser(inputValue));
366 var formatValue = parsedValue;
367 if (!parsedValue.isNaN()) {
368 // Only validate value or empty value can be re-fill to inputValue
369 // Reassign the formatValue within ranged of trigger control
370 formatValue = triggerValueUpdate(parsedValue, userTyping);
371 } else {
372 formatValue = triggerValueUpdate(decimalValue, userTyping);
373 }
374 if (value !== undefined) {
375 // Reset back with controlled value first
376 setInputValue(decimalValue, false);
377 } else if (!formatValue.isNaN()) {
378 // Reset input back since no validate value
379 setInputValue(formatValue, false);
380 }
381 };
382
383 // Solve the issue of the event triggering sequence when entering numbers in chinese input (Safari)
384 var onBeforeInput = function onBeforeInput() {
385 userTypingRef.current = true;
386 };
387 var onKeyDown = function onKeyDown(event) {
388 var key = event.key,
389 shiftKey = event.shiftKey;
390 userTypingRef.current = true;
391 shiftKeyRef.current = shiftKey;
392 if (key === 'Enter') {
393 if (!compositionRef.current) {
394 userTypingRef.current = false;
395 }
396 flushInputValue(false);
397 onPressEnter === null || onPressEnter === void 0 ? void 0 : onPressEnter(event);
398 }
399 if (keyboard === false) {
400 return;
401 }
402
403 // Do step
404 if (!compositionRef.current && ['Up', 'ArrowUp', 'Down', 'ArrowDown'].includes(key)) {
405 onInternalStep(key === 'Up' || key === 'ArrowUp');
406 event.preventDefault();
407 }
408 };
409 var onKeyUp = function onKeyUp() {
410 userTypingRef.current = false;
411 shiftKeyRef.current = false;
412 };
413
414 // >>> Focus & Blur
415 var onBlur = function onBlur() {
416 flushInputValue(false);
417 setFocus(false);
418 userTypingRef.current = false;
419 };
420
421 // ========================== Controlled ==========================
422 // Input by precision
423 useLayoutUpdateEffect(function () {
424 if (!decimalValue.isInvalidate()) {
425 setInputValue(decimalValue, false);
426 }
427 }, [precision]);
428
429 // Input by value
430 useLayoutUpdateEffect(function () {
431 var newValue = getMiniDecimal(value);
432 setDecimalValue(newValue);
433 var currentParsedValue = getMiniDecimal(mergedParser(inputValue));
434
435 // When user typing from `1.2` to `1.`, we should not convert to `1` immediately.
436 // But let it go if user set `formatter`
437 if (!newValue.equals(currentParsedValue) || !userTypingRef.current || formatter) {
438 // Update value as effect
439 setInputValue(newValue, userTypingRef.current);
440 }
441 }, [value]);
442
443 // ============================ Cursor ============================
444 useLayoutUpdateEffect(function () {
445 if (formatter) {
446 restoreCursor();
447 }
448 }, [inputValue]);
449
450 // ============================ Render ============================
451 return /*#__PURE__*/React.createElement("div", {
452 className: clsx(prefixCls, classNames === null || classNames === void 0 ? void 0 : classNames.input, className, (_clsx = {}, _defineProperty(_clsx, "".concat(prefixCls, "-focused"), focus), _defineProperty(_clsx, "".concat(prefixCls, "-disabled"), disabled), _defineProperty(_clsx, "".concat(prefixCls, "-readonly"), readOnly), _defineProperty(_clsx, "".concat(prefixCls, "-not-a-number"), decimalValue.isNaN()), _defineProperty(_clsx, "".concat(prefixCls, "-out-of-range"), !decimalValue.isInvalidate() && !isInRange(decimalValue)), _clsx)),
453 style: style,
454 onFocus: function onFocus() {
455 setFocus(true);
456 },
457 onBlur: onBlur,
458 onKeyDown: onKeyDown,
459 onKeyUp: onKeyUp,
460 onCompositionStart: onCompositionStart,
461 onCompositionEnd: onCompositionEnd,
462 onBeforeInput: onBeforeInput
463 }, controls && /*#__PURE__*/React.createElement(StepHandler, {
464 prefixCls: prefixCls,
465 upNode: upHandler,
466 downNode: downHandler,
467 upDisabled: upDisabled,
468 downDisabled: downDisabled,
469 onStep: onInternalStep
470 }), /*#__PURE__*/React.createElement("div", {
471 className: "".concat(inputClassName, "-wrap")
472 }, /*#__PURE__*/React.createElement("input", _extends({
473 autoComplete: "off",
474 role: "spinbutton",
475 "aria-valuemin": min,
476 "aria-valuemax": max,
477 "aria-valuenow": decimalValue.isInvalidate() ? null : decimalValue.toString(),
478 step: step
479 }, inputProps, {
480 ref: composeRef(inputRef, ref),
481 className: inputClassName,
482 value: inputValue,
483 onChange: onInternalInput,
484 disabled: disabled,
485 readOnly: readOnly
486 }))));
487});
488var InputNumber = /*#__PURE__*/React.forwardRef(function (props, ref) {
489 var disabled = props.disabled,
490 style = props.style,
491 prefixCls = props.prefixCls,
492 value = props.value,
493 prefix = props.prefix,
494 suffix = props.suffix,
495 addonBefore = props.addonBefore,
496 addonAfter = props.addonAfter,
497 classes = props.classes,
498 className = props.className,
499 classNames = props.classNames,
500 rest = _objectWithoutProperties(props, _excluded2);
501 var inputFocusRef = React.useRef(null);
502 var focus = function focus(option) {
503 if (inputFocusRef.current) {
504 triggerFocus(inputFocusRef.current, option);
505 }
506 };
507 return /*#__PURE__*/React.createElement(BaseInput, {
508 inputElement: /*#__PURE__*/React.createElement(InternalInputNumber, _extends({
509 prefixCls: prefixCls,
510 disabled: disabled,
511 classNames: classNames,
512 ref: composeRef(inputFocusRef, ref)
513 }, rest)),
514 className: className,
515 triggerFocus: focus,
516 prefixCls: prefixCls,
517 value: value,
518 disabled: disabled,
519 style: style,
520 prefix: prefix,
521 suffix: suffix,
522 addonAfter: addonAfter,
523 addonBefore: addonBefore,
524 classes: classes,
525 classNames: classNames,
526 components: {
527 affixWrapper: 'div',
528 groupWrapper: 'div',
529 wrapper: 'div',
530 groupAddon: 'div'
531 }
532 });
533});
534InputNumber.displayName = 'InputNumber';
535export default InputNumber;
\No newline at end of file