react-arborist 3.9.0 → 3.10.0

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/README.md CHANGED
@@ -245,15 +245,17 @@ function App() {
245
245
 
246
246
  ### Dynamic sizing
247
247
 
248
- You can add a ref to it with this package [ZeeCoder/use-resize-observer](https://github.com/ZeeCoder/use-resize-observer)
248
+ You can add a ref to it with this package [Pmndrs/react-use-measure](https://github.com/pmndrs/react-use-measure)
249
249
 
250
- That hook will return the height and width of the parent whenever it changes. You then pass these numbers to the Tree.
250
+ That hook will measure the boundaries (for instance width, height, top, left) of a view you reference. Then you pass the width and the height to the Tree.
251
251
 
252
252
  ```js
253
- const { ref, width, height } = useResizeObserver();
253
+ import useMeasure from "react-use-measure";
254
+
255
+ const [ref, bounds] = useMeasure();
254
256
 
255
257
  <div className="parent" ref={ref}>
256
- <Tree height={height} width={width} />
258
+ <Tree height={bounds.height} width={bounds.width} />
257
259
  </div>
258
260
  ```
259
261
 
@@ -18,7 +18,7 @@ let timeoutId = null;
18
18
  function DefaultContainer() {
19
19
  (0, context_1.useDataUpdates)();
20
20
  const tree = (0, context_1.useTreeApi)();
21
- return ((0, jsx_runtime_1.jsx)("div", { role: "tree", style: {
21
+ return ((0, jsx_runtime_1.jsx)("div", { role: "tree", "aria-label": tree.props["aria-label"], "aria-labelledby": tree.props["aria-labelledby"], "aria-multiselectable": !tree.props.disableMultiSelection || undefined, style: {
22
22
  height: tree.height,
23
23
  width: tree.width,
24
24
  minHeight: 0,
@@ -144,16 +144,16 @@ function DefaultContainer() {
144
144
  }
145
145
  return;
146
146
  }
147
- if (e.key === "a" && e.metaKey && !tree.props.disableMultiSelection) {
147
+ if (e.key === "a" && (e.metaKey || e.ctrlKey) && !tree.props.disableMultiSelection) {
148
148
  e.preventDefault();
149
149
  tree.selectAll();
150
150
  return;
151
151
  }
152
- if (e.key === "a" && !e.metaKey && tree.props.onCreate) {
152
+ if (e.key === "a" && !e.metaKey && !e.ctrlKey && tree.props.onCreate) {
153
153
  tree.createLeaf();
154
154
  return;
155
155
  }
156
- if (e.key === "A" && !e.metaKey) {
156
+ if (e.key === "A" && !e.metaKey && !e.ctrlKey) {
157
157
  if (!tree.props.onCreate)
158
158
  return;
159
159
  tree.createInternal();
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const jsx_runtime_1 = require("react/jsx-runtime");
4
+ const react_1 = require("@testing-library/react");
5
+ const tree_1 = require("./tree");
6
+ const data = [
7
+ {
8
+ id: "1",
9
+ name: "root",
10
+ children: [
11
+ { id: "2", name: "a" },
12
+ { id: "3", name: "b", children: [{ id: "4", name: "c" }] },
13
+ ],
14
+ },
15
+ ];
16
+ /* #303: multi-select should respond to Ctrl+Click (Windows) as well as
17
+ Cmd/Meta+Click (macOS). */
18
+ test("Ctrl+Click adds a row to the selection (#303)", () => {
19
+ (0, react_1.render)((0, jsx_runtime_1.jsx)(tree_1.Tree, { data: data, openByDefault: true }));
20
+ const [, a, b] = react_1.screen.getAllByRole("treeitem");
21
+ react_1.fireEvent.click(a);
22
+ expect(a.getAttribute("aria-selected")).toBe("true");
23
+ react_1.fireEvent.click(b, { ctrlKey: true });
24
+ expect(a.getAttribute("aria-selected")).toBe("true");
25
+ expect(b.getAttribute("aria-selected")).toBe("true");
26
+ });
27
+ test("Ctrl+Click toggles an already-selected row off (#303)", () => {
28
+ (0, react_1.render)((0, jsx_runtime_1.jsx)(tree_1.Tree, { data: data, openByDefault: true }));
29
+ const [, a, b] = react_1.screen.getAllByRole("treeitem");
30
+ react_1.fireEvent.click(a);
31
+ react_1.fireEvent.click(b, { ctrlKey: true });
32
+ react_1.fireEvent.click(b, { ctrlKey: true });
33
+ expect(a.getAttribute("aria-selected")).toBe("true");
34
+ expect(b.getAttribute("aria-selected")).toBe("false");
35
+ });
36
+ test("Ctrl+Click falls through to a plain select when multi-select is disabled (#303)", () => {
37
+ (0, react_1.render)((0, jsx_runtime_1.jsx)(tree_1.Tree, { data: data, openByDefault: true, disableMultiSelection: true }));
38
+ const [, a, b] = react_1.screen.getAllByRole("treeitem");
39
+ react_1.fireEvent.click(a);
40
+ react_1.fireEvent.click(b, { ctrlKey: true });
41
+ expect(a.getAttribute("aria-selected")).toBe("false");
42
+ expect(b.getAttribute("aria-selected")).toBe("true");
43
+ });
44
+ /* #325: forward an accessible name and multiselectable state onto the
45
+ role="tree" element. */
46
+ test("forwards aria-label to the role=tree element (#325)", () => {
47
+ (0, react_1.render)((0, jsx_runtime_1.jsx)(tree_1.Tree, { data: data, "aria-label": "File explorer" }));
48
+ expect(react_1.screen.getByRole("tree").getAttribute("aria-label")).toBe("File explorer");
49
+ });
50
+ test("forwards aria-labelledby to the role=tree element (#325)", () => {
51
+ (0, react_1.render)((0, jsx_runtime_1.jsx)(tree_1.Tree, { data: data, "aria-labelledby": "heading-id" }));
52
+ expect(react_1.screen.getByRole("tree").getAttribute("aria-labelledby")).toBe("heading-id");
53
+ });
54
+ test("marks the tree aria-multiselectable by default (#325)", () => {
55
+ (0, react_1.render)((0, jsx_runtime_1.jsx)(tree_1.Tree, { data: data }));
56
+ expect(react_1.screen.getByRole("tree").getAttribute("aria-multiselectable")).toBe("true");
57
+ });
58
+ test("omits aria-multiselectable when multi-select is disabled (#325)", () => {
59
+ (0, react_1.render)((0, jsx_runtime_1.jsx)(tree_1.Tree, { data: data, disableMultiSelection: true }));
60
+ expect(react_1.screen.getByRole("tree").hasAttribute("aria-multiselectable")).toBe(false);
61
+ });
@@ -5,7 +5,7 @@ const create_root_1 = require("../data/create-root");
5
5
  class NodeApi {
6
6
  constructor(params) {
7
7
  this.handleClick = (e) => {
8
- if (e.metaKey && !this.tree.props.disableMultiSelection) {
8
+ if ((e.metaKey || e.ctrlKey) && !this.tree.props.disableMultiSelection) {
9
9
  if (this.isSelected)
10
10
  this.deselect();
11
11
  else
@@ -50,6 +50,8 @@ export interface TreeProps<T> {
50
50
  initialOpenState?: OpenMap;
51
51
  searchTerm?: string;
52
52
  searchMatch?: (node: NodeApi<T>, searchTerm: string) => boolean;
53
+ "aria-label"?: string;
54
+ "aria-labelledby"?: string;
53
55
  className?: string | undefined;
54
56
  rowClassName?: string | undefined;
55
57
  dndRootElement?: globalThis.Node | null;
@@ -15,7 +15,7 @@ let timeoutId = null;
15
15
  export function DefaultContainer() {
16
16
  useDataUpdates();
17
17
  const tree = useTreeApi();
18
- return (_jsx("div", { role: "tree", style: {
18
+ return (_jsx("div", { role: "tree", "aria-label": tree.props["aria-label"], "aria-labelledby": tree.props["aria-labelledby"], "aria-multiselectable": !tree.props.disableMultiSelection || undefined, style: {
19
19
  height: tree.height,
20
20
  width: tree.width,
21
21
  minHeight: 0,
@@ -141,16 +141,16 @@ export function DefaultContainer() {
141
141
  }
142
142
  return;
143
143
  }
144
- if (e.key === "a" && e.metaKey && !tree.props.disableMultiSelection) {
144
+ if (e.key === "a" && (e.metaKey || e.ctrlKey) && !tree.props.disableMultiSelection) {
145
145
  e.preventDefault();
146
146
  tree.selectAll();
147
147
  return;
148
148
  }
149
- if (e.key === "a" && !e.metaKey && tree.props.onCreate) {
149
+ if (e.key === "a" && !e.metaKey && !e.ctrlKey && tree.props.onCreate) {
150
150
  tree.createLeaf();
151
151
  return;
152
152
  }
153
- if (e.key === "A" && !e.metaKey) {
153
+ if (e.key === "A" && !e.metaKey && !e.ctrlKey) {
154
154
  if (!tree.props.onCreate)
155
155
  return;
156
156
  tree.createInternal();
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,59 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { fireEvent, render, screen } from "@testing-library/react";
3
+ import { Tree } from "./tree";
4
+ const data = [
5
+ {
6
+ id: "1",
7
+ name: "root",
8
+ children: [
9
+ { id: "2", name: "a" },
10
+ { id: "3", name: "b", children: [{ id: "4", name: "c" }] },
11
+ ],
12
+ },
13
+ ];
14
+ /* #303: multi-select should respond to Ctrl+Click (Windows) as well as
15
+ Cmd/Meta+Click (macOS). */
16
+ test("Ctrl+Click adds a row to the selection (#303)", () => {
17
+ render(_jsx(Tree, { data: data, openByDefault: true }));
18
+ const [, a, b] = screen.getAllByRole("treeitem");
19
+ fireEvent.click(a);
20
+ expect(a.getAttribute("aria-selected")).toBe("true");
21
+ fireEvent.click(b, { ctrlKey: true });
22
+ expect(a.getAttribute("aria-selected")).toBe("true");
23
+ expect(b.getAttribute("aria-selected")).toBe("true");
24
+ });
25
+ test("Ctrl+Click toggles an already-selected row off (#303)", () => {
26
+ render(_jsx(Tree, { data: data, openByDefault: true }));
27
+ const [, a, b] = screen.getAllByRole("treeitem");
28
+ fireEvent.click(a);
29
+ fireEvent.click(b, { ctrlKey: true });
30
+ fireEvent.click(b, { ctrlKey: true });
31
+ expect(a.getAttribute("aria-selected")).toBe("true");
32
+ expect(b.getAttribute("aria-selected")).toBe("false");
33
+ });
34
+ test("Ctrl+Click falls through to a plain select when multi-select is disabled (#303)", () => {
35
+ render(_jsx(Tree, { data: data, openByDefault: true, disableMultiSelection: true }));
36
+ const [, a, b] = screen.getAllByRole("treeitem");
37
+ fireEvent.click(a);
38
+ fireEvent.click(b, { ctrlKey: true });
39
+ expect(a.getAttribute("aria-selected")).toBe("false");
40
+ expect(b.getAttribute("aria-selected")).toBe("true");
41
+ });
42
+ /* #325: forward an accessible name and multiselectable state onto the
43
+ role="tree" element. */
44
+ test("forwards aria-label to the role=tree element (#325)", () => {
45
+ render(_jsx(Tree, { data: data, "aria-label": "File explorer" }));
46
+ expect(screen.getByRole("tree").getAttribute("aria-label")).toBe("File explorer");
47
+ });
48
+ test("forwards aria-labelledby to the role=tree element (#325)", () => {
49
+ render(_jsx(Tree, { data: data, "aria-labelledby": "heading-id" }));
50
+ expect(screen.getByRole("tree").getAttribute("aria-labelledby")).toBe("heading-id");
51
+ });
52
+ test("marks the tree aria-multiselectable by default (#325)", () => {
53
+ render(_jsx(Tree, { data: data }));
54
+ expect(screen.getByRole("tree").getAttribute("aria-multiselectable")).toBe("true");
55
+ });
56
+ test("omits aria-multiselectable when multi-select is disabled (#325)", () => {
57
+ render(_jsx(Tree, { data: data, disableMultiSelection: true }));
58
+ expect(screen.getByRole("tree").hasAttribute("aria-multiselectable")).toBe(false);
59
+ });
@@ -2,7 +2,7 @@ import { ROOT_ID } from "../data/create-root";
2
2
  export class NodeApi {
3
3
  constructor(params) {
4
4
  this.handleClick = (e) => {
5
- if (e.metaKey && !this.tree.props.disableMultiSelection) {
5
+ if ((e.metaKey || e.ctrlKey) && !this.tree.props.disableMultiSelection) {
6
6
  if (this.isSelected)
7
7
  this.deselect();
8
8
  else
@@ -50,6 +50,8 @@ export interface TreeProps<T> {
50
50
  initialOpenState?: OpenMap;
51
51
  searchTerm?: string;
52
52
  searchMatch?: (node: NodeApi<T>, searchTerm: string) => boolean;
53
+ "aria-label"?: string;
54
+ "aria-labelledby"?: string;
53
55
  className?: string | undefined;
54
56
  rowClassName?: string | undefined;
55
57
  dndRootElement?: globalThis.Node | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-arborist",
3
- "version": "3.9.0",
3
+ "version": "3.10.0",
4
4
  "keywords": [
5
5
  "arborist",
6
6
  "dnd",
@@ -0,0 +1,74 @@
1
+ import { fireEvent, render, screen } from "@testing-library/react";
2
+ import { Tree } from "./tree";
3
+
4
+ type Datum = { id: string; name: string; children?: Datum[] };
5
+
6
+ const data: Datum[] = [
7
+ {
8
+ id: "1",
9
+ name: "root",
10
+ children: [
11
+ { id: "2", name: "a" },
12
+ { id: "3", name: "b", children: [{ id: "4", name: "c" }] },
13
+ ],
14
+ },
15
+ ];
16
+
17
+ /* #303: multi-select should respond to Ctrl+Click (Windows) as well as
18
+ Cmd/Meta+Click (macOS). */
19
+ test("Ctrl+Click adds a row to the selection (#303)", () => {
20
+ render(<Tree<Datum> data={data} openByDefault />);
21
+ const [, a, b] = screen.getAllByRole("treeitem");
22
+
23
+ fireEvent.click(a);
24
+ expect(a.getAttribute("aria-selected")).toBe("true");
25
+
26
+ fireEvent.click(b, { ctrlKey: true });
27
+ expect(a.getAttribute("aria-selected")).toBe("true");
28
+ expect(b.getAttribute("aria-selected")).toBe("true");
29
+ });
30
+
31
+ test("Ctrl+Click toggles an already-selected row off (#303)", () => {
32
+ render(<Tree<Datum> data={data} openByDefault />);
33
+ const [, a, b] = screen.getAllByRole("treeitem");
34
+
35
+ fireEvent.click(a);
36
+ fireEvent.click(b, { ctrlKey: true });
37
+ fireEvent.click(b, { ctrlKey: true });
38
+
39
+ expect(a.getAttribute("aria-selected")).toBe("true");
40
+ expect(b.getAttribute("aria-selected")).toBe("false");
41
+ });
42
+
43
+ test("Ctrl+Click falls through to a plain select when multi-select is disabled (#303)", () => {
44
+ render(<Tree<Datum> data={data} openByDefault disableMultiSelection />);
45
+ const [, a, b] = screen.getAllByRole("treeitem");
46
+
47
+ fireEvent.click(a);
48
+ fireEvent.click(b, { ctrlKey: true });
49
+
50
+ expect(a.getAttribute("aria-selected")).toBe("false");
51
+ expect(b.getAttribute("aria-selected")).toBe("true");
52
+ });
53
+
54
+ /* #325: forward an accessible name and multiselectable state onto the
55
+ role="tree" element. */
56
+ test("forwards aria-label to the role=tree element (#325)", () => {
57
+ render(<Tree<Datum> data={data} aria-label="File explorer" />);
58
+ expect(screen.getByRole("tree").getAttribute("aria-label")).toBe("File explorer");
59
+ });
60
+
61
+ test("forwards aria-labelledby to the role=tree element (#325)", () => {
62
+ render(<Tree<Datum> data={data} aria-labelledby="heading-id" />);
63
+ expect(screen.getByRole("tree").getAttribute("aria-labelledby")).toBe("heading-id");
64
+ });
65
+
66
+ test("marks the tree aria-multiselectable by default (#325)", () => {
67
+ render(<Tree<Datum> data={data} />);
68
+ expect(screen.getByRole("tree").getAttribute("aria-multiselectable")).toBe("true");
69
+ });
70
+
71
+ test("omits aria-multiselectable when multi-select is disabled (#325)", () => {
72
+ render(<Tree<Datum> data={data} disableMultiSelection />);
73
+ expect(screen.getByRole("tree").hasAttribute("aria-multiselectable")).toBe(false);
74
+ });
@@ -19,6 +19,9 @@ export function DefaultContainer() {
19
19
  return (
20
20
  <div
21
21
  role="tree"
22
+ aria-label={tree.props["aria-label"]}
23
+ aria-labelledby={tree.props["aria-labelledby"]}
24
+ aria-multiselectable={!tree.props.disableMultiSelection || undefined}
22
25
  style={{
23
26
  height: tree.height,
24
27
  width: tree.width,
@@ -133,16 +136,16 @@ export function DefaultContainer() {
133
136
  }
134
137
  return;
135
138
  }
136
- if (e.key === "a" && e.metaKey && !tree.props.disableMultiSelection) {
139
+ if (e.key === "a" && (e.metaKey || e.ctrlKey) && !tree.props.disableMultiSelection) {
137
140
  e.preventDefault();
138
141
  tree.selectAll();
139
142
  return;
140
143
  }
141
- if (e.key === "a" && !e.metaKey && tree.props.onCreate) {
144
+ if (e.key === "a" && !e.metaKey && !e.ctrlKey && tree.props.onCreate) {
142
145
  tree.createLeaf();
143
146
  return;
144
147
  }
145
- if (e.key === "A" && !e.metaKey) {
148
+ if (e.key === "A" && !e.metaKey && !e.ctrlKey) {
146
149
  if (!tree.props.onCreate) return;
147
150
  tree.createInternal();
148
151
  return;
@@ -200,7 +200,7 @@ export class NodeApi<T = any> {
200
200
  }
201
201
 
202
202
  handleClick = (e: React.MouseEvent) => {
203
- if (e.metaKey && !this.tree.props.disableMultiSelection) {
203
+ if ((e.metaKey || e.ctrlKey) && !this.tree.props.disableMultiSelection) {
204
204
  if (this.isSelected) this.deselect();
205
205
  else this.selectMulti();
206
206
  } else if (e.shiftKey && !this.tree.props.disableMultiSelection) {
@@ -69,6 +69,10 @@ export interface TreeProps<T> {
69
69
  searchTerm?: string;
70
70
  searchMatch?: (node: NodeApi<T>, searchTerm: string) => boolean;
71
71
 
72
+ /* Accessibility */
73
+ "aria-label"?: string;
74
+ "aria-labelledby"?: string;
75
+
72
76
  /* Extra */
73
77
  className?: string | undefined;
74
78
  rowClassName?: string | undefined;