import { Component, createEffect, createSignal, For, JSX, Match, mergeProps, Show, Switch } from 'solid-js'; import { Card } from '../Card/Card.tsx'; import HandlerContext, { HandlerContextType } from './context.ts'; interface AccordingProps { class?: string; /** * ## Variant props * The According is warped in a `` component, * and the variant prop is passed to the `` component. * `` component doesn't have `flat` variant, `flat` is meaning that * the According will be warped in a `
` element rather than a `` component. * */ variant?: 'quartz' | 'glass' | 'lightGlass' | 'flat'; shadow?: boolean; darkMode?: boolean; /** * If true, the `` will be warped in a `` component. */ separate?: boolean; children: JSX.Element[]; /** * If true, only one `` can be expanded at a time. */ mutex?: boolean; onExpand?: (index: number) => void; onCollapse?: (index: number) => void; defaultExpanded?: number[]; } function warpInCard( element: JSX.Element, variant: 'quartz' | 'glass' | 'lightGlass' | 'flat', shadow: boolean, darkMode: boolean, ): JSX.Element { return ( {element}
} > {element} ); } export const According: Component = (props: AccordingProps) => { props = mergeProps(props, { variant: 'quartz' as AccordingProps['variant'], shadow: true, darkMode: false, defaultExpanded: [], mutex: false, }); const [expanded, setExpanded] = createSignal>( new Array(props.children.length).fill(false), ); createEffect(() => { const expandedIndexes = props.defaultExpanded!; const newArray = new Array(props.children.length).fill(false); expandedIndexes.forEach((index) => { newArray[index] = true; }); setExpanded(newArray); }); function getHandlerContext(mutex: boolean): HandlerContextType { if (mutex) { return { onExpand: (index) => { const oldExpanded = expanded().findIndex((value) => value); const newArray = new Array(props.children.length).fill(false); newArray[index] = true; setExpanded(newArray); props.onExpand?.(index); props.onCollapse?.(oldExpanded); }, onCollapse: (index) => { const newArray = new Array(props.children.length).fill(false); setExpanded(newArray); props.onCollapse?.(index); }, } } else { return { onExpand: (index) => { const newArray = expanded().slice(); newArray[index] = true; setExpanded(newArray); props.onExpand?.(index); }, onCollapse: (index) => { const newArray = expanded().slice(); newArray[index] = false; setExpanded(newArray); props.onCollapse?.(index); }, } } } return ( { (child) => warpInCard( child, props.variant!, props.shadow!, props.darkMode!, ) } {props.children}
{props.children}
); };