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