feat: tree 공통 컴포넌트 생성
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import React from "react";
|
||||
import { type NodeModel } from "@minoru/react-dnd-treeview";
|
||||
import { type CustomData } from "./types";
|
||||
import { TypeIcon } from "./TypeIcon";
|
||||
|
||||
type Props = {
|
||||
node: NodeModel<CustomData>;
|
||||
depth: number;
|
||||
isOpen: boolean;
|
||||
onToggle: (id: NodeModel["id"]) => void;
|
||||
};
|
||||
|
||||
export const CustomNode: React.FC<Props> = (props) => {
|
||||
const { droppable, id, text } = props.node;
|
||||
const indent = props.depth * 24;
|
||||
|
||||
const handleToggle = (e: React.MouseEvent) => {
|
||||
if (!droppable) return;
|
||||
e.stopPropagation();
|
||||
props.onToggle(id);
|
||||
};
|
||||
|
||||
return (
|
||||
<div
|
||||
style={{ paddingInlineStart: indent }}
|
||||
className="flex cursor-pointer items-center gap-3 rounded-sm p-2 hover:bg-accent"
|
||||
onClick={handleToggle}
|
||||
>
|
||||
<div>
|
||||
<div>
|
||||
<TypeIcon droppable={droppable} isOpen={props.isOpen} />
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<span className="text-[1.4rem] font-medium text-label">{text}</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
import React from "react";
|
||||
import { images } from "@common/assets";
|
||||
|
||||
type Props = {
|
||||
droppable?: boolean;
|
||||
isOpen?: boolean;
|
||||
};
|
||||
|
||||
export const TypeIcon: React.FC<Props> = (props) => {
|
||||
if (props.droppable) {
|
||||
return (
|
||||
<img
|
||||
src={props.isOpen ? images.iconFolderOn : images.iconFolderOff}
|
||||
width={30}
|
||||
/>
|
||||
);
|
||||
}
|
||||
return <img src={images.iconFile} width={22} />;
|
||||
};
|
||||
@@ -0,0 +1,38 @@
|
||||
[
|
||||
{
|
||||
"id": 1,
|
||||
"parent": 0,
|
||||
"droppable": true,
|
||||
"text": "Folder 1"
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"parent": 1,
|
||||
"droppable": false,
|
||||
"text": "File 1-1"
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"parent": 1,
|
||||
"droppable": false,
|
||||
"text": "File 1-2"
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"parent": 0,
|
||||
"droppable": true,
|
||||
"text": "Folder 2"
|
||||
},
|
||||
{
|
||||
"id": 5,
|
||||
"parent": 4,
|
||||
"droppable": true,
|
||||
"text": "Folder 2-1"
|
||||
},
|
||||
{
|
||||
"id": 6,
|
||||
"parent": 5,
|
||||
"droppable": false,
|
||||
"text": "File 2-1-1"
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,68 @@
|
||||
import type { Meta, StoryObj } from "@storybook/react";
|
||||
import { useState } from "react";
|
||||
import { DndProvider } from "react-dnd";
|
||||
import {
|
||||
Tree,
|
||||
MultiBackend,
|
||||
getBackendOptions,
|
||||
type NodeModel,
|
||||
} from "@minoru/react-dnd-treeview";
|
||||
import { type CustomData } from "./types";
|
||||
import { CustomNode } from "./CustomNode";
|
||||
import SampleData from "./__mock__.json";
|
||||
|
||||
const meta: Meta<typeof Tree> = {
|
||||
title: "UI/Tree",
|
||||
component: Tree,
|
||||
parameters: {
|
||||
docs: {
|
||||
description: {
|
||||
component: `
|
||||
- [Docs](https://github.com/minop1205/react-dnd-treeview/blob/next/README.md)
|
||||
`,
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
export default meta;
|
||||
type Story = StoryObj<typeof Tree>;
|
||||
|
||||
export const Default: Story = {
|
||||
render: () => <TreeView />,
|
||||
};
|
||||
|
||||
function TreeView() {
|
||||
const [treeData, setTreeData] = useState<NodeModel<CustomData>[]>(SampleData);
|
||||
const handleDrop = (newTree: NodeModel<CustomData>[]) => setTreeData(newTree);
|
||||
|
||||
return (
|
||||
<DndProvider backend={MultiBackend} options={getBackendOptions()}>
|
||||
<div>
|
||||
<Tree<CustomData>
|
||||
tree={treeData}
|
||||
rootId={0}
|
||||
render={(
|
||||
node: NodeModel<CustomData>,
|
||||
{ depth, isOpen, onToggle },
|
||||
) => (
|
||||
<CustomNode
|
||||
node={node}
|
||||
depth={depth}
|
||||
isOpen={isOpen}
|
||||
onToggle={onToggle}
|
||||
/>
|
||||
)}
|
||||
onDrop={handleDrop}
|
||||
classes={{
|
||||
root: "",
|
||||
container: "flex flex-col",
|
||||
listItem: "",
|
||||
dropTarget: "bg-primary rounded-sm",
|
||||
draggingSource: "",
|
||||
placeholder: "",
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</DndProvider>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
export type CustomData = {
|
||||
fileType: string;
|
||||
fileSize: string;
|
||||
};
|
||||
Reference in New Issue
Block a user