| 40 | } |
| 41 | |
| 42 | export function useSpinButton(props: SpinButtonProps): SpinbuttonAria { |
| 43 | const _async = useRef<number>(undefined); |
| 44 | let { |
| 45 | value, |
| 46 | textValue, |
| 47 | minValue, |
| 48 | maxValue, |
| 49 | isDisabled, |
| 50 | isReadOnly, |
| 51 | isRequired, |
| 52 | onIncrement, |
| 53 | onIncrementPage, |
| 54 | onDecrement, |
| 55 | onDecrementPage, |
| 56 | onDecrementToMin, |
| 57 | onIncrementToMax |
| 58 | } = props; |
| 59 | const stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/spinbutton'); |
| 60 | |
| 61 | let isSpinning = useRef(false); |
| 62 | const clearAsync = useCallback(() => { |
| 63 | clearTimeout(_async.current); |
| 64 | isSpinning.current = false; |
| 65 | }, []); |
| 66 | const clearAsyncEvent = useEffectEvent(() => { |
| 67 | clearAsync(); |
| 68 | }); |
| 69 | |
| 70 | useEffect(() => { |
| 71 | return () => clearAsyncEvent(); |
| 72 | }, []); |
| 73 | |
| 74 | let onKeyDown = e => { |
| 75 | if ( |
| 76 | e.ctrlKey || |
| 77 | e.metaKey || |
| 78 | e.shiftKey || |
| 79 | e.altKey || |
| 80 | isReadOnly || |
| 81 | e.nativeEvent.isComposing |
| 82 | ) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | switch (e.key) { |
| 87 | case 'PageUp': |
| 88 | if (onIncrementPage) { |
| 89 | e.preventDefault(); |
| 90 | onIncrementPage?.(); |
| 91 | break; |
| 92 | } |
| 93 | // fallthrough! |
| 94 | case 'ArrowUp': |
| 95 | case 'Up': |
| 96 | if (onIncrement) { |
| 97 | e.preventDefault(); |
| 98 | onIncrement?.(); |
| 99 | } |