|
13 | 13 | import {AriaRadioGroupProps, AriaRadioProps, HoverEvents, Orientation, useFocusRing, useHover, useRadio, useRadioGroup, VisuallyHidden} from 'react-aria';
|
14 | 14 | import {ContextValue, forwardRefType, Provider, RACValidation, removeDataAttributes, RenderProps, SlotProps, useContextProps, useRenderProps, useSlot} from './utils';
|
15 | 15 | import {FieldErrorContext} from './FieldError';
|
16 |
| -import {filterDOMProps, mergeProps} from '@react-aria/utils'; |
| 16 | +import {filterDOMProps, mergeProps, mergeRefs, useObjectRef} from '@react-aria/utils'; |
17 | 17 | import {FormValidationBehaviorContext} from './Form';
|
18 | 18 | import {LabelContext} from './Label';
|
19 | 19 | import {RadioGroupState, useRadioGroupState} from 'react-stately';
|
20 |
| -import React, {createContext, ForwardedRef, forwardRef, useContext, useRef} from 'react'; |
| 20 | +import React, {createContext, ForwardedRef, forwardRef, MutableRefObject, useContext} from 'react'; |
21 | 21 | import {TextContext} from './Text';
|
22 | 22 |
|
23 | 23 | export interface RadioGroupProps extends Omit<AriaRadioGroupProps, 'children' | 'label' | 'description' | 'errorMessage' | 'validationState' | 'validationBehavior'>, RACValidation, RenderProps<RadioGroupRenderProps>, SlotProps {}
|
24 |
| -export interface RadioProps extends Omit<AriaRadioProps, 'children'>, HoverEvents, RenderProps<RadioRenderProps>, SlotProps {} |
| 24 | +export interface RadioProps extends Omit<AriaRadioProps, 'children'>, HoverEvents, RenderProps<RadioRenderProps>, SlotProps { |
| 25 | + /** |
| 26 | + * A ref for the HTML input element. |
| 27 | + */ |
| 28 | + inputRef?: MutableRefObject<HTMLInputElement> |
| 29 | +} |
25 | 30 |
|
26 | 31 | export interface RadioGroupRenderProps {
|
27 | 32 | /**
|
@@ -166,9 +171,13 @@ function RadioGroup(props: RadioGroupProps, ref: ForwardedRef<HTMLDivElement>) {
|
166 | 171 | }
|
167 | 172 |
|
168 | 173 | function Radio(props: RadioProps, ref: ForwardedRef<HTMLLabelElement>) {
|
169 |
| - [props, ref] = useContextProps(props, ref, RadioContext); |
| 174 | + let { |
| 175 | + inputRef: userProvidedInputRef = null, |
| 176 | + ...otherProps |
| 177 | + } = props; |
| 178 | + [props, ref] = useContextProps(otherProps, ref, RadioContext); |
170 | 179 | let state = React.useContext(RadioGroupStateContext)!;
|
171 |
| - let inputRef = useRef<HTMLInputElement>(null); |
| 180 | + let inputRef = useObjectRef(mergeRefs(userProvidedInputRef, props.inputRef !== undefined ? props.inputRef : null)); |
172 | 181 | let {labelProps, inputProps, isSelected, isDisabled, isPressed} = useRadio({
|
173 | 182 | ...removeDataAttributes<RadioProps>(props),
|
174 | 183 | // ReactNode type doesn't allow function children.
|
|
0 commit comments