90 lines
2.2 KiB
TypeScript
90 lines
2.2 KiB
TypeScript
"use client";
|
|
|
|
import { createContext, type ReactNode, useCallback, useContext, useMemo, useState } from "react";
|
|
|
|
import { ChevronsDownUp, ChevronsUpDown } from "lucide-react";
|
|
|
|
import { Button } from "@common/components";
|
|
|
|
interface SectionCollapsibleGroupValue {
|
|
batchOpen: boolean;
|
|
batchVersion: number;
|
|
expandAll: () => void;
|
|
collapseAll: () => void;
|
|
}
|
|
|
|
const SectionCollapsibleGroupContext = createContext<SectionCollapsibleGroupValue | null>(null);
|
|
|
|
function useSectionCollapsibleGroup() {
|
|
return useContext(SectionCollapsibleGroupContext);
|
|
}
|
|
|
|
interface SectionCollapsibleGroupProps {
|
|
children: ReactNode;
|
|
}
|
|
|
|
function SectionCollapsibleGroup({ children }: SectionCollapsibleGroupProps) {
|
|
const [batch, setBatch] = useState({ open: true, version: 0 });
|
|
|
|
const expandAll = useCallback(() => {
|
|
setBatch((prev) => ({ open: true, version: prev.version + 1 }));
|
|
}, []);
|
|
|
|
const collapseAll = useCallback(() => {
|
|
setBatch((prev) => ({ open: false, version: prev.version + 1 }));
|
|
}, []);
|
|
|
|
const value = useMemo(
|
|
() => ({
|
|
batchOpen: batch.open,
|
|
batchVersion: batch.version,
|
|
expandAll,
|
|
collapseAll,
|
|
}),
|
|
[batch.open, batch.version, expandAll, collapseAll],
|
|
);
|
|
|
|
return (
|
|
<SectionCollapsibleGroupContext.Provider value={value}>
|
|
{children}
|
|
</SectionCollapsibleGroupContext.Provider>
|
|
);
|
|
}
|
|
|
|
function SectionCollapsibleToggle() {
|
|
const group = useSectionCollapsibleGroup();
|
|
|
|
if (!group) return null;
|
|
|
|
const { batchOpen, expandAll, collapseAll } = group;
|
|
|
|
return (
|
|
<div className="flex gap-2">
|
|
<Button
|
|
type="button"
|
|
size="sm"
|
|
color="white"
|
|
className="gap-1 px-4 tracking-tight"
|
|
aria-expanded={batchOpen}
|
|
onClick={collapseAll}
|
|
>
|
|
<ChevronsDownUp className="shrink-0" />
|
|
전체접기
|
|
</Button>
|
|
<Button
|
|
type="button"
|
|
size="sm"
|
|
color="white"
|
|
className="gap-1 px-4 tracking-tight"
|
|
aria-expanded={batchOpen}
|
|
onClick={expandAll}
|
|
>
|
|
<ChevronsUpDown className="shrink-0" />
|
|
전체펼침
|
|
</Button>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export { SectionCollapsibleGroup, SectionCollapsibleToggle, useSectionCollapsibleGroup };
|