복합 API를 갖춘, 스타일 없고 접근 가능한 트리 원시 컴포넌트예요.
출처: 문서
본문
기능 (Features)
- 복합 부품: Root, Nodes, Node, Content, Toggle, ToggleIndicator, Selection, DropIndicator, Label, Filter, Header, Footer, Empty, Loading
- 하나의
<ul role="tree"> 안에 보이는 각 행마다 <li role="treeitem"> 하나씩 있는 평면 DOM
- 3단계 캐스케이드가 있는 선택 모드 single, multiple, checkbox
- 단일 onMove 이벤트가 있는 드래그앤드롭; _validateMove_는 동기 또는 비동기 가능
사용법 (Usage)
import { Tree } from 'primereact/tree';
<Tree.Root value={nodes}>
<Tree.Header>
<Tree.Filter />
</Tree.Header>
<Tree.Nodes>
{({ node }) => (
<Tree.Node uKey={node.key}>
<Tree.Content>
<Tree.Toggle>
<Tree.ToggleIndicator match="expanded">▼</Tree.ToggleIndicator>
<Tree.ToggleIndicator match="collapsed">▶</Tree.ToggleIndicator>
</Tree.Toggle>
<Tree.Label>{node.label}</Tree.Label>
</Tree.Content>
</Tree.Node>
)}
</Tree.Nodes>
<Tree.Footer />
<Tree.Empty />
</Tree.Root>
동작 (Behavior)
평면 렌더링 (Flat Render)
_Tree.Nodes_는 데이터를 위에서 아래로 순회하며 보이는 행마다 렌더 함수를 한 번씩 호출해요. _Tree.Node_는 _uKey_로 데이터를 조회해요. 각 <li>는 --px-tree-node-level CSS 변수(1부터 시작)에 자신의 깊이를 담아요. 이 값을 자신의 들여쓰기 단위와 결합하면 들여쓰기를 만들 수 있어요:
[data-part='content'] {
margin-inline-start: calc((var(--px-tree-node-level, 1) - 1) * var(--p-tree-indent, 1rem));
}
토글 표시기 (Toggle Indicator)
_Tree.Toggle_은 자체적으로 아이콘을 렌더링하지 않아요. 상태별 콘텐츠를 렌더링하려면 내부에 match="expanded", match="collapsed", 또는 match="always"를 갖는 _Tree.ToggleIndicator_를 추가하세요. 리프(leaf) 행에서는 토글이 <span data-part="toggle-spacer">로 대체되어 들여쓰기가 정렬을 유지해요.
선택 (Selection)
_Tree.Selection_은 렌더 prop 기반이에요. Tree.Node 안에서는 isSelected, isPartiallySelected, _toggle_을 노출해요. Tree.Header 안에서는 isAllSelected, isSomeSelected, _toggleAll_을 노출해요. 특정 토글 동작을 강제하려면 mode="radio" | "checkbox" | "single"을 전달하세요.
다형성 (Polymorphism)
모든 하위 컴포넌트는 as prop을 받아요. 다른 요소나 컴포넌트를 전달해 렌더링할 태그를 바꿀 수 있어요.
기본값: Root=div, Nodes=ul, Node=li, Content=div, Toggle=button, ToggleIndicator=span, Selection=Fragment, DropIndicator=div, Label=span, Filter=input, Header=div, Footer=div, Empty=div, Loading=div.
Pass Through
<DocPTViewer name="tree-pt" components={['TreeRoot']} />
API
TreeRoot
| Attribute |
Value |
data-scope |
"tree" |
data-part |
"root" |
data-dragover |
Present while a compatible drag is over the container |
| Label |
Type |
Description |
| root |
TreeRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
| nodes |
TreeRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the flat iterator's DOM element ( <ul role="tree"> ). |
| node |
TreeRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the node's DOM element ( <li> ). |
| content |
TreeRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the content's DOM element (clickable row). |
| label |
TreeRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the label's DOM element. |
| toggle |
TreeRootPassThroughType<ButtonHTMLAttributes> |
Used to pass attributes to the toggle button's DOM element. |
| toggleIndicator |
TreeRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the toggle indicator's DOM element (state-matched icon slot). |
| toggleSpacer |
TreeRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the toggle spacer rendered for leaf nodes. |
| selection |
TreeRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the selection slot (wraps user-provided checkbox/radio). |
| pcFilter |
TreeRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the filter's DOM element. |
| header |
TreeRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the header's DOM element. |
| footer |
TreeRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the footer's DOM element. |
| empty |
TreeRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the empty message's DOM element. |
| dropIndicator |
TreeRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the drop indicator's DOM element. |
| loading |
TreeRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the loading overlay's DOM element. |
TreeNodes
| Attribute |
Value |
data-scope |
"tree" |
data-part |
"nodes" |
role |
"tree" |
| Label |
Type |
Description |
| root |
TreeNodesPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
TreeNode
| Attribute |
Value |
data-scope |
"tree" |
data-part |
"node" |
data-node-key |
Node key |
data-expanded |
Present when expanded (non-leaf) |
data-collapsed |
Present when collapsed (non-leaf) |
data-selected |
Present when selected |
data-leaf |
Present when leaf |
data-disabled |
Present when disabled |
data-focused |
Present on the focused row |
| Label |
Type |
Description |
| root |
TreeNodePassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
TreeContent
| Attribute |
Value |
data-scope |
"tree" |
data-part |
"content" |
data-selected |
Present when selected or checked |
data-selectable |
Present when selectable |
data-expanded |
Present when expanded (non-leaf) |
data-collapsed |
Present when collapsed (non-leaf) |
data-leaf |
Present when leaf |
data-checked |
Present when checked (checkbox mode) |
data-partial-checked |
Present when partially checked |
data-disabled |
Present when disabled |
data-dragover |
Present while a compatible drag is over it |
| Label |
Type |
Description |
| root |
TreeContentPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
TreeToggle
| Attribute |
Value |
data-scope |
"tree" |
data-part |
"toggle" |
data-expanded |
Present when expanded |
data-collapsed |
Present when collapsed |
리프 노드에서는 토글이 <span data-part="toggle-spacer" aria-hidden>으로 대체돼요.
| Label |
Type |
Description |
| root |
TreeTogglePassThroughType<ButtonHTMLAttributes> |
Used to pass attributes to the root's DOM element. |
TreeToggleIndicator
| Attribute |
Value |
data-scope |
"tree" |
data-part |
"toggle-indicator" |
| Label |
Type |
Description |
| root |
TreeToggleIndicatorPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
TreeSelection
렌더 prop 전용이며, 자체 DOM 속성은 없어요.
| Label |
Type |
Description |
| root |
TreeSelectionPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
TreeDropIndicator
일치하는 드롭 지점이 hover될 때만 렌더링돼요. 각 Tree.Node마다 position="before" 하나와 position="after" 하나를 사용하세요.
| Attribute |
Value |
data-scope |
"tree" |
data-part |
"drop-point" |
| Label |
Type |
Description |
| root |
TreeDropIndicatorPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
TreeLabel
| Label |
Type |
Description |
| root |
TreeLabelPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
TreeFilter
| Attribute |
Value |
data-scope |
"tree" |
data-part |
"filter" |
| Label |
Type |
Description |
| root |
TreeFilterPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
| Label |
Type |
Description |
| root |
TreeHeaderPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
| Label |
Type |
Description |
| root |
TreeFooterPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
TreeEmpty
| Label |
Type |
Description |
| root |
TreeEmptyPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
TreeLoading
Tree.Root.loading이 true일 때만 렌더링돼요. Tree.Nodes의 형제 요소로 사용하세요.
| Attribute |
Value |
data-scope |
"tree" |
data-part |
"loading" |
role |
"status" |
| Label |
Type |
Description |
| root |
TreeLoadingPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
접근성 (Accessibility)
스크린 리더 (Screen Reader)
평면 <ul role="tree">에는 보이는 행마다 <li role="treeitem">이 하나씩 들어 있어요. 각 행은 aria-level, aria-posinset, aria-setsize를 가져요. 리프가 아닌 행은 aria-expanded를 설정해요. selectionMode="checkbox"에서는 aria-checked가 true, false, 또는 "mixed"가 돼요.
키보드 지원 (Keyboard Support)
| Key |
Function |
tab / shift + tab |
Enter / leave the tree. |
enter / space |
Toggle selection on the focused node. |
down arrow |
Move focus to the next visible row. |
up arrow |
Move focus to the previous visible row. |
right arrow |
Expand the focused node, or step into its first child. |
left arrow |
Collapse the focused node, or step out to its parent. |
home / end |
Jump to the first / last visible row. |
| Printable characters |
Type-ahead by label (500 ms reset). |
더 알아보기 (Learn more)