import * as React from "react"; import { ContainerProps, classList, fireClickOnEnter } from "../../util"; import { useId } from "../../../hooks/useId"; import { AccordionProvider, removeExpanded, setExpanded, useAccordionDispatch, useAccordionState } from "./context"; export interface AccordionProps extends ContainerProps { multiExpand?: boolean; defaultExpandedIds?: string[]; children?: React.ReactElement[] | React.ReactElement; } export interface AccordionItemProps extends ContainerProps { children?: [React.ReactElement, React.ReactElement]; noChevron?: boolean; itemId?: string; onExpandToggled?: (expanded: boolean) => void; } export interface AccordionHeaderProps extends ContainerProps { } export interface AccordionPanelProps extends ContainerProps { } export const Accordion = (props: AccordionProps) => { const { children, id, className, ariaLabel, ariaHidden, ariaDescribedBy, role, multiExpand, defaultExpandedIds } = props; return (
{children}
); }; export const AccordionItem = (props: AccordionItemProps) => { const { children, id, className, ariaLabel, ariaHidden, ariaDescribedBy, role, noChevron, itemId, onExpandToggled, } = props; const { expanded } = useAccordionState(); const dispatch = useAccordionDispatch(); const panelId = itemId ?? useId(); const mappedChildren = React.Children.toArray(children); const isExpanded = expanded.indexOf(panelId) !== -1; const onHeaderClick = React.useCallback(() => { if (isExpanded) { dispatch(removeExpanded(panelId)); } else { dispatch(setExpanded(panelId)); } onExpandToggled?.(!isExpanded); }, [isExpanded]); return (
{isExpanded && mappedChildren[1]}
); } export const AccordionHeader = (props: AccordionHeaderProps) => { const { id, className, ariaLabel, children, } = props; return (
{children}
); } export const AccordionPanel = (props: AccordionPanelProps) => { const { id, className, ariaLabel, children, } = props; return (
{children}
); }