import React, { ComponentType, ReactNode } from "react";
import { cn } from "@sparkle/lib/utils";
import { Icon } from "./Icon";
import { ListItem } from "./ListItem";
type ContextItemProps = {
action?: ReactNode;
children?: ReactNode;
className?: string;
hasSeparator?: boolean;
hasSeparatorIfLast?: boolean;
subElement?: ReactNode;
title: ReactNode;
visual: ReactNode;
hoverAction?: boolean;
onClick?: () => void;
truncateSubElement?: boolean;
};
export function ContextItem({
action,
children,
className,
hasSeparator = true,
hasSeparatorIfLast = false,
subElement,
title,
visual,
hoverAction,
onClick,
truncateSubElement,
}: ContextItemProps) {
return (
{visual}
{title}
{subElement &&
(truncateSubElement ? (
) : (
{subElement}
))}
{children &&
{children}
}
{action}
);
}
interface ContextItemListProps {
children: ReactNode;
className?: string;
hasBorder?: boolean;
}
ContextItem.List = function ({
children,
className,
hasBorder,
}: ContextItemListProps) {
// Ensure all children are of type ContextItem or ContextItem.SectionHeader
React.Children.forEach(children, (child) => {
if (child === null || child === undefined) {
return;
}
if (
!React.isValidElement(child) ||
(child.type !== ContextItem &&
child.type !== ContextItem.SectionHeader &&
// all children of child must be of type ContextItem or ContextItem.SectionHeader
React.Children.toArray(child.props.children).some(
(c) =>
!React.isValidElement(c) ||
(c.type !== ContextItem && c.type !== ContextItem.SectionHeader)
))
) {
throw new Error(
"All children of ContextItem.List must be of type ContextItem or ContextItem.SectionHeader"
);
}
});
return (
{children}
);
};
interface ContextItemDescriptionProps {
children?: ReactNode;
description?: string;
}
ContextItem.Description = function ({
children,
description,
}: ContextItemDescriptionProps) {
return (
<>
{description && (
{description}
)}
{children && <>{children}>}
>
);
};
interface ContextItemVisualProps {
visual?: ComponentType<{ className?: string }>;
}
ContextItem.Visual = function ({ visual }: ContextItemVisualProps) {
return ;
};
interface ItemSectionHeaderProps {
title: string;
description?: string;
hasBorder?: boolean;
}
ContextItem.SectionHeader = function ({
title,
description,
hasBorder = true,
}: ItemSectionHeaderProps) {
return (
{title}
{description && (
{description}
)}
);
};