import React, { useMemo } from 'react'; import type { Key, ReactElement } from 'react'; import type { StyleProp, ViewStyle } from 'react-native'; import AccordionItem from './AccordionItem'; import { Spacer, StyledWrapper } from './StyledAccordion'; import { usePropsOrInternalState } from '../../utils/hooks'; export interface AccordionProps { /** * List of accordion items to be rendered. Each item must have an unique key. */ items: { header: string | ReactElement; content: ReactElement; key: K; style?: StyleProp; testID?: string; }[]; /** * Key of the active item. * When activeItemKey and onItemPress is passed, the component is fully controlled. * Otherwise, it would be uncontrolled. */ activeItemKey?: K; /** * Callback invoked when an item is pressed. */ onItemPress?: (key: K) => void; /** * Item type. */ variant?: 'default' | 'card'; /** * Additional style. */ style?: StyleProp; /** * Testing id of the component. */ testID?: string; /** * If true, indicates that the Accordion is accessible to screen readers. */ accessible?: boolean; } const Accordion = ({ items, activeItemKey, onItemPress, variant = 'default', style, testID, accessible, }: AccordionProps) => { const defaultValue = useMemo( () => (typeof activeItemKey === 'number' ? NaN : '') as K, [activeItemKey] ); const [_activeItemKey, _onItemPress] = usePropsOrInternalState( defaultValue, activeItemKey, onItemPress ); return ( {items.map(({ key, ...props }, index) => { const open = _activeItemKey === key; return ( {variant === 'card' && index !== 0 && ( )} _onItemPress(open ? defaultValue : key)} variant={variant} /> ); })} ); }; export default Accordion;