11import { LitElement , html , TemplateResult } from "lit" ;
22import { property , state } from "lit/decorators.js" ;
33import { repeat } from "lit/directives/repeat.js" ;
4+ import { unsafeHTML } from "lit/directives/unsafe-html.js" ;
45import * as _ from "lodash-es" ;
5- import { noKeyWarning , renderInTooltip } from "./utils.js" ;
6+ import { noKeyWarning , renderInTooltip , generateUniqueKey } from "./utils.js" ;
67import "@shoelace-style/shoelace/dist/components/details/details.js" ;
78import "@shoelace-style/shoelace/dist/components/button/button.js" ;
89import formStyles from "./form.styles.js" ;
@@ -29,27 +30,22 @@ export default class EccUtilsDesignFormGroup extends LitElement {
2930 @property ( { type : Boolean , reflect : true } ) collapsible = false ;
3031
3132 @state ( ) private arrayInstances : Array < {
32- id : number ;
33- items : Element [ ] ;
33+ id : string ;
34+ content : string ;
3435 } > = [ ] ;
3536
36- @state ( ) private originalInstance : Element [ ] = [ ] ;
37- @state ( ) private items : Array < Element > = [ ] ;
37+ @state ( ) private content = "" ;
3838 @state ( ) private path = "" ;
3939
4040 declare setHTMLUnsafe : ( htmlString : string ) => void ;
4141 protected firstUpdated ( ) : void {
42+ this . content = this . innerHTML ;
43+
4244 if ( this . type === "array" ) {
43- this . originalInstance = Array . from ( this . querySelectorAll ( ":scope > *" ) ) ;
44- this . arrayInstances = Array . from (
45- { length : this . instances } ,
46- ( __ , index ) => ( {
47- id : index ,
48- items : this . originalInstance ,
49- } )
50- ) ;
51- } else {
52- this . items = Array . from ( this . querySelectorAll ( ":scope > *" ) ) ;
45+ this . arrayInstances = Array . from ( { length : this . instances } , ( ) => ( {
46+ id : generateUniqueKey ( ) ,
47+ content : this . content ,
48+ } ) ) ;
5349 }
5450
5551 this . setHTMLUnsafe ( "" ) ;
@@ -89,41 +85,33 @@ export default class EccUtilsDesignFormGroup extends LitElement {
8985 }
9086
9187 private renderGroupTemplate ( ) : TemplateResult {
92- return html `${ this . collapsible
93- ? repeat (
94- this . items ,
95- ( ) => _ . uniqueId ( "ecc-group-" ) ,
96- ( item ) => html `
97- < sl-details
98- data-testid ="group-collapsible "
99- summary =${ `${ this . label } ${ this . required ? "*" : "" } ` }
100- >
101- < div
102- class ="group-content "
103- ecc-group
104- ecc-group-key ="${ this . key } "
105- path ="${ this . path } "
106- >
107- ${ item }
108- </ div >
109- </ sl-details >
110- `
111- )
112- : repeat (
113- this . items ,
114- ( ) => _ . uniqueId ( "ecc-group-" ) ,
115- ( item ) => html `
116- < span > ${ this . label } ${ this . required ? "*" : "" } </ span >
88+ return this . collapsible
89+ ? html `
90+ < sl-details
91+ data-testid ="group-collapsible "
92+ summary =${ `${ this . label } ${ this . required ? "*" : "" } ` }
93+ >
11794 < div
11895 class ="group-content "
11996 ecc-group
12097 ecc-group-key ="${ this . key } "
12198 path ="${ this . path } "
12299 >
123- ${ item }
100+ ${ unsafeHTML ( this . content ) }
124101 </ div >
125- `
126- ) } `;
102+ </ sl-details >
103+ `
104+ : html `
105+ < span > ${ this . label } ${ this . required ? "*" : "" } </ span >
106+ < div
107+ class ="group-content "
108+ ecc-group
109+ ecc-group-key ="${ this . key } "
110+ path ="${ this . path } "
111+ >
112+ ${ unsafeHTML ( this . content ) }
113+ </ div >
114+ ` ;
127115 }
128116
129117 private renderArrayTemplate ( ) : TemplateResult {
@@ -143,12 +131,13 @@ export default class EccUtilsDesignFormGroup extends LitElement {
143131
144132 const addItem = ( ) => {
145133 if ( resolveAddButtonIsActive ( ) ) {
146- this . arrayInstances . push ( {
147- id : this . arrayInstances . length ,
148- items : this . originalInstance ,
149- } ) ;
134+ const newInstance = {
135+ id : generateUniqueKey ( ) ,
136+ content : this . content ,
137+ } ;
138+
139+ this . arrayInstances = [ ...this . arrayInstances , newInstance ] ;
150140
151- this . requestUpdate ( ) ;
152141 this . dispatchEvent (
153142 new CustomEvent ( "ecc-utils-array-add" , {
154143 detail : {
@@ -168,7 +157,7 @@ export default class EccUtilsDesignFormGroup extends LitElement {
168157 newItems . splice ( index , 1 ) ;
169158
170159 this . arrayInstances = newItems ;
171- this . requestUpdate ( ) ;
160+
172161 this . dispatchEvent (
173162 new CustomEvent ( "ecc-utils-array-delete" , {
174163 detail : {
@@ -182,14 +171,6 @@ export default class EccUtilsDesignFormGroup extends LitElement {
182171 }
183172 } ;
184173
185- const arrayDiv = ( item : Element , index : number ) => {
186- const div = document . createElement ( "div" ) ;
187- div . setAttribute ( "ecc-array-key" , `${ this . key } [${ index } ]` ) ;
188- div . innerHTML = item . outerHTML ;
189-
190- return div ;
191- } ;
192-
193174 return html `
194175 < div
195176 class ="array-container "
@@ -268,11 +249,7 @@ export default class EccUtilsDesignFormGroup extends LitElement {
268249 </ svg >
269250 </ sl-button >
270251 < div class ="array-item-container ">
271- ${ repeat (
272- instance . items ,
273- ( item ) => item . id ,
274- ( item ) => arrayDiv ( item , index )
275- ) }
252+ ${ unsafeHTML ( instance . content ) }
276253 </ div >
277254 </ div >
278255 `
0 commit comments