Skip to content

Commit 513ac01

Browse files
committed
Fix TreeView form state updates for inserted checked nodes
1 parent 5bbeec2 commit 513ac01

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ export class TreeViewElement extends HTMLElement {
3939
// nodes based on the component's select strategy. These two observers can conflict and cause infinite
4040
// looping, so we make sure something actually changed before computing inputs again.
4141
const somethingChanged = mutations.some(m => {
42+
if (m.type === 'childList') {
43+
return [...m.addedNodes].some(node => {
44+
if (!(node instanceof Element)) return false
45+
46+
return (
47+
node.matches('[role=treeitem][aria-checked=true]') ||
48+
Boolean(node.querySelector('[role=treeitem][aria-checked=true]'))
49+
)
50+
})
51+
}
52+
4253
if (!(m.target instanceof HTMLElement)) return false
4354
return m.target.getAttribute('aria-checked') !== m.oldValue
4455
})

test/system/alpha/tree_view_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,32 @@ def test_initial_form_state
931931
assert_equal "{\"path\":[\"src\",\"button.rb\"],\"nodeId\":\"src-button-rb\",\"value\":\"1\"}", response.dig("form_params", "folder_structure", 0)
932932
end
933933

934+
def test_form_state_updates_when_checked_nodes_are_inserted
935+
visit_preview(:form_input, expanded: true, route_format: :json)
936+
937+
evaluate_multiline_script(<<~JS)
938+
const tree = document.querySelector("tree-view")
939+
const checkedNode = tree.querySelector("[role=treeitem][aria-checked=true]")
940+
const clone = checkedNode.closest("li").cloneNode(true)
941+
const clonedTreeItem = clone.querySelector("[role=treeitem]")
942+
943+
clonedTreeItem.removeAttribute("id")
944+
clonedTreeItem.removeAttribute("data-node-id")
945+
clonedTreeItem.removeAttribute("data-value")
946+
clonedTreeItem.setAttribute("data-path", JSON.stringify(["async.rb"]))
947+
948+
tree.querySelector(":scope > ul").append(clone)
949+
JS
950+
951+
assert_selector("[data-target='tree-view.formInputContainer'] input", count: 2, visible: :all)
952+
953+
find("button[type=submit]").click
954+
955+
response = JSON.parse(find("pre").text)
956+
957+
assert_includes response.dig("form_params", "folder_structure"), "{\"path\":[\"async.rb\"]}"
958+
end
959+
934960
def test_form_submission_with_single_select_variant
935961
visit_preview(:form_input, expanded: true, select_variant: :single, route_format: :json)
936962

0 commit comments

Comments
 (0)