This commit is contained in:
rizrmd 2024-03-13 21:09:11 -07:00
parent 90563c20ed
commit a13b3ef577
3 changed files with 23 additions and 8 deletions

View File

@ -14,6 +14,7 @@ export const Tree = <T extends TreeItem>({
tree,
renderRow,
className,
rowHeight,
onChange,
drag,
}: {
@ -21,6 +22,7 @@ export const Tree = <T extends TreeItem>({
tree: T[];
drag?: boolean;
onChange?: (rows: T[]) => Promise<void> | void;
rowHeight?: number;
renderRow: (arg: { row: T; style: any; dragHandle: any }) => ReactNode;
}) => {
const local = useLocal({
@ -29,8 +31,8 @@ export const Tree = <T extends TreeItem>({
children: generateTreeChildren({
renderRow,
updateNode: (data, arg) => {
if (typeof arg.sort_idx === "number") {
data.sort_idx = arg.sort_idx;
for (const [k, v] of Object.entries(arg)) {
(data as any)[k] = v;
}
local.update[data.id] = { ...data };
@ -86,10 +88,12 @@ export const Tree = <T extends TreeItem>({
}
}}
>
{local.width && local.height && (
{local.width > 0 && local.height > 0 && (
<Arborist
initialData={local.tree}
width={local.width}
indent={10}
rowHeight={rowHeight}
height={local.height}
disableDrag={drag === false}
children={local.children}
@ -120,10 +124,12 @@ const formatTree = <T extends TreeItem>(tree: T[]) => {
parent.children = [];
}
if (!parent.children.find((e) => e.id === s.id)) {
parent.children.push(s);
scan(s.id, s as T);
}
}
}
};
scan(null, {
@ -145,9 +151,17 @@ const generateTreeChildren = <T extends TreeItem>(arg: {
return (({ node, style, dragHandle }) => {
let sort_idx = node.data.sort_idx;
if (typeof sort_idx === "string") sort_idx = parseInt(sort_idx);
if (!node.isDragging && sort_idx !== node.rowIndex) {
let parent_id: any = node.parent?.id;
if (parent_id === "__REACT_ARBORIST_INTERNAL_ROOT__") parent_id = null;
if (!node.isDragging) {
if (sort_idx !== node.rowIndex) {
updateNode(node.data, { sort_idx: node.rowIndex } as Partial<T>);
}
if (parent_id !== node.data.id_parent) {
updateNode(node.data, { id_parent: parent_id } as Partial<T>);
}
}
return renderRow({ row: node.data, style, dragHandle });
}) as ElementType<NodeRendererProps<T>>;

View File

@ -6,7 +6,7 @@ function Skeleton({
}: React.HTMLAttributes<HTMLDivElement>) {
return (
<div
className={cn("c-animate-pulse c-rounded-md c-bg-muted", className)}
className={cn("c-animate-pulse c-rounded-md c-bg-slate-100", className)}
{...props}
/>
)

1
utils/globals.d.ts vendored
View File

@ -6,3 +6,4 @@ declare var params: any;
declare var cx: any;
declare var preload: (urls: string[] | string) => any;
declare var navigate: (link: string) => void;
declare var siteurl: (path: string) => string;