Skip to content

Commit fb09d4d

Browse files
authored
Don't unselect on click and Enter (#116)
* 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]>
1 parent e7ba95e commit fb09d4d

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

packages/components/src/tree-view/index.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
// Distributed under the terms of the Modified BSD License.
44

55
import {
6+
isTreeItemElement,
67
treeViewTemplate as template,
7-
TreeView
8+
TreeView,
9+
type TreeItem
810
} from '@microsoft/fast-foundation';
911
import { treeViewStyles as styles } from './tree-view.styles.js';
1012

@@ -14,7 +16,41 @@ import { treeViewStyles as styles } from './tree-view.styles.js';
1416
* @public
1517
* @tagname jp-tree-view
1618
*/
17-
class JupyterTreeView extends TreeView {}
19+
class JupyterTreeView extends TreeView {
20+
/**
21+
* Handles click events bubbling up
22+
*
23+
* @internal
24+
*/
25+
public handleClick(e: Event): boolean | void {
26+
if (e.defaultPrevented) {
27+
// handled, do nothing
28+
return;
29+
}
30+
31+
if (!(e.target instanceof Element)) {
32+
// not a tree item, ignore
33+
return true;
34+
}
35+
36+
let item = e.target as Element | null;
37+
while (item && !isTreeItemElement(item)) {
38+
item = item.parentElement;
39+
40+
// Escape if we get out of the tree view component
41+
if (item === this) {
42+
item = null;
43+
}
44+
}
45+
46+
if (item && !(item as TreeItem).disabled) {
47+
// Force selection - it is not possible to unselect
48+
(item as TreeItem).selected = true;
49+
}
50+
51+
return;
52+
}
53+
}
1854

1955
/**
2056
* A function that returns a {@link @microsoft/fast-foundation#TreeView} registration for configuring the component with a DesignSystem.

packages/components/src/tree-view/tree-view.stories.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const Template: StoryFn = (args): HTMLElement => {
3636
<jp-tree-item>Nested item 3</jp-tree-item>
3737
</jp-tree-item>
3838
<jp-tree-item expanded>
39-
Root item 2
39+
Root <em style="padding: 0 0.2rem;">item</em> 2
4040
<jp-tree-item>
4141
Flowers
4242
<jp-tree-item disabled>Daisy</jp-tree-item>
Loading

0 commit comments

Comments
 (0)