Components

TreeView

드래그 앤 드롭을 기능을 갖춘 트리 요소

() => {
const initialItems = [
{
index: 'root',
children: ['1', '2'],
data: 'Root Folder',
isFolder: true,
isChecked: false
},
{
index: '1',
children: ['3'],
data: 'Folder 1',
isFolder: true,
isChecked: true
},
{
index: '2',
children: [],
data: 'File 1',
isFolder: false,
isChecked: false
},
{
index: '3',
children: [],
data: 'File 2',
isFolder: false,
isChecked: false
}
]
return (<TreeView items={initialItems} />)
}

API Reference

이 컴포넌트는 React Complex Tree의 props를 상속받으며 common margin props를 지원합니다.

PropTypeDefault
items
TreeViewItem[]
No default value
options
treeViewOptionsProps
UncontrolledTreeEnvironment props
rootOptions
TreeProps
Tree props

추가적인 자세한 옵션은 React Complex Tree의 Component 부분을 참고 하세요

Examples

Checkbox Example

renderItemTitle 함수를 통해 checkbox 표시 합니다

()=>{
const initialItems = [
{
index: 'root',
children: ['1', '2'],
data: 'Root Folder',
isFolder: true,
isChecked: false
},
{
index: '1',
children: ['3'],
data: 'Folder 1',
isFolder: true,
isChecked: true
},
{
index: '2',
children: [],
data: 'File 1',
isFolder: false,
isChecked: false
},
{
index: '3',
children: [],
data: 'File 2',
isFolder: false,
isChecked: false
}
]
return (
<TreeView
items={initialItems}
options={{
canDragAndDrop: false,
renderItemTitle: ({ title, item, context }) => (
<div style={{ display: 'flex', alignItems: 'center' }}>
<input type='checkbox' checked={item.isChecked} onClick={(e) => e.stopPropagation()} onChange={() => { item.isChecked = !item.isChecked context.selectItem() }} />
<span style={{ marginLeft: '8px' }}>{title}</span>
</div>
)
}}
/>
)
}

Export Example

TreeView컴포넌트의 Ref에서 제공하는 getTreeData 함수를 통해 현재 트리의 데이터를 가져 올 수 있습니다.

""
()=>{
const treeviewRef = React.useRef(null)
const [exportData, setExportData] = React.useState('')
const initialItems = [
{
index: 'root',
children: ['1', '2'],
data: 'Root Folder',
isFolder: true,
isChecked: false
},
{
index: '1',
children: ['3'],
data: 'Folder 1',
isFolder: true,
isChecked: true
},
{
index: '2',
children: [],
data: 'File 1',
isFolder: false,
isChecked: false
},
{
index: '3',
children: [],
data: 'File 2',
isFolder: false,
isChecked: false
}
]
const onClickExport = () => {
setExportData(treeviewRef.current?.getTreeData())
}
return (
<>
<button onClick={onClickExport}>Export</button>
<TreeView
items={initialItems}
options={{
renderItemTitle: ({ title, item, context }) => (
<div style={{ display: 'flex', alignItems: 'center' }}>
<input type='checkbox' checked={item.isChecked} onClick={(e) => e.stopPropagation()} onChange={() => { item.isChecked = !item.isChecked context.selectItem() }} />
<span style={{ marginLeft: '8px' }}>{title}</span>
</div>
)
}}
ref={treeviewRef}
/>
<pre>
{JSON.stringify(exportData)}
</pre>
</>
)
}