@@ -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
488497if ( ! window . customElements . get ( 'tree-view' ) ) {
0 commit comments