TreeSelect.tsx
3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { useState } from 'react';
import type { ButtonHTMLAttributes, ReactNode } from 'react';
import styles from './Shared.module.css';
import { Icon } from './Icon';
import { cx, notifyPopupOpen, useExclusivePopup } from './utils';
const selectPopupOpenEvent = 'gl-select-popup-open';
export interface TreeSelectNode {
label: ReactNode;
value: string;
disabled?: boolean;
children?: TreeSelectNode[];
}
export interface TreeSelectProps extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'onChange' | 'value'> {
/** 树节点保留 children 结构,供组织、分类、权限范围等层级选择使用。 */
treeData: TreeSelectNode[];
value?: string;
placeholder?: string;
onChange?: (value: string, node: TreeSelectNode) => void;
}
function findNode(nodes: TreeSelectNode[], value?: string): TreeSelectNode | undefined {
for (const node of nodes) {
if (node.value === value) return node;
const child = findNode(node.children ?? [], value);
if (child) return child;
}
return undefined;
}
export function TreeSelect({ treeData, value, placeholder = '请选择', onChange, ...props }: TreeSelectProps) {
const [open, setOpen] = useState(false);
const popupId = useExclusivePopup(selectPopupOpenEvent, setOpen);
const [expanded, setExpanded] = useState<Set<string>>(new Set());
const [innerValue, setInnerValue] = useState<string | undefined>(value);
const currentValue = value ?? innerValue;
const currentNode = findNode(treeData, currentValue);
const renderNodes = (nodes: TreeSelectNode[], level = 0): ReactNode[] =>
nodes.flatMap((node) => {
const hasChildren = Boolean(node.children?.length);
const isExpanded = expanded.has(node.value);
const item = (
<div
key={node.value}
className={cx(styles.treeItem, node.value === currentValue && styles.optionSelected, node.disabled && styles.disabled)}
role="treeitem"
aria-expanded={hasChildren ? isExpanded : undefined}
aria-disabled={node.disabled || undefined}
aria-selected={!hasChildren && node.value === currentValue}
style={{ paddingLeft: 8 + level * 16 }}
onClick={() => {
if (node.disabled) return;
if (hasChildren) {
const nextExpanded = new Set(expanded);
if (isExpanded) {
nextExpanded.delete(node.value);
} else {
nextExpanded.add(node.value);
}
setExpanded(nextExpanded);
return;
}
if (value === undefined) setInnerValue(node.value);
onChange?.(node.value, node);
setOpen(false);
}}
>
{hasChildren ? <Icon name={isExpanded ? 'chevron-down' : 'chevron-right'} /> : <span className={styles.treeSpacer} />}
{node.label}
</div>
);
return isExpanded && node.children ? [item, ...renderNodes(node.children, level + 1)] : [item];
});
const toggleOpen = () => {
setOpen((nextOpen) => {
if (!nextOpen) notifyPopupOpen(selectPopupOpenEvent, popupId);
return !nextOpen;
});
};
return (
<span className={styles.selectWrapper}>
<button {...props} className={cx(styles.control, styles.fieldButton)} role="combobox" aria-expanded={open} type="button" onClick={toggleOpen}>
<span>{currentNode?.label ?? placeholder}</span>
<Icon className={styles.fieldIcon} name={open ? 'chevron-down' : 'chevron-right'} />
</button>
{open ? (
<div className={cx(styles.popup, styles.selectPopup)} role="tree">
{renderNodes(treeData)}
</div>
) : null}
</span>
);
}