import React, { createContext, useContext, useRef } from "react"; import { AriaRadioGroupProps, AriaRadioProps, VisuallyHidden, useFocusRing } from "react-aria"; import { useRadio, useRadioGroup } from "react-aria"; import { RadioGroupState } from "react-stately"; import { useRadioGroupState } from "react-stately"; import cn from "~/utils/cn"; interface RadioGroupProps extends AriaRadioGroupProps { children: React.ReactElement[]; label: string; className?: string; } const RadioContext = createContext(null); function RadioGroup({ children, label, className, ...props }: RadioGroupProps) { const state = useRadioGroupState(props); const { radioGroupProps, labelProps } = useRadioGroup( { ...props, "aria-label": label, }, state, ); return (
{label} {children}
); } interface RadioProps extends AriaRadioProps { label: string; className?: string; } function Radio({ children, label, className, ...props }: RadioProps) { const state = useContext(RadioContext); const ref = useRef(null); const { inputProps, isSelected, isDisabled } = useRadio( { ...props, "aria-label": label, }, state!, ref, ); const { isFocusVisible, focusProps } = useFocusRing(); return (