Skip to content

Commit

Permalink
Don't unselect on click and Enter (#116)
Browse files Browse the repository at this point in the history
* Don't unselect on click and Enter
Select if click occurs in tree item sub element

* Update ref snapshots

---------

Co-authored-by: Frédéric Collonval <[email protected]>
  • Loading branch information
fcollonval and fcollonval authored Jul 6, 2024
1 parent e7ba95e commit fb09d4d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
40 changes: 38 additions & 2 deletions packages/components/src/tree-view/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
// Distributed under the terms of the Modified BSD License.

import {
isTreeItemElement,
treeViewTemplate as template,
TreeView
TreeView,
type TreeItem
} from '@microsoft/fast-foundation';
import { treeViewStyles as styles } from './tree-view.styles.js';

Expand All @@ -14,7 +16,41 @@ import { treeViewStyles as styles } from './tree-view.styles.js';
* @public
* @tagname jp-tree-view
*/
class JupyterTreeView extends TreeView {}
class JupyterTreeView extends TreeView {
/**
* Handles click events bubbling up
*
* @internal
*/
public handleClick(e: Event): boolean | void {
if (e.defaultPrevented) {
// handled, do nothing
return;
}

if (!(e.target instanceof Element)) {
// not a tree item, ignore
return true;
}

let item = e.target as Element | null;
while (item && !isTreeItemElement(item)) {
item = item.parentElement;

// Escape if we get out of the tree view component
if (item === this) {
item = null;
}
}

if (item && !(item as TreeItem).disabled) {
// Force selection - it is not possible to unselect
(item as TreeItem).selected = true;
}

return;
}
}

/**
* A function that returns a {@link @microsoft/fast-foundation#TreeView} registration for configuring the component with a DesignSystem.
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/tree-view/tree-view.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const Template: StoryFn = (args): HTMLElement => {
<jp-tree-item>Nested item 3</jp-tree-item>
</jp-tree-item>
<jp-tree-item expanded>
Root item 2
Root <em style="padding: 0 0.2rem;">item</em> 2
<jp-tree-item>
Flowers
<jp-tree-item disabled>Daisy</jp-tree-item>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit fb09d4d

Please sign in to comment.