1 | import _extends from "@babel/runtime/helpers/esm/extends";
|
2 | import { NumberInputActionTypes } from './numberInputAction.types';
|
3 | import { clampStepwise, isNumber } from './utils';
|
4 | function getClampedValues(rawValue, context) {
|
5 | const {
|
6 | min,
|
7 | max,
|
8 | step
|
9 | } = context;
|
10 | const clampedValue = rawValue === null ? null : clampStepwise(rawValue, min, max, step);
|
11 | const newInputValue = clampedValue === null ? '' : String(clampedValue);
|
12 | return {
|
13 | value: clampedValue,
|
14 | inputValue: newInputValue
|
15 | };
|
16 | }
|
17 | function stepValue(state, context, direction, multiplier) {
|
18 | const {
|
19 | value
|
20 | } = state;
|
21 | const {
|
22 | step = 1,
|
23 | min,
|
24 | max
|
25 | } = context;
|
26 | if (isNumber(value)) {
|
27 | return {
|
28 | up: value + (step != null ? step : 1) * multiplier,
|
29 | down: value - (step != null ? step : 1) * multiplier
|
30 | }[direction];
|
31 | }
|
32 | return {
|
33 | up: min != null ? min : 1,
|
34 | down: max != null ? max : -1
|
35 | }[direction];
|
36 | }
|
37 | function handleClamp(state, context, inputValue) {
|
38 | const {
|
39 | getInputValueAsString
|
40 | } = context;
|
41 | const numberValueAsString = getInputValueAsString(inputValue);
|
42 | const intermediateValue = numberValueAsString === '' || numberValueAsString === '-' ? null : parseInt(numberValueAsString, 10);
|
43 | const clampedValues = getClampedValues(intermediateValue, context);
|
44 | return _extends({}, state, clampedValues);
|
45 | }
|
46 | function handleInputChange(state, context, inputValue) {
|
47 | const {
|
48 | getInputValueAsString
|
49 | } = context;
|
50 | const numberValueAsString = getInputValueAsString(inputValue);
|
51 | if (numberValueAsString.match(/^-?\d+?$/) || numberValueAsString === '' || numberValueAsString === '-') {
|
52 | return _extends({}, state, {
|
53 | inputValue: numberValueAsString
|
54 | });
|
55 | }
|
56 | return state;
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 | function handleStep(state, context, applyMultiplier, direction) {
|
62 | const multiplier = applyMultiplier ? context.shiftMultiplier : 1;
|
63 | const newValue = stepValue(state, context, direction, multiplier);
|
64 | const clampedValues = getClampedValues(newValue, context);
|
65 | return _extends({}, state, clampedValues);
|
66 | }
|
67 | function handleToMinOrMax(state, context, to) {
|
68 | const newValue = context[to];
|
69 | if (!isNumber(newValue)) {
|
70 | return state;
|
71 | }
|
72 | return _extends({}, state, {
|
73 | value: newValue,
|
74 | inputValue: String(newValue)
|
75 | });
|
76 | }
|
77 | export function numberInputReducer(state, action) {
|
78 | const {
|
79 | type,
|
80 | context
|
81 | } = action;
|
82 | switch (type) {
|
83 | case NumberInputActionTypes.clamp:
|
84 | return handleClamp(state, context, action.inputValue);
|
85 | case NumberInputActionTypes.inputChange:
|
86 | return handleInputChange(state, context, action.inputValue);
|
87 | case NumberInputActionTypes.increment:
|
88 | return handleStep(state, context, action.applyMultiplier, 'up');
|
89 | case NumberInputActionTypes.decrement:
|
90 | return handleStep(state, context, action.applyMultiplier, 'down');
|
91 | case NumberInputActionTypes.incrementToMax:
|
92 | return handleToMinOrMax(state, context, 'max');
|
93 | case NumberInputActionTypes.decrementToMin:
|
94 | return handleToMinOrMax(state, context, 'min');
|
95 | case NumberInputActionTypes.resetInputValue:
|
96 | return _extends({}, state, {
|
97 | inputValue: String(state.value)
|
98 | });
|
99 | default:
|
100 | return state;
|
101 | }
|
102 | } |
\ | No newline at end of file |