MCPcopy
hub / github.com/adobe/react-spectrum / useColorField

Function useColorField

packages/react-aria/src/color/useColorField.ts:68–158  ·  view source on GitHub ↗
(
  props: AriaColorFieldProps,
  state: ColorFieldState,
  ref: RefObject<HTMLInputElement | null>
)

Source from the content-addressed store, hash-verified

66 * Color fields allow users to enter and adjust a hex color value.
67 */
68export 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.

Callers 3

renderColorFieldHookFunction · 0.90
HexColorFieldFunction · 0.90
HexColorFieldFunction · 0.90

Calls 11

useIdFunction · 0.90
useSpinButtonFunction · 0.90
useFocusWithinFunction · 0.90
useScrollWheelFunction · 0.90
useFormattedTextFieldFunction · 0.90
useFormResetFunction · 0.90
mergePropsFunction · 0.90
toHexIntMethod · 0.65
toStringMethod · 0.65
incrementFunction · 0.50
decrementFunction · 0.50

Tested by 1

renderColorFieldHookFunction · 0.72