| 66 | * Color fields allow users to enter and adjust a hex color value. |
| 67 | */ |
| 68 | export function useColorField( |
| 69 | props: AriaColorFieldProps, |
| 70 | state: ColorFieldState, |
| 71 | ref: RefObject<HTMLInputElement | null> |
| 72 | ): ColorFieldAria { |
| 73 | let {isDisabled, isReadOnly, isRequired, isWheelDisabled, validationBehavior = 'aria'} = props; |
| 74 | |
| 75 | let {colorValue, inputValue, increment, decrement, incrementToMax, decrementToMin, commit} = |
| 76 | state; |
| 77 | |
| 78 | let inputId = useId(); |
| 79 | let {spinButtonProps} = useSpinButton({ |
| 80 | isDisabled, |
| 81 | isReadOnly, |
| 82 | isRequired, |
| 83 | maxValue: 0xffffff, |
| 84 | minValue: 0, |
| 85 | onIncrement: increment, |
| 86 | onIncrementToMax: incrementToMax, |
| 87 | onDecrement: decrement, |
| 88 | onDecrementToMin: decrementToMin, |
| 89 | value: colorValue ? colorValue.toHexInt() : undefined, |
| 90 | textValue: colorValue ? colorValue.toString('hex') : undefined |
| 91 | }); |
| 92 | |
| 93 | let [focusWithin, setFocusWithin] = useState(false); |
| 94 | let {focusWithinProps} = useFocusWithin({isDisabled, onFocusWithinChange: setFocusWithin}); |
| 95 | |
| 96 | let onWheel = useCallback( |
| 97 | e => { |
| 98 | if (Math.abs(e.deltaY) <= Math.abs(e.deltaX)) { |
| 99 | return; |
| 100 | } |
| 101 | if (e.deltaY > 0) { |
| 102 | increment(); |
| 103 | } else if (e.deltaY < 0) { |
| 104 | decrement(); |
| 105 | } |
| 106 | }, |
| 107 | [decrement, increment] |
| 108 | ); |
| 109 | // If the input isn't supposed to receive input, disable scrolling. |
| 110 | let scrollingDisabled = isWheelDisabled || isDisabled || isReadOnly || !focusWithin; |
| 111 | useScrollWheel({onScroll: onWheel, isDisabled: scrollingDisabled}, ref); |
| 112 | |
| 113 | let onChange = value => { |
| 114 | if (state.validate(value)) { |
| 115 | state.setInputValue(value); |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | let {inputProps, ...otherProps} = useFormattedTextField( |
| 120 | { |
| 121 | ...props, |
| 122 | id: inputId, |
| 123 | value: inputValue, |
| 124 | // Intentionally invalid value that will be ignored by onChange during form reset |
| 125 | // This is handled separately below. |