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

Function useRadio

packages/react-aria/src/radio/useRadio.ts:74–201  ·  view source on GitHub ↗
(
  props: AriaRadioProps,
  state: RadioGroupState,
  ref: RefObject<HTMLInputElement | null>
)

Source from the content-addressed store, hash-verified

72 * @param ref - Ref to the HTML input element.
73 */
74export function useRadio(
75 props: AriaRadioProps,
76 state: RadioGroupState,
77 ref: RefObject<HTMLInputElement | null>
78): RadioAria {
79 let {
80 value,
81 children,
82 'aria-label': ariaLabel,
83 'aria-labelledby': ariaLabelledby,
84 onPressStart,
85 onPressEnd,
86 onPressChange,
87 onPress,
88 onPressUp,
89 onClick
90 } = props;
91
92 const isDisabled = props.isDisabled || state.isDisabled;
93
94 let hasChildren = children != null;
95 let hasAriaLabel = ariaLabel != null || ariaLabelledby != null;
96 if (!hasChildren && !hasAriaLabel && process.env.NODE_ENV !== 'production') {
97 console.warn(
98 'If you do not provide children, you must specify an aria-label for accessibility'
99 );
100 }
101
102 let checked = state.selectedValue === value;
103
104 let onChange = e => {
105 e.stopPropagation();
106 state.setSelectedValue(value);
107 };
108
109 // Handle press state for keyboard interactions and cases where labelProps is not used.
110 let {pressProps, isPressed} = usePress({
111 onPressStart,
112 onPressEnd,
113 onPressChange,
114 onPress,
115 onPressUp,
116 onClick,
117 isDisabled
118 });
119
120 // Handle press state on the label.
121 let {pressProps: labelProps, isPressed: isLabelPressed} = usePress({
122 onPressStart,
123 onPressEnd,
124 onPressChange,
125 onPressUp,
126 onClick,
127 isDisabled,
128 onPress(e) {
129 onPress?.(e);
130 state.setSelectedValue(value);
131 ref.current?.focus();

Callers 2

RadioGroup.tsxFile · 0.85
Radio.tsxFile · 0.85

Calls 9

usePressFunction · 0.90
useFocusableFunction · 0.90
mergePropsFunction · 0.90
filterDOMPropsFunction · 0.90
useFormResetFunction · 0.90
useFormValidationFunction · 0.90
useSlotId2Function · 0.90
setLastFocusedValueMethod · 0.80
filterMethod · 0.65

Tested by

no test coverage detected