( props: AriaRadioProps, state: RadioGroupState, ref: RefObject<HTMLInputElement | null> )
| 72 | * @param ref - Ref to the HTML input element. |
| 73 | */ |
| 74 | export 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(); |
no test coverage detected