Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API to prevent nodes from being selectable #169

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Testing Locally

1. Clone the repo
2. From the root, run yarn && yarn start
3. Visit localhost:3000
2. From the root, run `yarn && yarn start`
3. Visit <localhost:3000>

# Running Tests

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ interface TreeProps<T> {
openByDefault?: boolean;
selectionFollowsFocus?: boolean;
disableMultiSelection?: boolean;
disableSelect?: string | boolean | BoolFunc<T>;
disableEdit?: string | boolean | BoolFunc<T>;
disableDrag?: string | boolean | BoolFunc<T>;
disableDrop?:
Expand Down
17 changes: 17 additions & 0 deletions packages/e2e/cypress/e2e/gmail-spec.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,23 @@ describe("Testing the Gmail Demo", () => {
cy.get("@item").contains("Categories").click(); // collapses
cy.get("@item").should("have.length", 1);
});

it("can select inbox but not categories", () => {
cy.get("@item").contains("Inbox").click();
cy.focused().should("have.attr", "aria-selected", "true");
cy.get("@item").contains("Categories").click();
cy.focused().should("have.attr", "aria-selected", "false");
});

it("select all does not select categories or spam", () => {
cy.get("@item").contains("Inbox").click();
cy.focused().type("{meta}a");
cy.get("[aria-selected='true']")
.should("not.contain.text", "Categories")
.should("not.contain.text", "Spam")
.should("contain.text", "Inbox")
.should("have.length", TOTAL_ITEMS - 2);
});
});

function dragAndDrop(src: any, dst: any) {
Expand Down
4 changes: 4 additions & 0 deletions packages/react-arborist/src/interfaces/node-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export class NodeApi<T = any> {
return this.tree.isEditable(this.data);
}

get isSelectable() {
return this.tree.isSelectable(this.data);
}

get isEditing() {
return this.tree.editingId === this.id;
}
Expand Down
68 changes: 45 additions & 23 deletions packages/react-arborist/src/interfaces/tree-api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EditResult } from "../types/handlers";
import { Identity, IdObj } from "../types/utils";
import { BoolFunc, Identity, IdObj } from "../types/utils";
import { TreeProps } from "../types/tree-props";
import { MutableRefObject } from "react";
import { Align, FixedSizeList, ListOnItemsRenderedProps } from "react-window";
Expand Down Expand Up @@ -169,7 +169,7 @@ export class TreeApi<T> {
return this.visibleNodes.slice(start, end + 1);
}

