@oaiz/app-kit 0.1.4250 → 0.1.4254

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oaiz/app-kit",
3
- "version": "0.1.4250",
3
+ "version": "0.1.4254",
4
4
  "description": "Source components, hooks and theme for apps built on OAIZ, to copy and change",
5
5
  "keywords": [
6
6
  "oaiz",
@@ -24,6 +24,11 @@ export interface TreeViewProps {
24
24
  onSelect?: (node: TreeNode) => void;
25
25
  /** The ids of nodes open at first. */
26
26
  defaultExpanded?: string[];
27
+ /** The ids of open nodes, when the caller owns them, such as to reveal a node. */
28
+ expanded?: string[];
29
+ onExpandedChange?: (expanded: string[]) => void;
30
+ /** Nodes with children open and close instead of being chosen, as in a file tree. */
31
+ selectLeavesOnly?: boolean;
27
32
  className?: string;
28
33
  }
29
34
 
@@ -63,11 +68,17 @@ function TreeView({
63
68
  selected = null,
64
69
  onSelect,
65
70
  defaultExpanded = [],
71
+ expanded: controlledExpanded,
72
+ onExpandedChange,
73
+ selectLeavesOnly = false,
66
74
  className,
67
75
  }: TreeViewProps) {
68
- const [expanded, setExpanded] = React.useState(
76
+ const [ownExpanded, setOwnExpanded] = React.useState(
69
77
  () => new Set(defaultExpanded),
70
78
  );
79
+ const expanded = controlledExpanded
80
+ ? new Set(controlledExpanded)
81
+ : ownExpanded;
71
82
  const [focused, setFocused] = React.useState<string | null>(null);
72
83
  const refs = React.useRef(new Map<string, HTMLDivElement>());
73
84
  const rows = visibleRows(nodes, expanded);
@@ -78,13 +89,20 @@ function TreeView({
78
89
  setFocused(id);
79
90
  refs.current.get(id)?.focus();
80
91
  };
81
- const toggle = (id: string, open: boolean) =>
82
- setExpanded((now) => {
83
- const next = new Set(now);
84
- if (open) next.add(id);
85
- else next.delete(id);
86
- return next;
87
- });
92
+ const toggle = (id: string, open: boolean) => {
93
+ const next = new Set(expanded);
94
+ if (open) next.add(id);
95
+ else next.delete(id);
96
+ if (!controlledExpanded) setOwnExpanded(next);
97
+ onExpandedChange?.([...next]);
98
+ };
99
+
100
+ const choosable = (node: TreeNode) =>
101
+ onSelect !== undefined && !(selectLeavesOnly && node.children?.length);
102
+ const activate = (row: Row) => {
103
+ if (choosable(row.node)) onSelect?.(row.node);
104
+ else if (row.node.children?.length) toggle(row.node.id, !row.open);
105
+ };
88
106
 
89
107
  function onKeyDown(event: React.KeyboardEvent, row: Row, index: number) {
90
108
  const hasChildren = Boolean(row.node.children?.length);
@@ -111,8 +129,7 @@ function TreeView({
111
129
  break;
112
130
  case 'Enter':
113
131
  case ' ':
114
- onSelect?.(row.node);
115
- if (hasChildren && !onSelect) toggle(row.node.id, !row.open);
132
+ activate(row);
116
133
  break;
117
134
  default:
118
135
  if (event.key.length === 1 && /\S/.test(event.key)) {
@@ -149,15 +166,14 @@ function TreeView({
149
166
  role="treeitem"
150
167
  aria-level={row.level}
151
168
  aria-expanded={hasChildren ? row.open : undefined}
152
- aria-selected={onSelect ? isSelected : undefined}
169
+ aria-selected={choosable(row.node) ? isSelected : undefined}
153
170
  aria-label={textOf(row.node)}
154
171
  tabIndex={row.node.id === current ? 0 : -1}
155
172
  onFocus={() => setFocused(row.node.id)}
156
173
  onKeyDown={(event) => onKeyDown(event, row, index)}
157
174
  onClick={() => {
158
175
  setFocused(row.node.id);
159
- if (onSelect) onSelect(row.node);
160
- else if (hasChildren) toggle(row.node.id, !row.open);
176
+ activate(row);
161
177
  }}
162
178
  // Each level indents by one stack step from the inline gutter.
163
179
  style={{