import { ReactNode, createContext, useCallback, useContext, useState, } from "react"; interface WidgetStyle { selectClassName?: string; inputClassName?: string; rangeClassName?: string; switchClassName?: string; inputNextLine?: boolean; rangeNextLine?: boolean; } interface ChildrenMeta { type?: "unknown" | "input" | "range"; } export interface ListProps { className?: string; children?: ReactNode; id?: string; isMobileScreen?: boolean; widgetStyle?: WidgetStyle; } export interface ListItemProps { title: string; subTitle?: string; children?: JSX.Element | JSX.Element[]; className?: string; onClick?: () => void; nextline?: boolean; } export const ListContext = createContext< { isMobileScreen?: boolean; update?: (m: ChildrenMeta) => void } & WidgetStyle >({ isMobileScreen: false }); export function ListItem(props: ListItemProps) { const { className = "", onClick, title, subTitle, children, nextline, } = props; const context = useContext(ListContext); const [childrenType, setMeta] = useState("unknown"); const { inputNextLine, rangeNextLine } = context; let internalNextLine; switch (childrenType) { case "input": internalNextLine = !!(nextline || inputNextLine); break; case "range": internalNextLine = !!(nextline || rangeNextLine); break; default: internalNextLine = false; } const updateType = useCallback((m: ChildrenMeta) => { setMeta(m.type); }, []); return (
{title}
{subTitle && (
{subTitle}
)}
{children}
); } function List(props: ListProps) { const { className, children, id, widgetStyle } = props; const { isMobileScreen } = useContext(ListContext); return (
{children}
); } List.ListItem = ListItem; List.ListContext = ListContext; export default List;