TreeSelect
Seleção de árvore dropdown para dados hierárquicos.
Importação
Seção intitulada “Importação”import { TreeSelect } from 'asterui'import type { TreeDataNode } from 'asterui'Exemplos
Seção intitulada “Exemplos”TreeSelect Básico
Selecione um único valor de uma estrutura de árvore.
Select an item
import { TreeSelect } from 'asterui'
import type { TreeDataNode } from 'asterui'
import { useState } from 'react'
function App() {
const basicTreeData: TreeDataNode[] = [
{
key: 'parent',
title: 'Parent Node',
children: [
{
key: 'child1',
title: 'Child Node 1',
children: [
{ key: 'leaf1', title: 'Leaf 1' },
{ key: 'leaf2', title: 'Leaf 2' },
],
},
{ key: 'child2', title: 'Child Node 2' },
],
},
]
const [value, setValue] = useState<string | undefined>()
return (
<TreeSelect
treeData={basicTreeData}
value={value}
onChange={(val) => setValue(val as string)}
placeholder="Select an item"
/>
)
}
export default App Seleção Múltipla
Selecione múltiplos valores da árvore.
Select items
import { TreeSelect } from 'asterui'
import type { TreeDataNode } from 'asterui'
import { useState } from 'react'
function App() {
const categoriesData: TreeDataNode[] = [
{
key: 'electronics',
title: 'Electronics',
children: [
{
key: 'phones',
title: 'Phones',
children: [
{ key: 'iphone', title: 'iPhone' },
{ key: 'samsung', title: 'Samsung' },
{ key: 'pixel', title: 'Pixel' },
],
},
{
key: 'laptops',
title: 'Laptops',
children: [
{ key: 'macbook', title: 'MacBook' },
{ key: 'thinkpad', title: 'ThinkPad' },
],
},
],
},
{
key: 'clothing',
title: 'Clothing',
children: [
{ key: 'shirts', title: 'Shirts' },
{ key: 'pants', title: 'Pants' },
{ key: 'shoes', title: 'Shoes' },
],
},
]
const [value, setValue] = useState<string[]>([])
return (
<TreeSelect
treeData={categoriesData}
value={value}
onChange={(val) => setValue(val as string[])}
placeholder="Select items"
multiple
/>
)
}
export default App Com Checkbox
Use checkboxes para seleção com associação pai-filho.
Check items
import { TreeSelect } from 'asterui'
import type { TreeDataNode } from 'asterui'
import { useState } from 'react'
function App() {
const categoriesData: TreeDataNode[] = [
{
key: 'electronics',
title: 'Electronics',
children: [
{
key: 'phones',
title: 'Phones',
children: [
{ key: 'iphone', title: 'iPhone' },
{ key: 'samsung', title: 'Samsung' },
{ key: 'pixel', title: 'Pixel' },
],
},
{
key: 'laptops',
title: 'Laptops',
children: [
{ key: 'macbook', title: 'MacBook' },
{ key: 'thinkpad', title: 'ThinkPad' },
],
},
],
},
{
key: 'clothing',
title: 'Clothing',
children: [
{ key: 'shirts', title: 'Shirts' },
{ key: 'pants', title: 'Pants' },
{ key: 'shoes', title: 'Shoes' },
],
},
]
const [value, setValue] = useState<string[]>([])
return (
<TreeSelect
treeData={categoriesData}
value={value}
onChange={(val) => setValue(val as string[])}
placeholder="Check items"
treeCheckable
/>
)
}
export default App Buscável
Filtre nós da árvore com busca.
Search and select
import { TreeSelect } from 'asterui'
import type { TreeDataNode } from 'asterui'
import { useState } from 'react'
function App() {
const categoriesData: TreeDataNode[] = [
{
key: 'electronics',
title: 'Electronics',
children: [
{
key: 'phones',
title: 'Phones',
children: [
{ key: 'iphone', title: 'iPhone' },
{ key: 'samsung', title: 'Samsung' },
{ key: 'pixel', title: 'Pixel' },
],
},
{
key: 'laptops',
title: 'Laptops',
children: [
{ key: 'macbook', title: 'MacBook' },
{ key: 'thinkpad', title: 'ThinkPad' },
],
},
],
},
{
key: 'clothing',
title: 'Clothing',
children: [
{ key: 'shirts', title: 'Shirts' },
{ key: 'pants', title: 'Pants' },
{ key: 'shoes', title: 'Shoes' },
],
},
]
const [value, setValue] = useState<string | undefined>()
return (
<TreeSelect
treeData={categoriesData}
value={value}
onChange={(val) => setValue(val as string)}
placeholder="Search and select"
showSearch
/>
)
}
export default App Tamanhos
TreeSelect vem em vários tamanhos.
Extra small
Small
Medium
Large
Extra large
import { TreeSelect } from 'asterui'
import type { TreeDataNode } from 'asterui'
function App() {
const simpleData: TreeDataNode[] = [
{ key: 'opt1', title: 'Option 1' },
{ key: 'opt2', title: 'Option 2' },
{ key: 'opt3', title: 'Option 3' },
]
return (
<div className="flex flex-col gap-2">
<TreeSelect treeData={simpleData} size="xs" placeholder="Extra small" />
<TreeSelect treeData={simpleData} size="sm" placeholder="Small" />
<TreeSelect treeData={simpleData} size="md" placeholder="Medium" />
<TreeSelect treeData={simpleData} size="lg" placeholder="Large" />
<TreeSelect treeData={simpleData} size="xl" placeholder="Extra large" />
</div>
)
}
export default App Estado de Validação
Mostre estados de erro ou aviso.
Error state
Warning state
import { TreeSelect } from 'asterui'
import type { TreeDataNode } from 'asterui'
function App() {
const simpleData: TreeDataNode[] = [
{ key: 'opt1', title: 'Option 1' },
{ key: 'opt2', title: 'Option 2' },
{ key: 'opt3', title: 'Option 3' },
]
return (
<div className="flex flex-col gap-2">
<TreeSelect treeData={simpleData} status="error" placeholder="Error state" />
<TreeSelect treeData={simpleData} status="warning" placeholder="Warning state" />
</div>
)
}
export default App Carregamento Assíncrono
Carregue nós da árvore assincronamente ao expandir.
Expand to load
import { TreeSelect } from 'asterui'
import type { TreeDataNode } from 'asterui'
import { useState } from 'react'
function App() {
const [treeData, setTreeData] = useState<TreeDataNode[]>([
{ key: 'region1', title: 'Region 1' },
{ key: 'region2', title: 'Region 2' },
])
const loadData = async (node: TreeDataNode) => {
await new Promise((resolve) => setTimeout(resolve, 1000))
setTreeData((prev) => {
const updateNode = (nodes: TreeDataNode[]): TreeDataNode[] =>
nodes.map((n) =>
n.key === node.key
? {
...n,
children: [
{ key: `${n.key}-1`, title: 'Child 1', isLeaf: true },
{ key: `${n.key}-2`, title: 'Child 2', isLeaf: true },
],
}
: { ...n, children: n.children ? updateNode(n.children) : undefined }
)
return updateNode(prev)
})
}
return (
<TreeSelect
treeData={treeData}
loadData={loadData}
placeholder="Expand to load"
/>
)
}
export default App Limite de Tags
Limite o número de tags visíveis.
Select items
import { TreeSelect } from 'asterui'
import type { TreeDataNode } from 'asterui'
import { useState } from 'react'
function App() {
const categoriesData: TreeDataNode[] = [
{
key: 'electronics',
title: 'Electronics',
children: [
{
key: 'phones',
title: 'Phones',
children: [
{ key: 'iphone', title: 'iPhone' },
{ key: 'samsung', title: 'Samsung' },
{ key: 'pixel', title: 'Pixel' },
],
},
{
key: 'laptops',
title: 'Laptops',
children: [
{ key: 'macbook', title: 'MacBook' },
{ key: 'thinkpad', title: 'ThinkPad' },
],
},
],
},
{
key: 'clothing',
title: 'Clothing',
children: [
{ key: 'shirts', title: 'Shirts' },
{ key: 'pants', title: 'Pants' },
{ key: 'shoes', title: 'Shoes' },
],
},
]
const [value, setValue] = useState<string[]>([])
return (
<TreeSelect
treeData={categoriesData}
value={value}
onChange={(val) => setValue(val as string[])}
placeholder="Select items"
treeCheckable
maxTagCount={2}
maxTagPlaceholder={(omitted) => `+${omitted.length} more...`}
/>
)
}
export default App Linha da Árvore
Mostre linhas conectando nós da árvore.
With tree lines
import { TreeSelect } from 'asterui'
import type { TreeDataNode } from 'asterui'
function App() {
const basicTreeData: TreeDataNode[] = [
{
key: 'parent',
title: 'Parent Node',
children: [
{
key: 'child1',
title: 'Child Node 1',
children: [
{ key: 'leaf1', title: 'Leaf 1' },
{ key: 'leaf2', title: 'Leaf 2' },
],
},
{ key: 'child2', title: 'Child Node 2' },
],
},
]
return (
<TreeSelect
treeData={basicTreeData}
placeholder="With tree lines"
treeLine
treeDefaultExpandAll
/>
)
}
export default App Itens Desabilitados
Alguns nós da árvore podem ser desabilitados.
Select an item
import { TreeSelect } from 'asterui'
import type { TreeDataNode } from 'asterui'
import { useState } from 'react'
function App() {
const [value, setValue] = useState<string | undefined>()
const treeDataWithDisabled: TreeDataNode[] = [
{
key: 'parent',
title: 'Available Parent',
children: [
{ key: 'child1', title: 'Available Child' },
{ key: 'child2', title: 'Disabled Child', disabled: true },
{ key: 'child3', title: 'Another Available' },
],
},
]
return (
<TreeSelect
treeData={treeDataWithDisabled}
value={value}
onChange={(val) => setValue(val as string)}
placeholder="Select an item"
/>
)
}
export default App TreeSelect
Seção intitulada “TreeSelect”| Propriedade | Descrição | Tipo | Padrão |
|---|---|---|---|
treeData | Estrutura de dados da árvore | TreeDataNode[] | [] |
value | Valor(es) selecionado(s) | string | string[] | - |
defaultValue | Valor(es) selecionado(s) padrão | string | string[] | [] |
onChange | Callback quando a seleção muda | (value: string | string[], labels: ReactNode[]) => void | - |
multiple | Permitir seleção múltipla | boolean | false |
treeCheckable | Mostrar checkbox para nós da árvore | boolean | false |
treeCheckStrictly | Marcar sem associação pai-filho | boolean | false |
showCheckedStrategy | Como exibir itens marcados | 'SHOW_ALL' | 'SHOW_PARENT' | 'SHOW_CHILD' | 'SHOW_ALL' |
showSearch | Habilitar funcionalidade de busca | boolean | false |
placeholder | Texto de placeholder | string | 'Please select' |
allowClear | Mostrar botão de limpar | boolean | true |
disabled | Desabilitar o select | boolean | false |
size | Tamanho do input | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'md' |
status | Estado de validação | 'error' | 'warning' | - |
maxTagCount | Número máximo de tags a mostrar | number | 'responsive' | - |
treeLine | Mostrar linhas conectando | boolean | false |
treeDefaultExpandAll | Expandir todos os nós da árvore por padrão | boolean | false |
loadData | Carregar dados assincronamente | (node: TreeDataNode) => Promise<void> | - |
data-testid | ID de teste para o componente | string | 'treeselect' |
className | Classes CSS adicionais | string | - |
Acessibilidade
Seção intitulada “Acessibilidade”O componente TreeSelect segue os padrões de combobox e árvore WAI-ARIA com atributos ARIA adequados e navegação por teclado.
Navegação por Teclado
Seção intitulada “Navegação por Teclado”| Tecla | Ação |
|---|---|
| Enter / Space | Abre dropdown ou seleciona item focado |
| Escape | Fecha dropdown |
| ↓ | Abre dropdown ou move foco para baixo |
| ↑ | Move foco para cima |
| → | Expande nó da árvore focado |
| ← | Recolhe nó da árvore focado ou move para pai |
| Home | Move foco para primeiro item |
| End | Move foco para último item |