Skip to content

Commit 11668f7

Browse files
authored
Merge pull request #384 from opf/bug/68698-form-state-is-not-initialized-when-rendering-a-tree-view-with-already-selected-items
[68698] Form state is not initialized when rendering a tree view with already selected items
2 parents e39baf0 + c9f8de4 commit 11668f7

13 files changed

Lines changed: 120 additions & 28 deletions

File tree

.changeset/brave-horses-pump.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openproject/primer-view-components': patch
3+
---
4+
5+
Initiate form state for TreeView
86 Bytes
Loading
86 Bytes
Loading
-477 Bytes
Loading
4 Bytes
Loading
-46 Bytes
Loading
-46 Bytes
Loading
-10 Bytes
Loading

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

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,7 @@ export class TreeViewElement extends HTMLElement {
4545

4646
if (!somethingChanged) return
4747

48-
const newInputs = []
49-
50-
// eslint-disable-next-line custom-elements/no-dom-traversal-in-connectedcallback
51-
for (const node of this.querySelectorAll('[role=treeitem][aria-checked=true]')) {
52-
const newInput = this.formInputPrototype.cloneNode() as HTMLInputElement
53-
newInput.removeAttribute('data-target')
54-
newInput.removeAttribute('form')
55-
56-
const payload: {path: string[]; value?: string} = {
57-
path: this.getNodePath(node),
58-
}
59-
60-
const inputValue = this.getFormInputValueForNode(node)
61-
if (inputValue) payload.value = inputValue
62-
63-
newInput.value = JSON.stringify(payload)
64-
newInputs.push(newInput)
65-
}
66-
67-
this.formInputContainer.replaceChildren(...newInputs)
48+
this.updateHiddenFormInputs()
6849
})
6950

7051
updateInputsObserver.observe(this, {
@@ -73,6 +54,11 @@ export class TreeViewElement extends HTMLElement {
7354
attributeFilter: ['aria-checked'],
7455
})
7556

57+
// Correctly initialize the form
58+
if (this.formInputContainer) {
59+
this.updateHiddenFormInputs()
60+
}
61+
7662
// eslint-disable-next-line github/no-then -- We don't want to wait for this to resolve, just get on with it
7763
customElements.whenDefined('tree-view-sub-tree-node').then(() => {
7864
// depends on TreeViewSubTreeNodeElement#eachAncestorSubTreeNode, which may not be defined yet
@@ -483,6 +469,29 @@ export class TreeViewElement extends HTMLElement {
483469
selectVariant(node: Element): SelectVariant {
484470
return (node.getAttribute('data-select-variant') || 'none') as SelectVariant
485471
}
472+
473+
updateHiddenFormInputs() {
474+
const newInputs = []
475+
476+
// eslint-disable-next-line custom-elements/no-dom-traversal-in-connectedcallback
477+
for (const node of this.querySelectorAll('[role=treeitem][aria-checked=true]')) {
478+
const newInput = this.formInputPrototype.cloneNode() as HTMLInputElement
479+
newInput.removeAttribute('data-target')
480+
newInput.removeAttribute('form')
481+
482+
const payload: {path: string[]; value?: string} = {
483+
path: this.getNodePath(node),
484+
}
485+
486+
const inputValue = this.getFormInputValueForNode(node)
487+
if (inputValue) payload.value = inputValue
488+
489+
newInput.value = JSON.stringify(payload)
490+
newInputs.push(newInput)
491+
}
492+
493+
this.formInputContainer.replaceChildren(...newInputs)
494+
}
486495
}
487496

488497
if (!window.customElements.get('tree-view')) {

previews/primer/alpha/tree_view_preview/form_input.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<%= render(Primer::Alpha::Stack.new) do %>
33
<%= render(Primer::Alpha::TreeView.new(form_arguments: { builder: f, name: "folder_structure" })) do |tree| %>
44
<% tree.with_sub_tree(label: "src", expanded: expanded, select_variant: select_variant, value: 0) do |sub_tree| %>
5-
<% sub_tree.with_leaf(label: "button.rb", select_variant: select_variant, value: 1) %>
5+
<% sub_tree.with_leaf(label: "button.rb", select_variant: select_variant, value: 1, checked: true) %>
66
<% sub_tree.with_leaf(label: "icon_button.rb", current: true, select_variant: select_variant, value: 2) %>
77
<% end %>
88

0 commit comments

Comments
 (0)