import { Node } from "react-stately"; import { ItemProps as RSItemProps, SectionProps as RSSectionProps, Item as RSItem, Section as RSSection, } from "react-stately"; import { createBranchComponent, createLeafComponent, } from "@react-aria/collections"; import { createContext, useContext } from "react"; import { copyStaticProperties } from "@utils/clone"; import { Key, RenderBaseProps, RenderStyleProps } from "../types"; import { LinkDOMProps } from "@react-types/shared"; export interface ItemRenderProps { isSelected: boolean; isFocused: boolean; isFocusVisible: boolean; } export interface ElementItemProps extends RenderBaseProps, LinkDOMProps { /** Id for this item */ id?: Key; /** Associated callback when an action is performed on this item*/ onAction?: () => void; /** A string representation of the item's contents, used for features like typeahead. */ textValue?: string; /** An accessibility label for this item. */ "aria-label"?: string; // /** A list of child item objects. Used for dynamic collections. */ // childItems?: Iterable; // /** Whether this item has children, even if not loaded yet. */ // hasChildItems?: boolean; } interface ItemContextValue { render: ( props: ElementItemProps, ref: React.ForwardedRef, item: Node ) => React.ReactElement; } export const ItemContext = createContext(null); /** An Item in a Collection component * @example * * Apple * Orange * Banana * */ export const Item = createLeafComponent( "item", ( props: ElementItemProps, ref: React.ForwardedRef, item: Node ) => { const { render } = useContext(ItemContext)!; return render(props, ref, item); } ); copyStaticProperties(RSItem, Item); export interface ElementSectionProps extends RSSectionProps, RenderStyleProps { id?: Key; } interface SectionContextValue { render: ( props: ElementSectionProps, ref: React.ForwardedRef, section: Node ) => React.ReactElement; } export const SectionContext = createContext(null); /** A Section / grouping of items in a collection component * @example * *
* Apple * Orange * Banana *
*
* Carrot * Potato * Onion *
*
*/ export const Section = createBranchComponent( "section", ( props: ElementSectionProps, ref: React.ForwardedRef, section: Node ) => { const { render } = useContext(SectionContext)!; return render(props, ref, section); } ); copyStaticProperties(RSSection, Section); export type { ElementItemProps as ItemProps, ElementSectionProps as SectionProps, };