@fluentui/react-spinbutton 0.0.0-nightly-20220421-0418.1
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.json +173 -0
- package/CHANGELOG.md +71 -0
- package/LICENSE +15 -0
- package/MIGRATION.md +67 -0
- package/README.md +37 -0
- package/Spec.md +370 -0
- package/dist/react-spinbutton.d.ts +192 -0
- package/lib/SpinButton.d.ts +1 -0
- package/lib/SpinButton.js +2 -0
- package/lib/SpinButton.js.map +1 -0
- package/lib/components/SpinButton/SpinButton.d.ts +6 -0
- package/lib/components/SpinButton/SpinButton.js +15 -0
- package/lib/components/SpinButton/SpinButton.js.map +1 -0
- package/lib/components/SpinButton/SpinButton.strings.d.ts +2 -0
- package/lib/components/SpinButton/SpinButton.strings.js +5 -0
- package/lib/components/SpinButton/SpinButton.strings.js.map +1 -0
- package/lib/components/SpinButton/SpinButton.types.d.ts +149 -0
- package/lib/components/SpinButton/SpinButton.types.js +2 -0
- package/lib/components/SpinButton/SpinButton.types.js.map +1 -0
- package/lib/components/SpinButton/index.d.ts +5 -0
- package/lib/components/SpinButton/index.js +6 -0
- package/lib/components/SpinButton/index.js.map +1 -0
- package/lib/components/SpinButton/renderSpinButton.d.ts +5 -0
- package/lib/components/SpinButton/renderSpinButton.js +37 -0
- package/lib/components/SpinButton/renderSpinButton.js.map +1 -0
- package/lib/components/SpinButton/useSpinButton.d.ts +12 -0
- package/lib/components/SpinButton/useSpinButton.js +263 -0
- package/lib/components/SpinButton/useSpinButton.js.map +1 -0
- package/lib/components/SpinButton/useSpinButtonStyles.d.ts +7 -0
- package/lib/components/SpinButton/useSpinButtonStyles.js +367 -0
- package/lib/components/SpinButton/useSpinButtonStyles.js.map +1 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -0
- package/lib/tsdoc-metadata.json +11 -0
- package/lib/utils/clamp.d.ts +1 -0
- package/lib/utils/clamp.js +31 -0
- package/lib/utils/clamp.js.map +1 -0
- package/lib/utils/getBound.d.ts +2 -0
- package/lib/utils/getBound.js +14 -0
- package/lib/utils/getBound.js.map +1 -0
- package/lib/utils/index.d.ts +3 -0
- package/lib/utils/index.js +4 -0
- package/lib/utils/index.js.map +1 -0
- package/lib/utils/precision.d.ts +14 -0
- package/lib/utils/precision.js +41 -0
- package/lib/utils/precision.js.map +1 -0
- package/lib-commonjs/SpinButton.d.ts +1 -0
- package/lib-commonjs/SpinButton.js +10 -0
- package/lib-commonjs/SpinButton.js.map +1 -0
- package/lib-commonjs/components/SpinButton/SpinButton.d.ts +6 -0
- package/lib-commonjs/components/SpinButton/SpinButton.js +26 -0
- package/lib-commonjs/components/SpinButton/SpinButton.js.map +1 -0
- package/lib-commonjs/components/SpinButton/SpinButton.strings.d.ts +2 -0
- package/lib-commonjs/components/SpinButton/SpinButton.strings.js +11 -0
- package/lib-commonjs/components/SpinButton/SpinButton.strings.js.map +1 -0
- package/lib-commonjs/components/SpinButton/SpinButton.types.d.ts +149 -0
- package/lib-commonjs/components/SpinButton/SpinButton.types.js +6 -0
- package/lib-commonjs/components/SpinButton/SpinButton.types.js.map +1 -0
- package/lib-commonjs/components/SpinButton/index.d.ts +5 -0
- package/lib-commonjs/components/SpinButton/index.js +18 -0
- package/lib-commonjs/components/SpinButton/index.js.map +1 -0
- package/lib-commonjs/components/SpinButton/renderSpinButton.d.ts +5 -0
- package/lib-commonjs/components/SpinButton/renderSpinButton.js +48 -0
- package/lib-commonjs/components/SpinButton/renderSpinButton.js.map +1 -0
- package/lib-commonjs/components/SpinButton/useSpinButton.d.ts +12 -0
- package/lib-commonjs/components/SpinButton/useSpinButton.js +278 -0
- package/lib-commonjs/components/SpinButton/useSpinButton.js.map +1 -0
- package/lib-commonjs/components/SpinButton/useSpinButtonStyles.d.ts +7 -0
- package/lib-commonjs/components/SpinButton/useSpinButtonStyles.js +379 -0
- package/lib-commonjs/components/SpinButton/useSpinButtonStyles.js.map +1 -0
- package/lib-commonjs/index.d.ts +2 -0
- package/lib-commonjs/index.js +40 -0
- package/lib-commonjs/index.js.map +1 -0
- package/lib-commonjs/utils/clamp.d.ts +1 -0
- package/lib-commonjs/utils/clamp.js +40 -0
- package/lib-commonjs/utils/clamp.js.map +1 -0
- package/lib-commonjs/utils/getBound.d.ts +2 -0
- package/lib-commonjs/utils/getBound.js +23 -0
- package/lib-commonjs/utils/getBound.js.map +1 -0
- package/lib-commonjs/utils/index.d.ts +3 -0
- package/lib-commonjs/utils/index.js +14 -0
- package/lib-commonjs/utils/index.js.map +1 -0
- package/lib-commonjs/utils/precision.d.ts +14 -0
- package/lib-commonjs/utils/precision.js +52 -0
- package/lib-commonjs/utils/precision.js.map +1 -0
- package/package.json +64 -0
- package/spec-assets/spec-spinbutton-anatomy-alt-optional.png +0 -0
- package/spec-assets/spec-spinbutton-anatomy.png +0 -0
- package/spec-assets/spec-spinbutton-rtl.png +0 -0
@@ -0,0 +1,149 @@
|
|
1
|
+
import type { ComponentProps, ComponentState, Slot } from '@fluentui/react-utilities';
|
2
|
+
import * as React from 'react';
|
3
|
+
export declare type SpinButtonSlots = {
|
4
|
+
/**
|
5
|
+
* The root element of SpinButton is a container `<div>`.
|
6
|
+
* The root slot receives the `className` and `style` specified on the `<SpinButton>`.
|
7
|
+
* All other native props are applied to the primary slot: `input`.
|
8
|
+
*/
|
9
|
+
root: NonNullable<Slot<'span'>>;
|
10
|
+
/**
|
11
|
+
* Input that displays the current value and accepts direct input from the user.
|
12
|
+
* Displayed value is formatted.
|
13
|
+
*
|
14
|
+
* This is the primary slot.
|
15
|
+
*/
|
16
|
+
input: NonNullable<Slot<'input'>>;
|
17
|
+
/**
|
18
|
+
* Renders the increment control.
|
19
|
+
*/
|
20
|
+
incrementButton: NonNullable<Slot<'button'>>;
|
21
|
+
/**
|
22
|
+
* Renders the decrement control.
|
23
|
+
*/
|
24
|
+
decrementButton: NonNullable<Slot<'button'>>;
|
25
|
+
};
|
26
|
+
export declare type SpinButtonCommons = {
|
27
|
+
/**
|
28
|
+
* Initial value of the control (assumed to be valid). Updates to this prop will not be respected.
|
29
|
+
*
|
30
|
+
* Use this if you intend for the SpinButton to be an uncontrolled component which maintains its
|
31
|
+
* own value. For a controlled component, use `value` instead. (Mutually exclusive with `value`.)
|
32
|
+
*/
|
33
|
+
defaultValue: number;
|
34
|
+
/**
|
35
|
+
* Current value of the control (assumed to be valid).
|
36
|
+
*
|
37
|
+
* Only provide this if the SpinButton is a controlled component where you are maintaining its
|
38
|
+
* current state and passing updates based on change events; otherwise, use the `defaultValue`
|
39
|
+
* property. (Mutually exclusive with `defaultValue`.)
|
40
|
+
*/
|
41
|
+
value: number;
|
42
|
+
/**
|
43
|
+
* String representation of `value`.
|
44
|
+
*
|
45
|
+
* Use this when displaying the value to users as something other than a plain number.
|
46
|
+
* For example, when displaying currency values this might be "$1.00" when value is `1`.
|
47
|
+
*
|
48
|
+
* Only provide this if the SpinButton is a controlled component where you are maintaining its
|
49
|
+
* current state and passing updates based on change events. When SpinButton is used as an
|
50
|
+
* uncontrolled component this prop is ignored.
|
51
|
+
*/
|
52
|
+
displayValue: string;
|
53
|
+
/**
|
54
|
+
* Min value of the control. If not provided, the control has no minimum value.
|
55
|
+
*/
|
56
|
+
min: number;
|
57
|
+
/**
|
58
|
+
* Max value of the control. If not provided, the control has no maximum value.
|
59
|
+
*/
|
60
|
+
max: number;
|
61
|
+
/**
|
62
|
+
* Difference between two adjacent values of the control.
|
63
|
+
* This value is used to calculate the precision of the input if no `precision` is given.
|
64
|
+
* The precision calculated this way will always be greater than or equal 0.
|
65
|
+
* @default 1
|
66
|
+
*/
|
67
|
+
step: number;
|
68
|
+
/**
|
69
|
+
* Large difference between two values. This should be greater than `step` and is used
|
70
|
+
* when users hit the Page Up or Page Down keys.
|
71
|
+
* @default 1
|
72
|
+
*/
|
73
|
+
stepPage: number;
|
74
|
+
/**
|
75
|
+
* Callback for when the committed value changes.
|
76
|
+
* - User presses the up/down buttons (on single press or every spin)
|
77
|
+
* - User presses the up/down arrow keys (on single press or every spin)
|
78
|
+
* - User *commits* edits to the input text by focusing away (blurring) or pressing enter.
|
79
|
+
* Note that this is NOT called for every key press while the user is editing.
|
80
|
+
*/
|
81
|
+
onChange: (event: SpinButtonChangeEvent, data: SpinButtonOnChangeData) => void;
|
82
|
+
/**
|
83
|
+
* How many decimal places the value should be rounded to.
|
84
|
+
*
|
85
|
+
* The default is calculated based on the precision of `step`: i.e. if step = 1, precision = 0.
|
86
|
+
* step = 0.0089, precision = 4. step = 300, precision = 2. step = 23.00, precision = 2.
|
87
|
+
*/
|
88
|
+
precision: number;
|
89
|
+
/**
|
90
|
+
* Controls the colors and borders of the input.
|
91
|
+
* @default 'outline'
|
92
|
+
*/
|
93
|
+
appearance: 'outline' | 'underline' | 'filledDarker' | 'filledLighter';
|
94
|
+
/**
|
95
|
+
* Size of the input.
|
96
|
+
* @default 'medium'
|
97
|
+
*/
|
98
|
+
size: 'small' | 'medium';
|
99
|
+
/**
|
100
|
+
* Controls which input types update the value.
|
101
|
+
*
|
102
|
+
* - 'all': both the spinner buttons and input field are enabled.
|
103
|
+
* - 'spinners-only': only the spinner buttons are enabled.
|
104
|
+
* @default all
|
105
|
+
*/
|
106
|
+
inputType: 'all' | 'spinners-only';
|
107
|
+
/**
|
108
|
+
* Strings for localizing text in the control.
|
109
|
+
*/
|
110
|
+
strings?: SpinButtonStrings;
|
111
|
+
};
|
112
|
+
/**
|
113
|
+
* SpinButton Props
|
114
|
+
*/
|
115
|
+
export declare type SpinButtonProps = Omit<ComponentProps<Partial<SpinButtonSlots>, 'input'>, 'onChange' | 'size'> & Partial<SpinButtonCommons>;
|
116
|
+
/**
|
117
|
+
* State used in rendering SpinButton
|
118
|
+
*/
|
119
|
+
export declare type SpinButtonState = ComponentState<SpinButtonSlots> & Partial<SpinButtonCommons> & Pick<SpinButtonCommons, 'appearance' | 'size'> & {
|
120
|
+
/**
|
121
|
+
* State used to track which direction, if any, SpinButton is currently spinning.
|
122
|
+
* @default 'rest'
|
123
|
+
*/
|
124
|
+
spinState: SpinButtonSpinState;
|
125
|
+
/**
|
126
|
+
* State used to track if the value is at the range bounds of [min-max].
|
127
|
+
* @default 'none'
|
128
|
+
*/
|
129
|
+
atBound: SpinButtonBounds;
|
130
|
+
};
|
131
|
+
export declare type SpinButtonChangeEvent = React.MouseEvent<HTMLButtonElement> | React.ChangeEvent<HTMLElement> | React.FocusEvent<HTMLInputElement> | React.KeyboardEvent<HTMLInputElement>;
|
132
|
+
export declare type SpinButtonOnChangeData = {
|
133
|
+
value?: number;
|
134
|
+
displayValue?: string;
|
135
|
+
};
|
136
|
+
export declare type SpinButtonSpinState = 'rest' | 'up' | 'down';
|
137
|
+
export declare type SpinButtonBounds = 'none' | 'min' | 'max' | 'both';
|
138
|
+
export declare type SpinButtonStrings = {
|
139
|
+
/**
|
140
|
+
* Label applied to the increment button.
|
141
|
+
* Can include the token "\{step\}" which will be replaced with the value of the `step` prop.
|
142
|
+
*/
|
143
|
+
incrementButtonLabel: string;
|
144
|
+
/**
|
145
|
+
* Label applied to the decrement button.
|
146
|
+
* Can include the token "\{step\}" which will be replaced with the value of the `step` prop.
|
147
|
+
*/
|
148
|
+
decrementButtonLabel: string;
|
149
|
+
};
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"SpinButton.types.js","sourceRoot":"../src/","sources":["components/SpinButton/SpinButton.types.ts"],"names":[],"mappings":"","sourcesContent":["import type { ComponentProps, ComponentState, Slot } from '@fluentui/react-utilities';\n// import { Input } from '@fluentui/react-input';\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\nexport type SpinButtonCommons = {\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 defaultValue: 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. (Mutually exclusive with `defaultValue`.)\n */\n value: number;\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 * Min value of the control. If not provided, the control has no minimum value.\n */\n min: number;\n\n /**\n * Max value of the control. If not provided, the control has no maximum value.\n */\n max: number;\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 * 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 * Controls the colors and borders of the input.\n * @default 'outline'\n */\n appearance: 'outline' | 'underline' | 'filledDarker' | 'filledLighter';\n\n /**\n * Size of the input.\n * @default 'medium'\n */\n size: 'small' | 'medium';\n\n /**\n * Controls which input types update the value.\n *\n * - 'all': both the spinner buttons and input field are enabled.\n * - 'spinners-only': only the spinner buttons are enabled.\n * @default all\n */\n inputType: 'all' | 'spinners-only';\n\n /**\n * Strings for localizing text in the control.\n */\n strings?: SpinButtonStrings;\n};\n\n/**\n * SpinButton Props\n */\nexport type SpinButtonProps = Omit<ComponentProps<Partial<SpinButtonSlots>, 'input'>, 'onChange' | 'size'> &\n Partial<SpinButtonCommons>;\n\n/**\n * State used in rendering SpinButton\n */\nexport type SpinButtonState = ComponentState<SpinButtonSlots> &\n Partial<SpinButtonCommons> &\n Pick<SpinButtonCommons, '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;\n displayValue?: string;\n};\n\nexport type SpinButtonSpinState = 'rest' | 'up' | 'down';\nexport type SpinButtonBounds = 'none' | 'min' | 'max' | 'both';\n\nexport type SpinButtonStrings = {\n /**\n * Label applied to the increment button.\n * Can include the token \"\\{step\\}\" which will be replaced with the value of the `step` prop.\n */\n incrementButtonLabel: string;\n\n /**\n * Label applied to the decrement button.\n * Can include the token \"\\{step\\}\" which will be replaced with the value of the `step` prop.\n */\n decrementButtonLabel: string;\n};\n"]}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"../src/","sources":["components/SpinButton/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,uBAAuB,CAAC","sourcesContent":["export * from './SpinButton';\nexport * from './SpinButton.types';\nexport * from './renderSpinButton';\nexport * from './useSpinButton';\nexport * from './useSpinButtonStyles';\n"]}
|
@@ -0,0 +1,37 @@
|
|
1
|
+
import * as React from 'react';
|
2
|
+
import { getSlots } from '@fluentui/react-utilities';
|
3
|
+
/**
|
4
|
+
* Render the final JSX of SpinButton
|
5
|
+
*/
|
6
|
+
|
7
|
+
export const renderSpinButton_unstable = state => {
|
8
|
+
// Leaving this here for now.
|
9
|
+
// This is the approach using react-input's Input component.
|
10
|
+
// It has some Typescript problems and feels hacky.
|
11
|
+
// const { slots, slotProps } = getSlots<SpinButtonSlots>(state);
|
12
|
+
// const { contentAfter, ...otherInputSlotProps } = slotProps.input as SpinButtonSlots['input'];
|
13
|
+
// const inputContentAfter = {
|
14
|
+
// ...contentAfter,
|
15
|
+
// children: (
|
16
|
+
// <>
|
17
|
+
// <slots.incrementButton {...slotProps.incrementButton} />
|
18
|
+
// <slots.decrementButton {...slotProps.decrementButton} />
|
19
|
+
// </>
|
20
|
+
// ),
|
21
|
+
// };
|
22
|
+
// return (
|
23
|
+
// <slots.root {...slotProps.root}>
|
24
|
+
// <slots.input {...otherInputSlotProps} contentAfter={inputContentAfter}/>
|
25
|
+
// </slots.root>
|
26
|
+
// );
|
27
|
+
const {
|
28
|
+
slots,
|
29
|
+
slotProps
|
30
|
+
} = getSlots(state);
|
31
|
+
return /*#__PURE__*/React.createElement(slots.root, { ...slotProps.root
|
32
|
+
}, /*#__PURE__*/React.createElement(slots.input, { ...slotProps.input
|
33
|
+
}), /*#__PURE__*/React.createElement(slots.incrementButton, { ...slotProps.incrementButton
|
34
|
+
}), /*#__PURE__*/React.createElement(slots.decrementButton, { ...slotProps.decrementButton
|
35
|
+
}));
|
36
|
+
};
|
37
|
+
//# sourceMappingURL=renderSpinButton.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["components/SpinButton/renderSpinButton.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAZ,MAAuB,OAAvB;AACA,SAAS,QAAT,QAAyB,2BAAzB;AAGA;;AAEG;;AACH,OAAO,MAAM,yBAAyB,GAAI,KAAD,IAA2B;AAClE;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AAEA,QAAM;AAAE,IAAA,KAAF;AAAS,IAAA;AAAT,MAAuB,QAAQ,CAAkB,KAAlB,CAArC;AAEA,sBACE,KAAA,CAAA,aAAA,CAAC,KAAK,CAAC,IAAP,EAAW,EAAA,GAAK,SAAS,CAAC;AAAf,GAAX,eACE,KAAA,CAAA,aAAA,CAAC,KAAK,CAAC,KAAP,EAAY,EAAA,GAAK,SAAS,CAAC;AAAf,GAAZ,CADF,eAEE,KAAA,CAAA,aAAA,CAAC,KAAK,CAAC,eAAP,EAAsB,EAAA,GAAK,SAAS,CAAC;AAAf,GAAtB,CAFF,eAGE,KAAA,CAAA,aAAA,CAAC,KAAK,CAAC,eAAP,EAAsB,EAAA,GAAK,SAAS,CAAC;AAAf,GAAtB,CAHF,CADF;AAOD,CAhCM","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 // Leaving this here for now.\n // This is the approach using react-input's Input component.\n // It has some Typescript problems and feels hacky.\n // const { slots, slotProps } = getSlots<SpinButtonSlots>(state);\n\n // const { contentAfter, ...otherInputSlotProps } = slotProps.input as SpinButtonSlots['input'];\n // const inputContentAfter = {\n // ...contentAfter,\n // children: (\n // <>\n // <slots.incrementButton {...slotProps.incrementButton} />\n // <slots.decrementButton {...slotProps.decrementButton} />\n // </>\n // ),\n // };\n\n // return (\n // <slots.root {...slotProps.root}>\n // <slots.input {...otherInputSlotProps} contentAfter={inputContentAfter}/>\n // </slots.root>\n // );\n\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"],"sourceRoot":"../src/"}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import * as React from 'react';
|
2
|
+
import { SpinButtonProps, SpinButtonState } from './SpinButton.types';
|
3
|
+
/**
|
4
|
+
* Create the state required to render SpinButton.
|
5
|
+
*
|
6
|
+
* The returned state can be modified with hooks such as useSpinButtonStyles_unstable,
|
7
|
+
* before being passed to renderSpinButton_unstable.
|
8
|
+
*
|
9
|
+
* @param props - props from this instance of SpinButton
|
10
|
+
* @param ref - reference to root HTMLElement of SpinButton
|
11
|
+
*/
|
12
|
+
export declare const useSpinButton_unstable: (props: SpinButtonProps, ref: React.Ref<HTMLInputElement>) => SpinButtonState;
|
@@ -0,0 +1,263 @@
|
|
1
|
+
import * as React from 'react';
|
2
|
+
import { getPartitionedNativeProps, resolveShorthand, useControllableState, useMergedEventCallbacks, useTimeout } from '@fluentui/react-utilities';
|
3
|
+
import * as Keys from '@fluentui/keyboard-keys';
|
4
|
+
import { spinButtonDefaultStrings } from './SpinButton.strings';
|
5
|
+
import { calculatePrecision, precisionRound, getBound, clampWhenInRange } from '../../utils/index';
|
6
|
+
import { ChevronUp16Regular, ChevronDown16Regular } from '@fluentui/react-icons';
|
7
|
+
const DEFAULT_SPIN_DELAY_MS = 150;
|
8
|
+
const MIN_SPIN_DELAY_MS = 80;
|
9
|
+
const MAX_SPIN_TIME_MS = 1000; // This is here to give an ease the mouse held down case.
|
10
|
+
// Exact easing it to be defined. Once it is we'll likely
|
11
|
+
// pull this out into a util function in the SpinButton package.
|
12
|
+
|
13
|
+
const lerp = (start, end, percent) => start + (end - start) * percent;
|
14
|
+
/**
|
15
|
+
* Create the state required to render SpinButton.
|
16
|
+
*
|
17
|
+
* The returned state can be modified with hooks such as useSpinButtonStyles_unstable,
|
18
|
+
* before being passed to renderSpinButton_unstable.
|
19
|
+
*
|
20
|
+
* @param props - props from this instance of SpinButton
|
21
|
+
* @param ref - reference to root HTMLElement of SpinButton
|
22
|
+
*/
|
23
|
+
|
24
|
+
|
25
|
+
export const useSpinButton_unstable = (props, ref) => {
|
26
|
+
const nativeProps = getPartitionedNativeProps({
|
27
|
+
props,
|
28
|
+
primarySlotTagName: 'input',
|
29
|
+
excludedPropNames: ['onChange', 'size', 'min', 'max']
|
30
|
+
});
|
31
|
+
const {
|
32
|
+
value,
|
33
|
+
displayValue,
|
34
|
+
defaultValue,
|
35
|
+
min,
|
36
|
+
max,
|
37
|
+
step = 1,
|
38
|
+
stepPage = 1,
|
39
|
+
precision: precisionFromProps,
|
40
|
+
onChange,
|
41
|
+
size = 'medium',
|
42
|
+
appearance = 'outline',
|
43
|
+
root,
|
44
|
+
input,
|
45
|
+
incrementButton,
|
46
|
+
decrementButton,
|
47
|
+
inputType = 'all',
|
48
|
+
strings = spinButtonDefaultStrings
|
49
|
+
} = props;
|
50
|
+
const precision = React.useMemo(() => {
|
51
|
+
return precisionFromProps !== null && precisionFromProps !== void 0 ? precisionFromProps : Math.max(calculatePrecision(step), 0);
|
52
|
+
}, [precisionFromProps, step]);
|
53
|
+
const [currentValue, setCurrentValue] = useControllableState({
|
54
|
+
state: value,
|
55
|
+
defaultState: defaultValue,
|
56
|
+
initialState: 0
|
57
|
+
});
|
58
|
+
const [textValue, setTextValue] = React.useState(value !== undefined && displayValue !== undefined ? displayValue : String(currentValue));
|
59
|
+
const [spinState, setSpinState] = React.useState('rest');
|
60
|
+
const [atBound, setAtBound] = React.useState('none');
|
61
|
+
const internalState = React.useRef({
|
62
|
+
value: currentValue,
|
63
|
+
spinState,
|
64
|
+
spinTime: 0,
|
65
|
+
spinDelay: DEFAULT_SPIN_DELAY_MS
|
66
|
+
});
|
67
|
+
const state = {
|
68
|
+
size,
|
69
|
+
appearance,
|
70
|
+
spinState,
|
71
|
+
atBound,
|
72
|
+
components: {
|
73
|
+
root: 'span',
|
74
|
+
input: 'input',
|
75
|
+
incrementButton: 'button',
|
76
|
+
decrementButton: 'button'
|
77
|
+
},
|
78
|
+
root: resolveShorthand(root, {
|
79
|
+
required: true,
|
80
|
+
defaultProps: nativeProps.root
|
81
|
+
}),
|
82
|
+
input: resolveShorthand(input, {
|
83
|
+
required: true,
|
84
|
+
defaultProps: {
|
85
|
+
ref,
|
86
|
+
autoComplete: 'off',
|
87
|
+
role: 'spinbutton',
|
88
|
+
appearance: appearance,
|
89
|
+
type: 'text',
|
90
|
+
...nativeProps.primary
|
91
|
+
}
|
92
|
+
}),
|
93
|
+
incrementButton: resolveShorthand(incrementButton, {
|
94
|
+
required: true,
|
95
|
+
defaultProps: {
|
96
|
+
tabIndex: -1,
|
97
|
+
children: /*#__PURE__*/React.createElement(ChevronUp16Regular, null),
|
98
|
+
disabled: nativeProps.primary.disabled,
|
99
|
+
'aria-label': strings.incrementButtonLabel.replace('{step}', step.toString())
|
100
|
+
}
|
101
|
+
}),
|
102
|
+
decrementButton: resolveShorthand(decrementButton, {
|
103
|
+
required: true,
|
104
|
+
defaultProps: {
|
105
|
+
tabIndex: -1,
|
106
|
+
children: /*#__PURE__*/React.createElement(ChevronDown16Regular, null),
|
107
|
+
disabled: nativeProps.primary.disabled,
|
108
|
+
'aria-label': strings.decrementButtonLabel.replace('{step}', step.toString())
|
109
|
+
}
|
110
|
+
})
|
111
|
+
};
|
112
|
+
const [setStepTimeout, clearStepTimeout] = useTimeout();
|
113
|
+
React.useEffect(() => {
|
114
|
+
let newTextValue;
|
115
|
+
|
116
|
+
if (value !== undefined) {
|
117
|
+
const roundedValue = precisionRound(value, precision);
|
118
|
+
newTextValue = displayValue !== null && displayValue !== void 0 ? displayValue : String(roundedValue);
|
119
|
+
internalState.current.value = roundedValue;
|
120
|
+
setAtBound(getBound(roundedValue, min, max));
|
121
|
+
} else {
|
122
|
+
newTextValue = String(precisionRound(currentValue, precision));
|
123
|
+
internalState.current.value = currentValue;
|
124
|
+
}
|
125
|
+
|
126
|
+
setTextValue(newTextValue);
|
127
|
+
}, [value, displayValue, currentValue, precision, setAtBound, min, max]);
|
128
|
+
|
129
|
+
const handleInputChange = e => {
|
130
|
+
if (inputType === 'spinners-only') {
|
131
|
+
return;
|
132
|
+
}
|
133
|
+
|
134
|
+
if (!internalState.current.previousTextValue) {
|
135
|
+
internalState.current.previousTextValue = textValue;
|
136
|
+
}
|
137
|
+
|
138
|
+
const newValue = e.target.value;
|
139
|
+
setTextValue(newValue);
|
140
|
+
};
|
141
|
+
|
142
|
+
const stepValue = (e, direction) => {
|
143
|
+
const dir = direction === 'up' || direction === 'upPage' ? 1 : -1;
|
144
|
+
const stepSize = direction === 'upPage' || direction === 'downPage' ? stepPage : step;
|
145
|
+
const val = internalState.current.value;
|
146
|
+
let newValue = val + stepSize * dir;
|
147
|
+
|
148
|
+
if (!Number.isNaN(newValue)) {
|
149
|
+
newValue = clampWhenInRange(val, newValue, min, max);
|
150
|
+
}
|
151
|
+
|
152
|
+
commit(e, newValue);
|
153
|
+
|
154
|
+
if (internalState.current.spinState !== 'rest') {
|
155
|
+
setStepTimeout(() => {
|
156
|
+
// Ease the step speed a bit
|
157
|
+
internalState.current.spinTime += internalState.current.spinDelay;
|
158
|
+
internalState.current.spinDelay = lerp(DEFAULT_SPIN_DELAY_MS, MIN_SPIN_DELAY_MS, internalState.current.spinTime / MAX_SPIN_TIME_MS);
|
159
|
+
stepValue(e, direction);
|
160
|
+
}, internalState.current.spinDelay);
|
161
|
+
}
|
162
|
+
};
|
163
|
+
|
164
|
+
const handleIncrementMouseDown = e => {
|
165
|
+
internalState.current.spinState = 'up';
|
166
|
+
stepValue(e, 'up');
|
167
|
+
};
|
168
|
+
|
169
|
+
const handleDecrementMouseDown = e => {
|
170
|
+
internalState.current.spinState = 'down';
|
171
|
+
stepValue(e, 'down');
|
172
|
+
};
|
173
|
+
|
174
|
+
const handleStepMouseUpOrLeave = e => {
|
175
|
+
clearStepTimeout();
|
176
|
+
internalState.current.spinState = 'rest';
|
177
|
+
internalState.current.spinDelay = DEFAULT_SPIN_DELAY_MS;
|
178
|
+
internalState.current.spinTime = 0;
|
179
|
+
};
|
180
|
+
|
181
|
+
const handleBlur = e => {
|
182
|
+
commit(e, currentValue, textValue);
|
183
|
+
internalState.current.previousTextValue = undefined;
|
184
|
+
};
|
185
|
+
|
186
|
+
const handleKeyDown = e => {
|
187
|
+
if (e.key === Keys.ArrowUp) {
|
188
|
+
stepValue(e, 'up');
|
189
|
+
setSpinState('up');
|
190
|
+
} else if (e.key === Keys.ArrowDown) {
|
191
|
+
stepValue(e, 'down');
|
192
|
+
setSpinState('down');
|
193
|
+
} else if (e.key === Keys.PageUp) {
|
194
|
+
e.preventDefault();
|
195
|
+
stepValue(e, 'upPage');
|
196
|
+
setSpinState('up');
|
197
|
+
} else if (e.key === Keys.PageDown) {
|
198
|
+
e.preventDefault();
|
199
|
+
stepValue(e, 'downPage');
|
200
|
+
setSpinState('down');
|
201
|
+
} else if (!e.shiftKey && e.key === Keys.Home && min !== undefined) {
|
202
|
+
commit(e, min);
|
203
|
+
setSpinState('down');
|
204
|
+
} else if (!e.shiftKey && e.key === Keys.End && max !== undefined) {
|
205
|
+
commit(e, max);
|
206
|
+
setSpinState('up');
|
207
|
+
} else if (e.key === Keys.Enter) {
|
208
|
+
commit(e, currentValue, textValue);
|
209
|
+
setSpinState('rest');
|
210
|
+
} else if (e.key === Keys.Escape) {
|
211
|
+
if (internalState.current.previousTextValue) {
|
212
|
+
setTextValue(internalState.current.previousTextValue);
|
213
|
+
internalState.current.previousTextValue = undefined;
|
214
|
+
}
|
215
|
+
|
216
|
+
setSpinState('rest');
|
217
|
+
} else {
|
218
|
+
setSpinState('rest');
|
219
|
+
}
|
220
|
+
};
|
221
|
+
|
222
|
+
const handleKeyUp = e => {
|
223
|
+
setSpinState('rest');
|
224
|
+
};
|
225
|
+
|
226
|
+
const commit = (e, newValue, newDisplayValue) => {
|
227
|
+
const valueChanged = newValue !== undefined && currentValue !== newValue;
|
228
|
+
const displayValueChanged = newDisplayValue !== undefined && internalState.current.previousTextValue !== undefined && internalState.current.previousTextValue !== newDisplayValue;
|
229
|
+
let roundedValue;
|
230
|
+
|
231
|
+
if (valueChanged) {
|
232
|
+
roundedValue = precisionRound(newValue, precision);
|
233
|
+
setCurrentValue(roundedValue);
|
234
|
+
internalState.current.value = roundedValue;
|
235
|
+
setAtBound(getBound(roundedValue, min, max));
|
236
|
+
}
|
237
|
+
|
238
|
+
if (valueChanged || displayValueChanged) {
|
239
|
+
onChange === null || onChange === void 0 ? void 0 : onChange(e, {
|
240
|
+
value: roundedValue,
|
241
|
+
displayValue: newDisplayValue
|
242
|
+
});
|
243
|
+
}
|
244
|
+
};
|
245
|
+
|
246
|
+
state.input.value = textValue;
|
247
|
+
state.input['aria-valuemin'] = min;
|
248
|
+
state.input['aria-valuemax'] = max;
|
249
|
+
state.input['aria-valuenow'] = currentValue;
|
250
|
+
state.input['aria-valuetext'] = value !== undefined && displayValue || undefined;
|
251
|
+
state.input.onChange = useMergedEventCallbacks(state.input.onChange, handleInputChange);
|
252
|
+
state.input.onBlur = useMergedEventCallbacks(state.input.onBlur, handleBlur);
|
253
|
+
state.input.onKeyDown = useMergedEventCallbacks(state.input.onKeyDown, handleKeyDown);
|
254
|
+
state.input.onKeyUp = useMergedEventCallbacks(state.input.onKeyUp, handleKeyUp);
|
255
|
+
state.incrementButton.onMouseDown = useMergedEventCallbacks(handleIncrementMouseDown, state.incrementButton.onMouseDown);
|
256
|
+
state.incrementButton.onMouseUp = useMergedEventCallbacks(state.incrementButton.onMouseUp, handleStepMouseUpOrLeave);
|
257
|
+
state.incrementButton.onMouseLeave = useMergedEventCallbacks(state.incrementButton.onMouseLeave, handleStepMouseUpOrLeave);
|
258
|
+
state.decrementButton.onMouseDown = useMergedEventCallbacks(handleDecrementMouseDown, state.decrementButton.onMouseDown);
|
259
|
+
state.decrementButton.onMouseUp = useMergedEventCallbacks(state.decrementButton.onMouseUp, handleStepMouseUpOrLeave);
|
260
|
+
state.decrementButton.onMouseLeave = useMergedEventCallbacks(state.decrementButton.onMouseLeave, handleStepMouseUpOrLeave);
|
261
|
+
return state;
|
262
|
+
};
|
263
|
+
//# sourceMappingURL=useSpinButton.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["components/SpinButton/useSpinButton.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAZ,MAAuB,OAAvB;AACA,SACE,yBADF,EAEE,gBAFF,EAGE,oBAHF,EAIE,uBAJF,EAKE,UALF,QAMO,2BANP;AAOA,OAAO,KAAK,IAAZ,MAAsB,yBAAtB;AAQA,SAAS,wBAAT,QAAyC,sBAAzC;AACA,SAAS,kBAAT,EAA6B,cAA7B,EAA6C,QAA7C,EAAuD,gBAAvD,QAA+E,mBAA/E;AACA,SAAS,kBAAT,EAA6B,oBAA7B,QAAyD,uBAAzD;AAUA,MAAM,qBAAqB,GAAG,GAA9B;AACA,MAAM,iBAAiB,GAAG,EAA1B;AACA,MAAM,gBAAgB,GAAG,IAAzB,C,CAEA;AACA;AACA;;AACA,MAAM,IAAI,GAAG,CAAC,KAAD,EAAgB,GAAhB,EAA6B,OAA7B,KAAyD,KAAK,GAAG,CAAC,GAAG,GAAG,KAAP,IAAgB,OAA9F;AAEA;;;;;;;;AAQG;;;AACH,OAAO,MAAM,sBAAsB,GAAG,CAAC,KAAD,EAAyB,GAAzB,KAA8E;AAClH,QAAM,WAAW,GAAG,yBAAyB,CAAC;AAC5C,IAAA,KAD4C;AAE5C,IAAA,kBAAkB,EAAE,OAFwB;AAG5C,IAAA,iBAAiB,EAAE,CAAC,UAAD,EAAa,MAAb,EAAqB,KAArB,EAA4B,KAA5B;AAHyB,GAAD,CAA7C;AAMA,QAAM;AACJ,IAAA,KADI;AAEJ,IAAA,YAFI;AAGJ,I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O,KAAP;AACD,CAvQM","sourcesContent":["import * as React from 'react';\nimport {\n getPartitionedNativeProps,\n resolveShorthand,\n useControllableState,\n useMergedEventCallbacks,\n useTimeout,\n} from '@fluentui/react-utilities';\nimport * as Keys from '@fluentui/keyboard-keys';\nimport {\n SpinButtonProps,\n SpinButtonState,\n SpinButtonSpinState,\n SpinButtonChangeEvent,\n SpinButtonBounds,\n} from './SpinButton.types';\nimport { spinButtonDefaultStrings } from './SpinButton.strings';\nimport { calculatePrecision, precisionRound, getBound, clampWhenInRange } from '../../utils/index';\nimport { ChevronUp16Regular, ChevronDown16Regular } from '@fluentui/react-icons';\n\ntype InternalState = {\n value: number;\n spinState: SpinButtonSpinState;\n spinTime: number;\n spinDelay: number;\n previousTextValue?: string;\n};\n\nconst DEFAULT_SPIN_DELAY_MS = 150;\nconst MIN_SPIN_DELAY_MS = 80;\nconst MAX_SPIN_TIME_MS = 1000;\n\n// This is here to give an ease the mouse held down case.\n// Exact easing it to be defined. Once it is we'll likely\n// pull this out into a util function in the SpinButton package.\nconst lerp = (start: number, end: number, percent: number): number => start + (end - start) * percent;\n\n/**\n * Create the state required to render SpinButton.\n *\n * The returned state can be modified with hooks such as useSpinButtonStyles_unstable,\n * before being passed to renderSpinButton_unstable.\n *\n * @param props - props from this instance of SpinButton\n * @param ref - reference to root HTMLElement of SpinButton\n */\nexport const useSpinButton_unstable = (props: SpinButtonProps, ref: React.Ref<HTMLInputElement>): SpinButtonState => {\n const nativeProps = getPartitionedNativeProps({\n props,\n primarySlotTagName: 'input',\n excludedPropNames: ['onChange', 'size', 'min', 'max'],\n });\n\n const {\n value,\n displayValue,\n defaultValue,\n min,\n max,\n step = 1,\n stepPage = 1,\n precision: precisionFromProps,\n onChange,\n size = 'medium',\n appearance = 'outline',\n root,\n input,\n incrementButton,\n decrementButton,\n inputType = 'all',\n strings = spinButtonDefaultStrings,\n } = props;\n\n const precision = React.useMemo(() => {\n return precisionFromProps ?? Math.max(calculatePrecision(step), 0);\n }, [precisionFromProps, step]);\n\n const [currentValue, setCurrentValue] = useControllableState({\n state: value,\n defaultState: defaultValue,\n initialState: 0,\n });\n const [textValue, setTextValue] = React.useState(\n value !== undefined && displayValue !== undefined ? displayValue : String(currentValue),\n );\n const [spinState, setSpinState] = React.useState<SpinButtonSpinState>('rest');\n const [atBound, setAtBound] = React.useState<SpinButtonBounds>('none');\n\n const internalState = React.useRef<InternalState>({\n value: currentValue,\n spinState,\n spinTime: 0,\n spinDelay: DEFAULT_SPIN_DELAY_MS,\n });\n\n const state: SpinButtonState = {\n size,\n appearance,\n spinState,\n atBound,\n\n components: {\n root: 'span',\n input: 'input',\n incrementButton: 'button',\n decrementButton: 'button',\n },\n root: resolveShorthand(root, {\n required: true,\n defaultProps: nativeProps.root,\n }),\n input: resolveShorthand(input, {\n required: true,\n defaultProps: {\n ref,\n autoComplete: 'off',\n role: 'spinbutton',\n appearance: appearance,\n type: 'text',\n ...nativeProps.primary,\n },\n }),\n incrementButton: resolveShorthand(incrementButton, {\n required: true,\n defaultProps: {\n tabIndex: -1,\n children: <ChevronUp16Regular />,\n disabled: nativeProps.primary.disabled,\n 'aria-label': strings.incrementButtonLabel.replace('{step}', step.toString()),\n },\n }),\n decrementButton: resolveShorthand(decrementButton, {\n required: true,\n defaultProps: {\n tabIndex: -1,\n children: <ChevronDown16Regular />,\n disabled: nativeProps.primary.disabled,\n 'aria-label': strings.decrementButtonLabel.replace('{step}', step.toString()),\n },\n }),\n };\n\n const [setStepTimeout, clearStepTimeout] = useTimeout();\n\n React.useEffect(() => {\n let newTextValue;\n if (value !== undefined) {\n const roundedValue = precisionRound(value, precision);\n newTextValue = displayValue ?? String(roundedValue);\n internalState.current.value = roundedValue;\n setAtBound(getBound(roundedValue, min, max));\n } else {\n newTextValue = String(precisionRound(currentValue, precision));\n internalState.current.value = currentValue;\n }\n setTextValue(newTextValue);\n }, [value, displayValue, currentValue, precision, setAtBound, min, max]);\n\n const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {\n if (inputType === 'spinners-only') {\n return;\n }\n\n if (!internalState.current.previousTextValue) {\n internalState.current.previousTextValue = textValue;\n }\n\n const newValue = e.target.value;\n setTextValue(newValue);\n };\n\n const stepValue = (e: SpinButtonChangeEvent, direction: 'up' | 'down' | 'upPage' | 'downPage') => {\n const dir = direction === 'up' || direction === 'upPage' ? 1 : -1;\n const stepSize = direction === 'upPage' || direction === 'downPage' ? stepPage : step;\n const val = internalState.current.value;\n\n let newValue = val + stepSize * dir;\n if (!Number.isNaN(newValue)) {\n newValue = clampWhenInRange(val, newValue, min, max);\n }\n\n commit(e, newValue);\n\n if (internalState.current.spinState !== 'rest') {\n setStepTimeout(() => {\n // Ease the step speed a bit\n internalState.current.spinTime += internalState.current.spinDelay;\n internalState.current.spinDelay = lerp(\n DEFAULT_SPIN_DELAY_MS,\n MIN_SPIN_DELAY_MS,\n internalState.current.spinTime / MAX_SPIN_TIME_MS,\n );\n stepValue(e, direction);\n }, internalState.current.spinDelay);\n }\n };\n\n const handleIncrementMouseDown = (e: React.MouseEvent<HTMLButtonElement>) => {\n internalState.current.spinState = 'up';\n stepValue(e, 'up');\n };\n\n const handleDecrementMouseDown = (e: React.MouseEvent<HTMLButtonElement>) => {\n internalState.current.spinState = 'down';\n stepValue(e, 'down');\n };\n\n const handleStepMouseUpOrLeave = (e: React.MouseEvent<HTMLButtonElement>) => {\n clearStepTimeout();\n internalState.current.spinState = 'rest';\n internalState.current.spinDelay = DEFAULT_SPIN_DELAY_MS;\n internalState.current.spinTime = 0;\n };\n\n const handleBlur = (e: React.FocusEvent<HTMLInputElement>) => {\n commit(e, currentValue, textValue);\n internalState.current.previousTextValue = undefined;\n };\n\n const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {\n if (e.key === Keys.ArrowUp) {\n stepValue(e, 'up');\n setSpinState('up');\n } else if (e.key === Keys.ArrowDown) {\n stepValue(e, 'down');\n setSpinState('down');\n } else if (e.key === Keys.PageUp) {\n e.preventDefault();\n stepValue(e, 'upPage');\n setSpinState('up');\n } else if (e.key === Keys.PageDown) {\n e.preventDefault();\n stepValue(e, 'downPage');\n setSpinState('down');\n } else if (!e.shiftKey && e.key === Keys.Home && min !== undefined) {\n commit(e, min);\n setSpinState('down');\n } else if (!e.shiftKey && e.key === Keys.End && max !== undefined) {\n commit(e, max);\n setSpinState('up');\n } else if (e.key === Keys.Enter) {\n commit(e, currentValue, textValue);\n setSpinState('rest');\n } else if (e.key === Keys.Escape) {\n if (internalState.current.previousTextValue) {\n setTextValue(internalState.current.previousTextValue);\n internalState.current.previousTextValue = undefined;\n }\n setSpinState('rest');\n } else {\n setSpinState('rest');\n }\n };\n\n const handleKeyUp = (e: React.KeyboardEvent<HTMLInputElement>) => {\n setSpinState('rest');\n };\n\n const commit = (e: SpinButtonChangeEvent, newValue?: number, newDisplayValue?: string) => {\n const valueChanged = newValue !== undefined && currentValue !== newValue;\n const displayValueChanged =\n newDisplayValue !== undefined &&\n internalState.current.previousTextValue !== undefined &&\n internalState.current.previousTextValue !== newDisplayValue;\n\n let roundedValue;\n if (valueChanged) {\n roundedValue = precisionRound(newValue!, precision);\n setCurrentValue(roundedValue);\n internalState.current.value = roundedValue;\n setAtBound(getBound(roundedValue, min, max));\n }\n\n if (valueChanged || displayValueChanged) {\n onChange?.(e, { value: roundedValue, displayValue: newDisplayValue });\n }\n };\n\n state.input.value = textValue;\n state.input['aria-valuemin'] = min;\n state.input['aria-valuemax'] = max;\n state.input['aria-valuenow'] = currentValue;\n state.input['aria-valuetext'] = (value !== undefined && displayValue) || undefined;\n state.input.onChange = useMergedEventCallbacks(state.input.onChange, handleInputChange);\n state.input.onBlur = useMergedEventCallbacks(state.input.onBlur, handleBlur);\n state.input.onKeyDown = useMergedEventCallbacks(state.input.onKeyDown, handleKeyDown);\n state.input.onKeyUp = useMergedEventCallbacks(state.input.onKeyUp, handleKeyUp);\n\n state.incrementButton.onMouseDown = useMergedEventCallbacks(\n handleIncrementMouseDown,\n state.incrementButton.onMouseDown,\n );\n state.incrementButton.onMouseUp = useMergedEventCallbacks(state.incrementButton.onMouseUp, handleStepMouseUpOrLeave);\n state.incrementButton.onMouseLeave = useMergedEventCallbacks(\n state.incrementButton.onMouseLeave,\n handleStepMouseUpOrLeave,\n );\n\n state.decrementButton.onMouseDown = useMergedEventCallbacks(\n handleDecrementMouseDown,\n state.decrementButton.onMouseDown,\n );\n state.decrementButton.onMouseUp = useMergedEventCallbacks(state.decrementButton.onMouseUp, handleStepMouseUpOrLeave);\n state.decrementButton.onMouseLeave = useMergedEventCallbacks(\n state.decrementButton.onMouseLeave,\n handleStepMouseUpOrLeave,\n );\n\n return state;\n};\n"],"sourceRoot":"../src/"}
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import { SlotClassNames } from '@fluentui/react-utilities';
|
2
|
+
import type { SpinButtonSlots, SpinButtonState } from './SpinButton.types';
|
3
|
+
export declare const spinButtonClassNames: SlotClassNames<SpinButtonSlots>;
|
4
|
+
/**
|
5
|
+
* Apply styling to the SpinButton slots based on the state
|
6
|
+
*/
|
7
|
+
export declare const useSpinButtonStyles_unstable: (state: SpinButtonState) => SpinButtonState;
|