@fluentui/react-spinbutton 0.0.0-nightly-20230317-1454.1 → 0.0.0-nightly-20230321-0440.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.json +17 -17
- package/CHANGELOG.md +11 -11
- package/lib/SpinButton.js +1 -1
- package/lib/SpinButton.js.map +1 -1
- package/lib/SpinButtonField.js +1 -1
- package/lib/SpinButtonField.js.map +1 -1
- package/lib/components/SpinButton/SpinButton.js +6 -6
- package/lib/components/SpinButton/SpinButton.js.map +1 -1
- package/lib/components/SpinButton/SpinButton.types.js +1 -1
- package/lib/components/SpinButton/SpinButton.types.js.map +1 -1
- package/lib/components/SpinButton/index.js +5 -5
- package/lib/components/SpinButton/index.js.map +1 -1
- package/lib/components/SpinButton/renderSpinButton.js +11 -3
- package/lib/components/SpinButton/renderSpinButton.js.map +1 -1
- package/lib/components/SpinButton/useSpinButton.js +57 -56
- package/lib/components/SpinButton/useSpinButton.js.map +1 -1
- package/lib/components/SpinButton/useSpinButtonStyles.js +14 -14
- package/lib/components/SpinButton/useSpinButtonStyles.js.map +1 -1
- package/lib/components/SpinButtonField/SpinButtonField.js +3 -2
- package/lib/components/SpinButtonField/SpinButtonField.js.map +1 -1
- package/lib/components/SpinButtonField/index.js +1 -1
- package/lib/components/SpinButtonField/index.js.map +1 -1
- package/lib/index.js +2 -2
- package/lib/index.js.map +1 -1
- package/lib/utils/clamp.js +1 -1
- package/lib/utils/clamp.js.map +1 -1
- package/lib/utils/getBound.js +4 -4
- package/lib/utils/getBound.js.map +1 -1
- package/lib/utils/index.js +3 -3
- package/lib/utils/index.js.map +1 -1
- package/lib/utils/precision.js +7 -6
- package/lib/utils/precision.js.map +1 -1
- package/lib-commonjs/SpinButton.js +4 -5
- package/lib-commonjs/SpinButton.js.map +1 -1
- package/lib-commonjs/SpinButtonField.js +4 -5
- package/lib-commonjs/SpinButtonField.js.map +1 -1
- package/lib-commonjs/components/SpinButton/SpinButton.js +20 -19
- package/lib-commonjs/components/SpinButton/SpinButton.js.map +1 -1
- package/lib-commonjs/components/SpinButton/SpinButton.types.js +2 -5
- package/lib-commonjs/components/SpinButton/SpinButton.types.js.map +1 -1
- package/lib-commonjs/components/SpinButton/index.js +8 -9
- package/lib-commonjs/components/SpinButton/index.js.map +1 -1
- package/lib-commonjs/components/SpinButton/renderSpinButton.js +24 -13
- package/lib-commonjs/components/SpinButton/renderSpinButton.js.map +1 -1
- package/lib-commonjs/components/SpinButton/useSpinButton.js +259 -246
- package/lib-commonjs/components/SpinButton/useSpinButton.js.map +1 -1
- package/lib-commonjs/components/SpinButton/useSpinButtonStyles.js +246 -506
- package/lib-commonjs/components/SpinButton/useSpinButtonStyles.js.map +1 -1
- package/lib-commonjs/components/SpinButtonField/SpinButtonField.js +11 -17
- package/lib-commonjs/components/SpinButtonField/SpinButtonField.js.map +1 -1
- package/lib-commonjs/components/SpinButtonField/index.js +4 -5
- package/lib-commonjs/components/SpinButtonField/index.js.map +1 -1
- package/lib-commonjs/index.js +49 -21
- package/lib-commonjs/index.js.map +1 -1
- package/lib-commonjs/utils/clamp.js +21 -28
- package/lib-commonjs/utils/clamp.js.map +1 -1
- package/lib-commonjs/utils/getBound.js +14 -16
- package/lib-commonjs/utils/getBound.js.map +1 -1
- package/lib-commonjs/utils/index.js +6 -7
- package/lib-commonjs/utils/index.js.map +1 -1
- package/lib-commonjs/utils/precision.js +33 -33
- package/lib-commonjs/utils/precision.js.map +1 -1
- package/package.json +12 -11
- package/.swcrc +0 -39
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["
|
1
|
+
{"version":3,"names":[],"sources":["../../../../../../../../../packages/react-components/react-spinbutton/src/components/SpinButton/SpinButton.types.ts"],"sourcesContent":["import type { ComponentProps, ComponentState, Slot } from '@fluentui/react-utilities';\nimport * as React from 'react';\n\nexport type SpinButtonSlots = {\n /**\n * The root element of SpinButton is a container `<div>`.\n * The root slot receives the `className` and `style` specified on the `<SpinButton>`.\n * All other native props are applied to the primary slot: `input`.\n */\n root: NonNullable<Slot<'span'>>;\n\n /**\n * Input that displays the current value and accepts direct input from the user.\n * Displayed value is formatted.\n *\n * This is the primary slot.\n */\n input: NonNullable<Slot<'input'>>;\n\n /**\n * Renders the increment control.\n */\n incrementButton: NonNullable<Slot<'button'>>;\n\n /**\n * Renders the decrement control.\n */\n decrementButton: NonNullable<Slot<'button'>>;\n};\n\n/**\n * SpinButton Props\n */\nexport type SpinButtonProps = Omit<\n ComponentProps<Partial<SpinButtonSlots>, 'input'>,\n 'defaultValue' | 'onChange' | 'size' | 'value'\n> & {\n /**\n * Controls the colors and borders of the input.\n * @default 'outline'\n */\n appearance?: 'outline' | 'underline' | 'filled-darker' | 'filled-lighter';\n\n /**\n * Initial value of the control (assumed to be valid). Updates to this prop will not be respected.\n *\n * Use this if you intend for the SpinButton to be an uncontrolled component which maintains its\n * own value. For a controlled component, use `value` instead. (Mutually exclusive with `value`.)\n *\n * Use `null` to indicate the control has no value.\n */\n defaultValue?: number | null;\n\n /**\n * String representation of `value`.\n *\n * Use this when displaying the value to users as something other than a plain number.\n * For example, when displaying currency values this might be \"$1.00\" when value is `1`.\n *\n * Only provide this if the SpinButton is a controlled component where you are maintaining its\n * current state and passing updates based on change events. When SpinButton is used as an\n * uncontrolled component this prop is ignored.\n */\n displayValue?: string;\n\n /**\n * Max value of the control. If not provided, the control has no maximum value.\n */\n max?: number;\n\n /**\n * Min value of the control. If not provided, the control has no minimum value.\n */\n min?: number;\n\n /**\n * Callback for when the committed value changes.\n * - User presses the up/down buttons (on single press or every spin)\n * - User presses the up/down arrow keys (on single press or every spin)\n * - User *commits* edits to the input text by focusing away (blurring) or pressing enter.\n * Note that this is NOT called for every key press while the user is editing.\n */\n onChange?: (event: SpinButtonChangeEvent, data: SpinButtonOnChangeData) => void;\n\n /**\n * How many decimal places the value should be rounded to.\n *\n * The default is calculated based on the precision of `step`: i.e. if step = 1, precision = 0.\n * step = 0.0089, precision = 4. step = 300, precision = 2. step = 23.00, precision = 2.\n */\n precision?: number;\n\n /**\n * Size of the input.\n * @default 'medium'\n */\n size?: 'small' | 'medium';\n\n /**\n * Difference between two adjacent values of the control.\n * This value is used to calculate the precision of the input if no `precision` is given.\n * The precision calculated this way will always be greater than or equal 0.\n * @default 1\n */\n step?: number;\n\n /**\n * Large difference between two values. This should be greater than `step` and is used\n * when users hit the Page Up or Page Down keys.\n * @default 1\n */\n stepPage?: number;\n\n /**\n * Current value of the control (assumed to be valid).\n *\n * Only provide this if the SpinButton is a controlled component where you are maintaining its\n * current state and passing updates based on change events; otherwise, use the `defaultValue`\n * property.\n *\n * Use `null` to indicate the control has no value.\n *\n * Mutually exclusive with `defaultValue`.\n */\n value?: number | null;\n};\n\n/**\n * State used in rendering SpinButton\n */\nexport type SpinButtonState = ComponentState<SpinButtonSlots> &\n Required<Pick<SpinButtonProps, 'appearance' | 'size'>> & {\n /**\n * State used to track which direction, if any, SpinButton is currently spinning.\n * @default 'rest'\n */\n spinState: SpinButtonSpinState;\n\n /**\n * State used to track if the value is at the range bounds of [min-max].\n * @default 'none'\n */\n atBound: SpinButtonBounds;\n };\n\nexport type SpinButtonChangeEvent =\n | React.MouseEvent<HTMLButtonElement>\n | React.ChangeEvent<HTMLElement>\n | React.FocusEvent<HTMLInputElement>\n | React.KeyboardEvent<HTMLInputElement>;\n\nexport type SpinButtonOnChangeData = {\n value?: number | null;\n displayValue?: string;\n};\n\nexport type SpinButtonSpinState = 'rest' | 'up' | 'down';\nexport type SpinButtonBounds = 'none' | 'min' | 'max' | 'both';\n"],"mappings":""}
|
@@ -1,13 +1,12 @@
|
|
1
1
|
"use strict";
|
2
|
+
|
2
3
|
Object.defineProperty(exports, "__esModule", {
|
3
|
-
|
4
|
+
value: true
|
4
5
|
});
|
5
|
-
const
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
//# sourceMappingURL=index.js.map
|
12
|
-
|
6
|
+
const tslib_1 = /*#__PURE__*/require("tslib");
|
7
|
+
tslib_1.__exportStar(require("./SpinButton"), exports);
|
8
|
+
tslib_1.__exportStar(require("./SpinButton.types"), exports);
|
9
|
+
tslib_1.__exportStar(require("./renderSpinButton"), exports);
|
10
|
+
tslib_1.__exportStar(require("./useSpinButton"), exports);
|
11
|
+
tslib_1.__exportStar(require("./useSpinButtonStyles"), exports);
|
13
12
|
//# sourceMappingURL=index.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["
|
1
|
+
{"version":3,"names":["tslib_1","__exportStar","require","exports"],"sources":["../../../../../../../../../packages/react-components/react-spinbutton/src/components/SpinButton/index.ts"],"sourcesContent":["export * from './SpinButton';\nexport * from './SpinButton.types';\nexport * from './renderSpinButton';\nexport * from './useSpinButton';\nexport * from './useSpinButtonStyles';\n"],"mappings":";;;;;;AAAAA,OAAA,CAAAC,YAAA,CAAAC,OAAA,kBAAAC,OAAA;AACAH,OAAA,CAAAC,YAAA,CAAAC,OAAA,wBAAAC,OAAA;AACAH,OAAA,CAAAC,YAAA,CAAAC,OAAA,wBAAAC,OAAA;AACAH,OAAA,CAAAC,YAAA,CAAAC,OAAA,qBAAAC,OAAA;AACAH,OAAA,CAAAC,YAAA,CAAAC,OAAA,2BAAAC,OAAA"}
|
@@ -1,17 +1,28 @@
|
|
1
1
|
"use strict";
|
2
|
+
|
2
3
|
Object.defineProperty(exports, "__esModule", {
|
3
|
-
|
4
|
-
});
|
5
|
-
Object.defineProperty(exports, "renderSpinButton_unstable", {
|
6
|
-
enumerable: true,
|
7
|
-
get: ()=>renderSpinButton_unstable
|
4
|
+
value: true
|
8
5
|
});
|
9
|
-
|
10
|
-
const
|
11
|
-
const
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
6
|
+
exports.renderSpinButton_unstable = void 0;
|
7
|
+
const React = /*#__PURE__*/require("react");
|
8
|
+
const react_utilities_1 = /*#__PURE__*/require("@fluentui/react-utilities");
|
9
|
+
/**
|
10
|
+
* Render the final JSX of SpinButton
|
11
|
+
*/
|
12
|
+
const renderSpinButton_unstable = state => {
|
13
|
+
const {
|
14
|
+
slots,
|
15
|
+
slotProps
|
16
|
+
} = react_utilities_1.getSlots(state);
|
17
|
+
return React.createElement(slots.root, {
|
18
|
+
...slotProps.root
|
19
|
+
}, React.createElement(slots.input, {
|
20
|
+
...slotProps.input
|
21
|
+
}), React.createElement(slots.incrementButton, {
|
22
|
+
...slotProps.incrementButton
|
23
|
+
}), React.createElement(slots.decrementButton, {
|
24
|
+
...slotProps.decrementButton
|
25
|
+
}));
|
26
|
+
};
|
27
|
+
exports.renderSpinButton_unstable = renderSpinButton_unstable;
|
17
28
|
//# sourceMappingURL=renderSpinButton.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["
|
1
|
+
{"version":3,"names":["React","require","react_utilities_1","renderSpinButton_unstable","state","slots","slotProps","getSlots","createElement","root","input","incrementButton","decrementButton","exports"],"sources":["../../../../../../../../../packages/react-components/react-spinbutton/src/components/SpinButton/renderSpinButton.tsx"],"sourcesContent":["import * as React from 'react';\nimport { getSlots } from '@fluentui/react-utilities';\nimport type { SpinButtonState, SpinButtonSlots } from './SpinButton.types';\n\n/**\n * Render the final JSX of SpinButton\n */\nexport const renderSpinButton_unstable = (state: SpinButtonState) => {\n const { slots, slotProps } = getSlots<SpinButtonSlots>(state);\n\n return (\n <slots.root {...slotProps.root}>\n <slots.input {...slotProps.input} />\n <slots.incrementButton {...slotProps.incrementButton} />\n <slots.decrementButton {...slotProps.decrementButton} />\n </slots.root>\n );\n};\n"],"mappings":";;;;;;AAAA,MAAAA,KAAA,gBAAAC,OAAA;AACA,MAAAC,iBAAA,gBAAAD,OAAA;AAGA;;;AAGO,MAAME,yBAAyB,GAAIC,KAAsB,IAAI;EAClE,MAAM;IAAEC,KAAK;IAAEC;EAAS,CAAE,GAAGJ,iBAAA,CAAAK,QAAQ,CAAkBH,KAAK,CAAC;EAE7D,OACEJ,KAAA,CAAAQ,aAAA,CAACH,KAAK,CAACI,IAAI;IAAA,GAAKH,SAAS,CAACG;EAAI,GAC5BT,KAAA,CAAAQ,aAAA,CAACH,KAAK,CAACK,KAAK;IAAA,GAAKJ,SAAS,CAACI;EAAK,EAAI,EACpCV,KAAA,CAAAQ,aAAA,CAACH,KAAK,CAACM,eAAe;IAAA,GAAKL,SAAS,CAACK;EAAe,EAAI,EACxDX,KAAA,CAAAQ,aAAA,CAACH,KAAK,CAACO,eAAe;IAAA,GAAKN,SAAS,CAACM;EAAe,EAAI,CAC7C;AAEjB,CAAC;AAVYC,OAAA,CAAAV,yBAAyB,GAAAA,yBAAA"}
|
@@ -1,260 +1,273 @@
|
|
1
1
|
"use strict";
|
2
|
+
|
2
3
|
Object.defineProperty(exports, "__esModule", {
|
3
|
-
|
4
|
-
});
|
5
|
-
Object.defineProperty(exports, "useSpinButton_unstable", {
|
6
|
-
enumerable: true,
|
7
|
-
get: ()=>useSpinButton_unstable
|
4
|
+
value: true
|
8
5
|
});
|
9
|
-
|
10
|
-
const
|
11
|
-
const
|
12
|
-
const
|
13
|
-
const
|
14
|
-
const
|
15
|
-
const
|
6
|
+
exports.useSpinButton_unstable = void 0;
|
7
|
+
const React = /*#__PURE__*/require("react");
|
8
|
+
const react_utilities_1 = /*#__PURE__*/require("@fluentui/react-utilities");
|
9
|
+
const keyboard_keys_1 = /*#__PURE__*/require("@fluentui/keyboard-keys");
|
10
|
+
const index_1 = /*#__PURE__*/require("../../utils/index");
|
11
|
+
const react_icons_1 = /*#__PURE__*/require("@fluentui/react-icons");
|
12
|
+
const react_shared_contexts_1 = /*#__PURE__*/require("@fluentui/react-shared-contexts");
|
16
13
|
const DEFAULT_SPIN_DELAY_MS = 150;
|
17
14
|
const MIN_SPIN_DELAY_MS = 80;
|
18
15
|
const MAX_SPIN_TIME_MS = 1000;
|
19
16
|
// This is here to give an ease for the mouse held down case.
|
20
17
|
// Exact easing it to be defined. Once it is we'll likely
|
21
18
|
// pull this out into a util function in the SpinButton package.
|
22
|
-
const lerp = (start, end, percent)=>start + (end - start) * percent;
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
}
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
const displayValueChanged = newDisplayValue !== undefined && internalState.current.previousTextValue !== undefined && internalState.current.previousTextValue !== newDisplayValue;
|
160
|
-
let roundedValue;
|
161
|
-
if (valueChanged) {
|
162
|
-
roundedValue = (0, _index.precisionRound)(newValue, precision);
|
163
|
-
setCurrentValue(roundedValue);
|
164
|
-
} else if (displayValueChanged && !isControlled) {
|
165
|
-
const nextValue = parseFloat(newDisplayValue);
|
166
|
-
if (!isNaN(nextValue)) {
|
167
|
-
setCurrentValue((0, _index.precisionRound)(nextValue, precision));
|
168
|
-
}
|
169
|
-
}
|
170
|
-
if (valueChanged || displayValueChanged) {
|
171
|
-
onChange?.(e, {
|
172
|
-
value: roundedValue,
|
173
|
-
displayValue: newDisplayValue
|
174
|
-
});
|
175
|
-
}
|
19
|
+
const lerp = (start, end, percent) => start + (end - start) * percent;
|
20
|
+
/**
|
21
|
+
* Create the state required to render SpinButton.
|
22
|
+
*
|
23
|
+
* The returned state can be modified with hooks such as useSpinButtonStyles_unstable,
|
24
|
+
* before being passed to renderSpinButton_unstable.
|
25
|
+
*
|
26
|
+
* @param props - props from this instance of SpinButton
|
27
|
+
* @param ref - reference to root HTMLElement of SpinButton
|
28
|
+
*/
|
29
|
+
const useSpinButton_unstable = (props, ref) => {
|
30
|
+
var _a, _b;
|
31
|
+
const nativeProps = react_utilities_1.getPartitionedNativeProps({
|
32
|
+
props,
|
33
|
+
primarySlotTagName: 'input',
|
34
|
+
excludedPropNames: ['defaultValue', 'max', 'min', 'onChange', 'size', 'value']
|
35
|
+
});
|
36
|
+
const overrides = react_shared_contexts_1.useOverrides_unstable();
|
37
|
+
const {
|
38
|
+
value,
|
39
|
+
displayValue,
|
40
|
+
defaultValue,
|
41
|
+
min,
|
42
|
+
max,
|
43
|
+
step = 1,
|
44
|
+
stepPage = 1,
|
45
|
+
precision: precisionFromProps,
|
46
|
+
onChange,
|
47
|
+
size = 'medium',
|
48
|
+
appearance = (_a = overrides.inputDefaultAppearance) !== null && _a !== void 0 ? _a : 'outline',
|
49
|
+
root,
|
50
|
+
input,
|
51
|
+
incrementButton,
|
52
|
+
decrementButton
|
53
|
+
} = props;
|
54
|
+
const precision = React.useMemo(() => {
|
55
|
+
return precisionFromProps !== null && precisionFromProps !== void 0 ? precisionFromProps : Math.max(index_1.calculatePrecision(step), 0);
|
56
|
+
}, [precisionFromProps, step]);
|
57
|
+
const [currentValue, setCurrentValue] = react_utilities_1.useControllableState({
|
58
|
+
state: value,
|
59
|
+
defaultState: defaultValue,
|
60
|
+
initialState: 0
|
61
|
+
});
|
62
|
+
const isControlled = value !== undefined;
|
63
|
+
const [textValue, setTextValue] = React.useState(undefined);
|
64
|
+
const [keyboardSpinState, setKeyboardSpinState] = React.useState('rest');
|
65
|
+
const internalState = React.useRef({
|
66
|
+
value: currentValue,
|
67
|
+
spinState: 'rest',
|
68
|
+
spinTime: 0,
|
69
|
+
spinDelay: DEFAULT_SPIN_DELAY_MS,
|
70
|
+
atBound: currentValue !== null ? index_1.getBound(index_1.precisionRound(currentValue, precision), min, max) : 'none'
|
71
|
+
});
|
72
|
+
const [setStepTimeout, clearStepTimeout] = react_utilities_1.useTimeout();
|
73
|
+
const stepValue = (e, direction, startFrom) => {
|
74
|
+
let startValue = internalState.current.value;
|
75
|
+
if (startFrom) {
|
76
|
+
const num = parseFloat(startFrom);
|
77
|
+
if (!isNaN(num)) {
|
78
|
+
startValue = num;
|
79
|
+
}
|
80
|
+
}
|
81
|
+
const val = startValue;
|
82
|
+
const dir = direction === 'up' || direction === 'upPage' ? 1 : -1;
|
83
|
+
const stepSize = direction === 'upPage' || direction === 'downPage' ? stepPage : step;
|
84
|
+
if (val === null) {
|
85
|
+
const stepStart = min === undefined ? 0 : min;
|
86
|
+
const nullStep = index_1.clamp(stepStart + stepSize * dir, min, max);
|
87
|
+
commit(e, nullStep);
|
88
|
+
return;
|
89
|
+
}
|
90
|
+
let newValue = val + stepSize * dir;
|
91
|
+
if (!Number.isNaN(newValue)) {
|
92
|
+
newValue = index_1.clamp(newValue, min, max);
|
93
|
+
}
|
94
|
+
commit(e, newValue);
|
95
|
+
if (internalState.current.spinState !== 'rest') {
|
96
|
+
setStepTimeout(() => {
|
97
|
+
// Ease the step speed a bit
|
98
|
+
internalState.current.spinTime += internalState.current.spinDelay;
|
99
|
+
internalState.current.spinDelay = lerp(DEFAULT_SPIN_DELAY_MS, MIN_SPIN_DELAY_MS, internalState.current.spinTime / MAX_SPIN_TIME_MS);
|
100
|
+
stepValue(e, direction);
|
101
|
+
}, internalState.current.spinDelay);
|
102
|
+
}
|
103
|
+
};
|
104
|
+
const handleInputChange = e => {
|
105
|
+
if (!internalState.current.previousTextValue) {
|
106
|
+
internalState.current.previousTextValue = textValue !== null && textValue !== void 0 ? textValue : String(currentValue);
|
107
|
+
}
|
108
|
+
const newValue = e.target.value;
|
109
|
+
setTextValue(newValue);
|
110
|
+
};
|
111
|
+
const handleIncrementMouseDown = e => {
|
112
|
+
internalState.current.spinState = 'up';
|
113
|
+
stepValue(e, 'up');
|
114
|
+
};
|
115
|
+
const handleDecrementMouseDown = e => {
|
116
|
+
internalState.current.spinState = 'down';
|
117
|
+
stepValue(e, 'down');
|
118
|
+
};
|
119
|
+
const handleStepMouseUpOrLeave = e => {
|
120
|
+
clearStepTimeout();
|
121
|
+
internalState.current.spinState = 'rest';
|
122
|
+
internalState.current.spinDelay = DEFAULT_SPIN_DELAY_MS;
|
123
|
+
internalState.current.spinTime = 0;
|
124
|
+
};
|
125
|
+
const handleBlur = e => {
|
126
|
+
commit(e, currentValue, textValue);
|
127
|
+
internalState.current.previousTextValue = undefined;
|
128
|
+
};
|
129
|
+
const handleKeyDown = e => {
|
130
|
+
let nextKeyboardSpinState = 'rest';
|
131
|
+
if (e.key === keyboard_keys_1.ArrowUp) {
|
132
|
+
stepValue(e, 'up', textValue);
|
133
|
+
nextKeyboardSpinState = 'up';
|
134
|
+
} else if (e.key === keyboard_keys_1.ArrowDown) {
|
135
|
+
stepValue(e, 'down', textValue);
|
136
|
+
nextKeyboardSpinState = 'down';
|
137
|
+
} else if (e.key === keyboard_keys_1.PageUp) {
|
138
|
+
e.preventDefault();
|
139
|
+
stepValue(e, 'upPage', textValue);
|
140
|
+
nextKeyboardSpinState = 'up';
|
141
|
+
} else if (e.key === keyboard_keys_1.PageDown) {
|
142
|
+
e.preventDefault();
|
143
|
+
stepValue(e, 'downPage', textValue);
|
144
|
+
nextKeyboardSpinState = 'down';
|
145
|
+
} else if (!e.shiftKey && e.key === keyboard_keys_1.Home && min !== undefined) {
|
146
|
+
commit(e, min);
|
147
|
+
nextKeyboardSpinState = 'down';
|
148
|
+
} else if (!e.shiftKey && e.key === keyboard_keys_1.End && max !== undefined) {
|
149
|
+
commit(e, max);
|
150
|
+
nextKeyboardSpinState = 'up';
|
151
|
+
} else if (e.key === keyboard_keys_1.Enter) {
|
152
|
+
commit(e, currentValue, textValue);
|
153
|
+
internalState.current.previousTextValue = undefined;
|
154
|
+
} else if (e.key === keyboard_keys_1.Escape) {
|
155
|
+
if (internalState.current.previousTextValue) {
|
176
156
|
setTextValue(undefined);
|
177
|
-
|
178
|
-
|
179
|
-
|
157
|
+
internalState.current.previousTextValue = undefined;
|
158
|
+
}
|
159
|
+
}
|
160
|
+
if (keyboardSpinState !== nextKeyboardSpinState) {
|
161
|
+
setKeyboardSpinState(nextKeyboardSpinState);
|
162
|
+
}
|
163
|
+
};
|
164
|
+
const handleKeyUp = e => {
|
165
|
+
if (keyboardSpinState !== 'rest') {
|
166
|
+
setKeyboardSpinState('rest');
|
167
|
+
internalState.current.spinState = 'rest';
|
168
|
+
}
|
169
|
+
};
|
170
|
+
const commit = (e, newValue, newDisplayValue) => {
|
171
|
+
const valueChanged = newValue !== undefined && currentValue !== newValue;
|
172
|
+
const displayValueChanged = newDisplayValue !== undefined && internalState.current.previousTextValue !== undefined && internalState.current.previousTextValue !== newDisplayValue;
|
173
|
+
let roundedValue;
|
174
|
+
if (valueChanged) {
|
175
|
+
roundedValue = index_1.precisionRound(newValue, precision);
|
176
|
+
setCurrentValue(roundedValue);
|
177
|
+
} else if (displayValueChanged && !isControlled) {
|
178
|
+
const nextValue = parseFloat(newDisplayValue);
|
179
|
+
if (!isNaN(nextValue)) {
|
180
|
+
setCurrentValue(index_1.precisionRound(nextValue, precision));
|
181
|
+
}
|
182
|
+
}
|
183
|
+
if (valueChanged || displayValueChanged) {
|
184
|
+
onChange === null || onChange === void 0 ? void 0 : onChange(e, {
|
185
|
+
value: roundedValue,
|
186
|
+
displayValue: newDisplayValue
|
187
|
+
});
|
188
|
+
}
|
189
|
+
setTextValue(undefined);
|
190
|
+
};
|
191
|
+
const state = {
|
192
|
+
size,
|
193
|
+
appearance,
|
194
|
+
spinState: keyboardSpinState,
|
195
|
+
atBound: internalState.current.atBound,
|
196
|
+
components: {
|
197
|
+
root: 'span',
|
198
|
+
input: 'input',
|
199
|
+
incrementButton: 'button',
|
200
|
+
decrementButton: 'button'
|
201
|
+
},
|
202
|
+
root: react_utilities_1.resolveShorthand(root, {
|
203
|
+
required: true,
|
204
|
+
defaultProps: nativeProps.root
|
205
|
+
}),
|
206
|
+
input: react_utilities_1.resolveShorthand(input, {
|
207
|
+
required: true,
|
208
|
+
defaultProps: {
|
209
|
+
ref,
|
210
|
+
autoComplete: 'off',
|
211
|
+
role: 'spinbutton',
|
180
212
|
appearance,
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
disabled: nativeProps.primary.disabled,
|
220
|
-
"aria-label": "Decrement value",
|
221
|
-
type: "button"
|
222
|
-
}
|
223
|
-
})
|
224
|
-
};
|
225
|
-
let valueToDisplay;
|
226
|
-
if (textValue !== undefined) {
|
227
|
-
valueToDisplay = textValue;
|
228
|
-
} else if (value === null || currentValue === null) {
|
229
|
-
valueToDisplay = displayValue ?? "";
|
230
|
-
internalState.current.value = null;
|
231
|
-
internalState.current.atBound = "none";
|
213
|
+
type: 'text',
|
214
|
+
...nativeProps.primary
|
215
|
+
}
|
216
|
+
}),
|
217
|
+
incrementButton: react_utilities_1.resolveShorthand(incrementButton, {
|
218
|
+
required: true,
|
219
|
+
defaultProps: {
|
220
|
+
tabIndex: -1,
|
221
|
+
children: React.createElement(react_icons_1.ChevronUp16Regular, null),
|
222
|
+
disabled: nativeProps.primary.disabled,
|
223
|
+
'aria-label': 'Increment value',
|
224
|
+
type: 'button'
|
225
|
+
}
|
226
|
+
}),
|
227
|
+
decrementButton: react_utilities_1.resolveShorthand(decrementButton, {
|
228
|
+
required: true,
|
229
|
+
defaultProps: {
|
230
|
+
tabIndex: -1,
|
231
|
+
children: React.createElement(react_icons_1.ChevronDown16Regular, null),
|
232
|
+
disabled: nativeProps.primary.disabled,
|
233
|
+
'aria-label': 'Decrement value',
|
234
|
+
type: 'button'
|
235
|
+
}
|
236
|
+
})
|
237
|
+
};
|
238
|
+
let valueToDisplay;
|
239
|
+
if (textValue !== undefined) {
|
240
|
+
valueToDisplay = textValue;
|
241
|
+
} else if (value === null || currentValue === null) {
|
242
|
+
valueToDisplay = displayValue !== null && displayValue !== void 0 ? displayValue : '';
|
243
|
+
internalState.current.value = null;
|
244
|
+
internalState.current.atBound = 'none';
|
245
|
+
} else {
|
246
|
+
const roundedValue = index_1.precisionRound(currentValue, precision);
|
247
|
+
internalState.current.value = roundedValue;
|
248
|
+
internalState.current.atBound = index_1.getBound(roundedValue, min, max);
|
249
|
+
if (isControlled) {
|
250
|
+
valueToDisplay = displayValue !== null && displayValue !== void 0 ? displayValue : String(roundedValue);
|
232
251
|
} else {
|
233
|
-
|
234
|
-
internalState.current.value = roundedValue;
|
235
|
-
internalState.current.atBound = (0, _index.getBound)(roundedValue, min, max);
|
236
|
-
if (isControlled) {
|
237
|
-
valueToDisplay = displayValue ?? String(roundedValue);
|
238
|
-
} else {
|
239
|
-
valueToDisplay = String(roundedValue);
|
240
|
-
}
|
252
|
+
valueToDisplay = String(roundedValue);
|
241
253
|
}
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
254
|
+
}
|
255
|
+
state.input.value = valueToDisplay;
|
256
|
+
state.input['aria-valuemin'] = min;
|
257
|
+
state.input['aria-valuemax'] = max;
|
258
|
+
state.input['aria-valuenow'] = currentValue !== null && currentValue !== void 0 ? currentValue : undefined;
|
259
|
+
state.input['aria-valuetext'] = (_b = state.input['aria-valuetext']) !== null && _b !== void 0 ? _b : value !== undefined && displayValue || undefined;
|
260
|
+
state.input.onChange = react_utilities_1.mergeCallbacks(state.input.onChange, handleInputChange);
|
261
|
+
state.input.onBlur = react_utilities_1.mergeCallbacks(state.input.onBlur, handleBlur);
|
262
|
+
state.input.onKeyDown = react_utilities_1.mergeCallbacks(state.input.onKeyDown, handleKeyDown);
|
263
|
+
state.input.onKeyUp = react_utilities_1.mergeCallbacks(state.input.onKeyUp, handleKeyUp);
|
264
|
+
state.incrementButton.onMouseDown = react_utilities_1.mergeCallbacks(handleIncrementMouseDown, state.incrementButton.onMouseDown);
|
265
|
+
state.incrementButton.onMouseUp = react_utilities_1.mergeCallbacks(state.incrementButton.onMouseUp, handleStepMouseUpOrLeave);
|
266
|
+
state.incrementButton.onMouseLeave = react_utilities_1.mergeCallbacks(state.incrementButton.onMouseLeave, handleStepMouseUpOrLeave);
|
267
|
+
state.decrementButton.onMouseDown = react_utilities_1.mergeCallbacks(handleDecrementMouseDown, state.decrementButton.onMouseDown);
|
268
|
+
state.decrementButton.onMouseUp = react_utilities_1.mergeCallbacks(state.decrementButton.onMouseUp, handleStepMouseUpOrLeave);
|
269
|
+
state.decrementButton.onMouseLeave = react_utilities_1.mergeCallbacks(state.decrementButton.onMouseLeave, handleStepMouseUpOrLeave);
|
270
|
+
return state;
|
271
|
+
};
|
272
|
+
exports.useSpinButton_unstable = useSpinButton_unstable;
|
260
273
|
//# sourceMappingURL=useSpinButton.js.map
|