indexOf(id: string | null | IdObj) {
indexOf(id: Identity) {
const key = utils.identifyNull(id);
if (!key) return null;
return this.idToIndex[key];
Expand Down Expand Up @@ -219,7 +219,7 @@ export class TreeApi<T> {
}
}

async delete(node: string | IdObj | null | string[] | IdObj[]) {
async delete(node: Identity | string[] | IdObj[]) {
if (!node) return;
const idents = Array.isArray(node) ? node : [node];
const ids = idents.map(identify);
Expand Down Expand Up @@ -256,7 +256,7 @@ export class TreeApi<T> {
setTimeout(() => this.onFocus()); // Return focus to element;
}

activate(id: string | IdObj | null) {
activate(id: Identity) {
const node = this.get(identifyNull(id));
if (!node) return;
safeRun(this.props.onActivate, node);
Expand Down Expand Up @@ -328,9 +328,13 @@ export class TreeApi<T> {
const changeFocus = opts.focus !== false;
const id = identify(node);
if (changeFocus) this.dispatch(focus(id));
this.dispatch(selection.only(id));
this.dispatch(selection.anchor(id));
this.dispatch(selection.mostRecent(id));
if (this.get(id)?.isSelectable) {
this.setSelection({
ids: [id],
anchor: id,
mostRecent: id,
});
}
this.scrollTo(id, opts.align);
if (this.focusedNode && changeFocus) {
safeRun(this.props.onFocus, this.focusedNode);
Expand All @@ -348,9 +352,11 @@ export class TreeApi<T> {
const node = this.get(identifyNull(identity));
if (!node) return;
this.dispatch(focus(node.id));
this.dispatch(selection.add(node.id));
this.dispatch(selection.anchor(node.id));
this.dispatch(selection.mostRecent(node.id));
if (node.isSelectable) {
this.dispatch(selection.add(node.id));
this.dispatch(selection.anchor(node.id));
this.dispatch(selection.mostRecent(node.id));
}
this.scrollTo(node);
if (this.focusedNode) safeRun(this.props.onFocus, this.focusedNode);
safeRun(this.props.onSelect, this.selectedNodes);
Expand All @@ -359,11 +365,14 @@ export class TreeApi<T> {
selectContiguous(identity: Identity) {
if (!identity) return;
const id = identify(identity);
const { anchor, mostRecent } = this.state.nodes.selection;
this.dispatch(focus(id));
this.dispatch(selection.remove(this.nodesBetween(anchor, mostRecent)));
this.dispatch(selection.add(this.nodesBetween(anchor, identifyNull(id))));
this.dispatch(selection.mostRecent(id));
if (this.get(id)?.isSelectable) {
const {anchor, mostRecent} = this.state.nodes.selection;
const selectableNodes = this.filterSelectableNodes(this.nodesBetween(anchor, identifyNull(id)))
this.dispatch(selection.remove(this.nodesBetween(anchor, mostRecent)));
this.dispatch(selection.add(selectableNodes));
this.dispatch(selection.mostRecent(id));
}
this.scrollTo(id);
if (this.focusedNode) safeRun(this.props.onFocus, this.focusedNode);
safeRun(this.props.onSelect, this.selectedNodes);
Expand All @@ -375,8 +384,9 @@ export class TreeApi<T> {
}

selectAll() {
const allSelectableNodes = this.filterSelectableNodes(Object.keys(this.idToIndex));
this.setSelection({
ids: Object.keys(this.idToIndex),
ids: allSelectableNodes,
anchor: this.firstNode,
mostRecent: this.lastNode,
});
Expand All @@ -385,10 +395,16 @@ export class TreeApi<T> {
safeRun(this.props.onSelect, this.selectedNodes);
}

private filterSelectableNodes(nodes: (IdObj | string)[]) {
return nodes
.map(n => this.get(identify(n)))
.filter(n => !!n && n.isSelectable) as NodeApi<T>[];
}

setSelection(args: {
ids: (IdObj | string)[] | null;
anchor: IdObj | string | null;
mostRecent: IdObj | string | null;
anchor: Identity;
mostRecent: Identity;
}) {
const ids = new Set(args.ids?.map(identify));
const anchor = identifyNull(args.anchor);
Expand Down Expand Up @@ -580,16 +596,22 @@ export class TreeApi<T> {
}

isEditable(data: T) {
const check = this.props.disableEdit || (() => false);
return !utils.access(data, check) ?? true;
return this.isActionPossible(data, this.props.disableEdit);
}

isDraggable(data: T) {
const check = this.props.disableDrag || (() => false);
return !utils.access(data, check) ?? true;
return this.isActionPossible(data, this.props.disableDrag);
}

isSelectable(data: T) {
return this.isActionPossible(data, this.props.disableSelect);
}

private isActionPossible(data: T, disabler: string | boolean | BoolFunc<T> = (() => false)) {
return !utils.access(data, disabler);
}

isDragging(node: string | IdObj | null) {
isDragging(node: Identity) {
const id = identifyNull(node);
if (!id) return false;
return this.state.nodes.drag.id === id;
Expand All @@ -603,7 +625,7 @@ export class TreeApi<T> {
return this.matchFn(node);
}

willReceiveDrop(node: string | IdObj | null) {
willReceiveDrop(node: Identity) {
const id = identifyNull(node);
if (!id) return false;
return id === this.state.nodes.drag.idWillReceiveDrop;
Expand Down
1 change: 1 addition & 0 deletions packages/react-arborist/src/types/tree-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface TreeProps<T> {
openByDefault?: boolean;
selectionFollowsFocus?: boolean;
disableMultiSelection?: boolean;
disableSelect?: string | boolean | BoolFunc<T>;
disableEdit?: string | boolean | BoolFunc<T>;
disableDrag?: string | boolean | BoolFunc<T>;
disableDrop?:
Expand Down
4 changes: 3 additions & 1 deletion packages/showcase/pages/gmail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default function GmailSidebar() {
renderCursor={Cursor}
searchTerm={term}
paddingBottom={32}
disableSelect={(data) => ["Categories", "Spam"].includes(data.name)}
disableEdit={(data) => data.readOnly}
disableDrop={({ parentNode, dragNodes }) => {
if (
Expand Down Expand Up @@ -78,7 +79,7 @@ export default function GmailSidebar() {
<p>The tree is fully functional. Try the following:</p>
<ul>
<li>Drag the items around</li>
<li>Try to drag Inbox into Categories (not allowed)</li>
<li>Try to drag Inbox into {"'"}Categories{"'"} (not allowed)</li>
<li>Move focus with the arrow keys</li>
<li>Toggle folders (press spacebar)</li>
<li>
Expand All @@ -89,6 +90,7 @@ export default function GmailSidebar() {
<li>Create a new folder (press shift+A)</li>
<li>Delete items (press delete)</li>
<li>Select multiple items with shift or meta</li>
<li>{"'"}Categories{"'"} and {"'"}Spam{"'"} cannot be selected</li>
<li>
Filter the tree by typing in this text box:{" "}
<input
Expand Down