Skip to content

Commit 03ed5a1

Browse files
committed
Introduce mixed_descendants selection strategy
1 parent 5e6c8e8 commit 03ed5a1

3 files changed

Lines changed: 27 additions & 15 deletions

File tree

app/components/primer/alpha/tree_view/sub_tree_node.rb

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ class TreeView
88
# This component is part of the <%= link_to_component(Primer::Alpha::TreeView) %> component and should
99
# not be used directly.
1010
class SubTreeNode < Primer::Component
11-
DEFAULT_SELECT_STRATEGY = :descendants
11+
DEFAULT_SELECT_STRATEGY = :mixed_descendants
1212
SELECT_STRATEGIES = [
1313
:self,
14-
DEFAULT_SELECT_STRATEGY
14+
DEFAULT_SELECT_STRATEGY,
15+
:descendants
1516
]
1617

1718
# @!parse
@@ -59,7 +60,7 @@ class SubTreeNode < Primer::Component
5960
visual: IconPair.new(
6061
**system_arguments,
6162
expanded: @sub_tree.expanded?,
62-
)
63+
)
6364
)
6465
}
6566
}
@@ -108,10 +109,19 @@ class SubTreeNode < Primer::Component
108109
# @param label [String] The node's label, i.e. it's textual content.
109110
# @param path [Array<String>] The node's "path," i.e. this node's label and the labels of all its ancestors. This node should be reachable by traversing the tree following this path.
110111
# @param node_variant [Symbol] The variant to use for this node. <%= one_of(Primer::Alpha::TreeView::NODE_VARIANT_OPTIONS) %>
112+
# @param sub_tree_component_klass [Class] The class to use for the sub-tree instead of the default <%= link_to_component(Primer::Alpha::TreeView::SubTree) %>
111113
# @param expanded [Boolean] Whether or not this sub-tree should be rendered expanded.
112114
# @param select_strategy [Symbol] What should happen when this sub-tree node is checked. <%= one_of(Primer::Alpha::TreeView::SubTreeNode::SELECT_STRATEGIES) %>
113115
# @param system_arguments [Hash] The arguments accepted by <%= link_to_component(Primer::Alpha::TreeView::Node) %>.
114-
def initialize(label:, path:, node_variant:, expanded: false, select_strategy: DEFAULT_SELECT_STRATEGY, **system_arguments)
116+
def initialize(
117+
label:,
118+
path:,
119+
node_variant:,
120+
sub_tree_component_klass: SubTree,
121+
expanded: false,
122+
select_strategy: DEFAULT_SELECT_STRATEGY,
123+
**system_arguments
124+
)
115125
@label = label
116126
@system_arguments = system_arguments
117127
@select_strategy = fetch_or_fallback(SELECT_STRATEGIES, select_strategy, DEFAULT_SELECT_STRATEGY)
@@ -123,16 +133,16 @@ def initialize(label:, path:, node_variant:, expanded: false, select_strategy: D
123133

124134
@system_arguments[:data] = merge_data(
125135
@system_arguments, {
126-
data: {
127-
target: "tree-view-sub-tree-node.node",
128-
"node-type": "sub-tree"
129-
}
136+
data: {
137+
target: "tree-view-sub-tree-node.node",
138+
"node-type": "sub-tree"
130139
}
140+
}
131141
)
132142

133143
sub_tree_arguments = @system_arguments.delete(:sub_tree_arguments) || {}
134144

135-
@sub_tree = SubTree.new(
145+
@sub_tree = sub_tree_component_klass.new(
136146
expanded: expanded,
137147
path: path,
138148
node_variant: node_variant,

app/components/primer/alpha/tree_view/tree_view_sub_tree_node_element.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import type {TreeViewNodeInfo} from '../../shared_events'
77

88
type LoadingState = 'loading' | 'error' | 'success'
99

10+
export type SelectStrategy = 'self' | 'descendants' | 'mixed_descendants'
11+
1012
@controller
1113
export class TreeViewSubTreeNodeElement extends HTMLElement {
1214
@target node: HTMLElement
@@ -70,7 +72,7 @@ export class TreeViewSubTreeNodeElement extends HTMLElement {
7072
)
7173

7274
const checkedMutationObserver = new MutationObserver(() => {
73-
if (this.selectStrategy !== 'descendants') return
75+
if (this.selectStrategy !== 'mixed_descendants') return
7476

7577
let checkType = 'unknown'
7678

@@ -127,8 +129,8 @@ export class TreeViewSubTreeNodeElement extends HTMLElement {
127129
this.#update()
128130
}
129131

130-
get selectStrategy(): string {
131-
return this.node.getAttribute('data-select-strategy') || 'descendants'
132+
get selectStrategy(): SelectStrategy {
133+
return (this.node.getAttribute('data-select-strategy') || 'descendants') as SelectStrategy
132134
}
133135

134136
disconnectedCallback() {
@@ -375,7 +377,7 @@ export class TreeViewSubTreeNodeElement extends HTMLElement {
375377
const rootInfo = this.treeView?.infoFromNode(this.node, newCheckValue)
376378
if (rootInfo) nodeInfos.push(rootInfo)
377379

378-
if (this.selectStrategy === 'descendants') {
380+
if (this.selectStrategy === 'descendants' || this.selectStrategy === 'mixed_descendants') {
379381
for (const node of this.eachDescendantNode()) {
380382
const info = this.treeView?.infoFromNode(node, newCheckValue)
381383
if (info) nodeInfos.push(info)

previews/primer/alpha/tree_view_preview.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class TreeViewPreview < ViewComponent::Preview
1010
# @param expanded [Boolean] toggle
1111
# @param disabled [Boolean] toggle
1212
# @param select_variant [Symbol] select [multiple, none]
13-
# @param select_strategy [Symbol] select [self, descendants]
13+
# @param select_strategy [Symbol] select [self, descendants, mixed_descendants]
1414
def default(
1515
expanded: false,
1616
disabled: false,
@@ -29,7 +29,7 @@ def default(
2929
#
3030
# @param expanded [Boolean] toggle
3131
# @param select_variant [Symbol] select [multiple, none]
32-
# @param select_strategy [Symbol] select [self, descendants]
32+
# @param select_strategy [Symbol] select [self, descendants, mixed_descendants]
3333
def playground(
3434
expanded: false,
3535
select_variant: Primer::Alpha::TreeView::Node::DEFAULT_SELECT_VARIANT,

0 commit comments

Comments
 (0)