2026-10-24 02:13:30 +00:00
|
|
|
import { Button } from "@repo/shadcn-ui/components/button";
|
|
|
|
|
import type { ReactNode } from "react";
|
|
|
|
|
import { useId, useState } from "react";
|
|
|
|
|
|
|
|
|
|
/** Resumen local con ampliación explícita: ningún elemento queda inaccesible. */
|
|
|
|
|
export function CollectionPreview<T>({
|
|
|
|
|
items,
|
|
|
|
|
limit,
|
|
|
|
|
expandLabel,
|
|
|
|
|
collapseLabel,
|
|
|
|
|
children,
|
2026-10-24 23:19:17 +00:00
|
|
|
expanded: controlledExpanded,
|
|
|
|
|
onExpandedChange,
|
2026-10-24 02:13:30 +00:00
|
|
|
}: {
|
|
|
|
|
items: readonly T[];
|
|
|
|
|
limit: number;
|
|
|
|
|
expandLabel: string;
|
|
|
|
|
collapseLabel: string;
|
|
|
|
|
children: (items: readonly T[]) => ReactNode;
|
2026-10-24 23:19:17 +00:00
|
|
|
expanded?: boolean;
|
|
|
|
|
onExpandedChange?: (expanded: boolean) => void;
|
2026-10-24 02:13:30 +00:00
|
|
|
}) {
|
2026-10-24 23:19:17 +00:00
|
|
|
const [localExpanded, setLocalExpanded] = useState(false);
|
|
|
|
|
const expanded = controlledExpanded ?? localExpanded;
|
2026-10-24 02:13:30 +00:00
|
|
|
const id = useId();
|
|
|
|
|
const previewLimit = Number.isSafeInteger(limit) && limit > 0 ? limit : items.length;
|
|
|
|
|
const limited = items.length > previewLimit;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex min-w-0 flex-col gap-2" data-slot="collection-preview">
|
|
|
|
|
<div id={id}>{children(limited && !expanded ? items.slice(0, previewLimit) : items)}</div>
|
|
|
|
|
{limited && (
|
|
|
|
|
<Button
|
|
|
|
|
aria-controls={id}
|
|
|
|
|
aria-expanded={expanded}
|
|
|
|
|
className="w-fit"
|
2026-10-24 23:19:17 +00:00
|
|
|
onClick={() => {
|
|
|
|
|
if (controlledExpanded === undefined) {
|
|
|
|
|
setLocalExpanded(!expanded);
|
|
|
|
|
}
|
|
|
|
|
onExpandedChange?.(!expanded);
|
|
|
|
|
}}
|
2026-10-24 02:13:30 +00:00
|
|
|
size="sm"
|
|
|
|
|
type="button"
|
|
|
|
|
variant="ghost"
|
|
|
|
|
>
|
|
|
|
|
{expanded ? collapseLabel : expandLabel}
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|