Skip to content

Commit

Permalink
Prevent Selection Sync From Stealing Focus (#118)
Browse files Browse the repository at this point in the history
Prevent Selection Sync From Stealing Focus Fixes #117
  • Loading branch information
jameskerr authored Feb 12, 2023
1 parent abfc6d6 commit ac9ef69
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ yarn-error.log*
!.yarn/versions

.parcel-cache
.vscode
11 changes: 11 additions & 0 deletions packages/e2e/cypress/e2e/cities-spec.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
describe("Testing the Cities Demo", () => {
beforeEach(() => {
cy.visit("http://localhost:3000/cities");
});

it("Does not steel the selection when the selection prop changes", () => {
cy.get("button").contains("Select San Francisco").click();
cy.focused().invoke("is", "button").should("equal", true);
cy.focused().should("have.text", "Select San Francisco");
});
});
2 changes: 1 addition & 1 deletion packages/react-arborist/src/components/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function TreeProvider<T>({
/* Change selection based on props */
useEffect(() => {
if (api.props.selection) {
api.select(api.props.selection);
api.select(api.props.selection, { focus: false });
} else {
api.deselectAll();
}
Expand Down
9 changes: 6 additions & 3 deletions packages/react-arborist/src/interfaces/tree-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,15 +323,18 @@ export class TreeApi<T> {
this.focus(this.at(index));
}

select(node: Identity, opts: { align?: Align } = {}) {
select(node: Identity, opts: { align?: Align; focus?: boolean } = {}) {
if (!node) return;
const changeFocus = opts.focus !== false;
const id = identify(node);
this.dispatch(focus(id));
if (changeFocus) this.dispatch(focus(id));
this.dispatch(selection.only(id));
this.dispatch(selection.anchor(id));
this.dispatch(selection.mostRecent(id));
this.scrollTo(id, opts.align);
if (this.focusedNode) safeRun(this.props.onFocus, this.focusedNode);
if (this.focusedNode && changeFocus) {
safeRun(this.props.onFocus, this.focusedNode);
}
safeRun(this.props.onSelect, this.selectedNodes);
}

Expand Down

0 comments on commit ac9ef69

Please sign in to comment.