forked from primer/view_components
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtree_view_preview.rb
More file actions
251 lines (229 loc) · 7.51 KB
/
Copy pathtree_view_preview.rb
File metadata and controls
251 lines (229 loc) · 7.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# frozen_string_literal: true
module Primer
module Alpha
# @label TreeView
class TreeViewPreview < ViewComponent::Preview
# @label Default
#
# @snapshot interactive
# @param expanded [Boolean] toggle
# @param disabled [Boolean] toggle
# @param select_variant [Symbol] select [multiple, single, none]
# @param select_strategy [Symbol] select [self, descendants, mixed_descendants]
def default(
expanded: false,
disabled: false,
select_variant: Primer::Alpha::TreeView::Node::DEFAULT_SELECT_VARIANT,
select_strategy: Primer::Alpha::TreeView::SubTreeNode::DEFAULT_SELECT_STRATEGY
)
render_with_template(locals: {
expanded: coerce_bool(expanded),
disabled: coerce_bool(disabled),
select_variant: select_variant.to_sym,
select_strategy: select_strategy.to_sym
})
end
# @label Playground
#
# @param expanded [Boolean] toggle
# @param select_variant [Symbol] select [multiple, single, none]
# @param select_strategy [Symbol] select [self, descendants, mixed_descendants]
def playground(
expanded: false,
select_variant: Primer::Alpha::TreeView::Node::DEFAULT_SELECT_VARIANT,
select_strategy: Primer::Alpha::TreeView::SubTreeNode::DEFAULT_SELECT_STRATEGY
)
render_with_template(locals: {
expanded: coerce_bool(expanded),
select_variant: select_variant.to_sym,
select_strategy: select_strategy.to_sym,
populate: -> (*args) { populate(*args) }
})
end
# @label Single select
#
# @snapshot interactive
# @param expanded [Boolean] toggle
# @param disabled [Boolean] toggle
def single_select(
expanded: false,
disabled: false
)
render_with_template(locals: {
expanded: coerce_bool(expanded),
disabled: coerce_bool(disabled),
select_variant: :single,
})
end
# @label Multi select
#
# @snapshot interactive
# @param expanded [Boolean] toggle
# @param disabled [Boolean] toggle
def multi_select(
expanded: false,
disabled: false
)
render_with_template(locals: {
expanded: coerce_bool(expanded),
disabled: coerce_bool(disabled),
select_variant: :multiple,
})
end
# @label Empty
#
# @snapshot interactive
def empty
end
# @label Loading failure
#
# @snapshot interactive
def loading_failure
end
# @label Loading spinner
#
# @snapshot interactive
# @param simulate_failure [Boolean] toggle
# @param simulate_empty [Boolean] toggle
def loading_spinner(simulate_failure: false, simulate_empty: false)
render_with_template(locals: {
simulate_failure: coerce_bool(simulate_failure),
simulate_empty: coerce_bool(simulate_empty),
})
end
# @label Loading skeleton
#
# @snapshot interactive
# @param simulate_failure [Boolean] toggle
# @param simulate_empty [Boolean] toggle
def loading_skeleton(simulate_failure: false, simulate_empty: false)
render_with_template(locals: {
simulate_failure: coerce_bool(simulate_failure),
simulate_empty: coerce_bool(simulate_empty)
})
end
# @label Async alpha
#
# @param action_menu_expanded [Boolean] toggle
def async_alpha(action_menu_expanded: false)
render_with_template(locals: {
action_menu_expanded: coerce_bool(action_menu_expanded)
})
end
# @label Leaf node playground
#
# @param label [String] text
# @param leading_visual_icon [Symbol] octicon
# @param leading_action_icon [Symbol] octicon
# @param trailing_visual_icon [Symbol] octicon
# @param trailing_action_icon [Symbol] octicon
# @param select_variant [Symbol] select [multiple, single, none]
# @param disabled [Boolean] toggle
def leaf_node_playground(
label: "Leaf node",
leading_visual_icon: nil,
leading_action_icon: nil,
trailing_visual_icon: nil,
trailing_action_icon: nil,
select_variant: Primer::Alpha::TreeView::Node::DEFAULT_SELECT_VARIANT,
disabled: false
)
render_with_template(locals: {
label: label,
leading_visual_icon: leading_visual_icon,
leading_action_icon: leading_action_icon,
trailing_visual_icon: trailing_visual_icon,
trailing_action_icon: trailing_action_icon,
select_variant: select_variant.to_sym,
disabled: disabled
})
end
# @label Links
#
# @param expanded [Boolean] toggle
# @param disabled [Boolean] toggle
def links(expanded: false, disabled: false)
render_with_template(locals: {
expanded: coerce_bool(expanded),
disabled: coerce_bool(disabled)
})
end
# @label Buttons
#
# @param expanded [Boolean] toggle
# @param disabled [Boolean] toggle
def buttons(expanded: false, disabled: false)
render_with_template(locals: {
expanded: coerce_bool(expanded),
disabled: coerce_bool(disabled)
})
end
# @label Auto expansion
#
def auto_expansion
render(Primer::Alpha::TreeView.new) do |tree|
tree.with_sub_tree(label: "Level 1") do |level1|
level1.with_sub_tree(label: "Level 2") do |level2|
# marking this node as expanded should automatically expand all ancestors
level2.with_sub_tree(label: "Level 3", expanded: true) do |level3|
level3.with_leaf(label: "Level 4")
end
end
end
end
end
# @label Form input
#
# @param select_variant [Symbol] select [multiple, single]
# @param expanded [Boolean] toggle
def form_input(select_variant: :multiple, expanded: true)
render_with_template(locals: {
select_variant: select_variant.to_sym,
expanded: coerce_bool(expanded)
})
end
# @label Doubled paths
# @hidden
def doubled_path
render_with_template
end
private
def coerce_bool(value)
case value
when true, false
value
when "true"
true
when "false"
false
else
false
end
end
def populate(node, data, node_arguments)
return unless data
entries = (
data.fetch("children", {}).keys.map { |label, idx| [label, :directory] } +
data.fetch("files", []).map { |label| [label, :file] }
)
entries.sort_by!(&:first)
entries.each do |label, kind, idx|
case kind
when :directory
node.with_sub_tree(label: label, **node_arguments) do |sub_tree|
sub_tree.with_leading_visual_icons do |icons|
icons.with_expanded_icon(icon: :"file-directory-open-fill", color: :accent)
icons.with_collapsed_icon(icon: :"file-directory-fill", color: :accent)
end
populate(sub_tree, data["children"][label], node_arguments)
end
when :file
node.with_leaf(label: label, **node_arguments) do |leaf|
leaf.with_leading_visual_icon(icon: :file)
end
end
end
end
end
end
end