import { useRef } from "react"; import { AriaTabListProps, AriaTabPanelProps, useTab, useTabList, useTabPanel } from "react-aria"; import { Item, Node, TabListState, useTabListState } from "react-stately"; import cn from "~/utils/cn"; export interface OptionsProps extends AriaTabListProps { label: string; className?: string; } function Options({ label, className, ...props }: OptionsProps) { const state = useTabListState(props); const ref = useRef(null); const { tabListProps } = useTabList(props, state, ref); return (
{[...state.collection].map((item) => (
); } export interface OptionsOptionProps { item: Node; state: TabListState; } function Option({ item, state }: OptionsOptionProps) { const { key, rendered } = item; const ref = useRef(null); const { tabProps } = useTab({ key }, state, ref); return (
{rendered}
); } export interface OptionsPanelProps extends AriaTabPanelProps { state: TabListState; } function OptionsPanel({ state, ...props }: OptionsPanelProps) { const ref = useRef(null); const { tabPanelProps } = useTabPanel(props, state, ref); return (
{state.selectedItem?.props.children}
); } export default Object.assign(Options, { Item });