1
1
import { LitElement , html , TemplateResult } from "lit" ;
2
2
import { property , state } from "lit/decorators.js" ;
3
3
import { repeat } from "lit/directives/repeat.js" ;
4
+ import { unsafeHTML } from "lit/directives/unsafe-html.js" ;
4
5
import * as _ from "lodash-es" ;
5
- import { noKeyWarning , renderInTooltip } from "./utils.js" ;
6
+ import { noKeyWarning , renderInTooltip , generateUniqueKey } from "./utils.js" ;
6
7
import "@shoelace-style/shoelace/dist/components/details/details.js" ;
7
8
import "@shoelace-style/shoelace/dist/components/button/button.js" ;
8
9
import formStyles from "./form.styles.js" ;
@@ -29,27 +30,22 @@ export default class EccUtilsDesignFormGroup extends LitElement {
29
30
@property ( { type : Boolean , reflect : true } ) collapsible = false ;
30
31
31
32
@state ( ) private arrayInstances : Array < {
32
- id : number ;
33
- items : Element [ ] ;
33
+ id : string ;
34
+ content : string ;
34
35
} > = [ ] ;
35
36
36
- @state ( ) private originalInstance : Element [ ] = [ ] ;
37
- @state ( ) private items : Array < Element > = [ ] ;
37
+ @state ( ) private content = "" ;
38
38
@state ( ) private path = "" ;
39
39
40
40
declare setHTMLUnsafe : ( htmlString : string ) => void ;
41
41
protected firstUpdated ( ) : void {
42
+ this . content = this . innerHTML ;
43
+
42
44
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
+ } ) ) ;
53
49
}
54
50
55
51
this . setHTMLUnsafe ( "" ) ;
@@ -89,41 +85,33 @@ export default class EccUtilsDesignFormGroup extends LitElement {
89
85
}
90
86
91
87
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
+ >
117
94
< div
118
95
class ="group-content "
119
96
ecc-group
120
97
ecc-group-key ="${ this . key } "
121
98
path ="${ this . path } "
122
99
>
123
- ${ item }
100
+ ${ unsafeHTML ( this . content ) }
124
101
</ 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
+ ` ;
127
115
}
128
116
129
117
private renderArrayTemplate ( ) : TemplateResult {
@@ -143,12 +131,13 @@ export default class EccUtilsDesignFormGroup extends LitElement {
143
131
144
132
const addItem = ( ) => {
145
133
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 ] ;
150
140
151
- this . requestUpdate ( ) ;
152
141
this . dispatchEvent (
153
142
new CustomEvent ( "ecc-utils-array-add" , {
154
143
detail : {
@@ -168,7 +157,7 @@ export default class EccUtilsDesignFormGroup extends LitElement {
168
157
newItems . splice ( index , 1 ) ;
169
158
170
159
this . arrayInstances = newItems ;
171
- this . requestUpdate ( ) ;
160
+
172
161
this . dispatchEvent (
173
162
new CustomEvent ( "ecc-utils-array-delete" , {
174
163
detail : {
@@ -182,14 +171,6 @@ export default class EccUtilsDesignFormGroup extends LitElement {
182
171
}
183
172
} ;
184
173
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
-
193
174
return html `
194
175
< div
195
176
class ="array-container "
@@ -268,11 +249,7 @@ export default class EccUtilsDesignFormGroup extends LitElement {
268
249
</ svg >
269
250
</ sl-button >
270
251
< div class ="array-item-container ">
271
- ${ repeat (
272
- instance . items ,
273
- ( item ) => item . id ,
274
- ( item ) => arrayDiv ( item , index )
275
- ) }
252
+ ${ unsafeHTML ( instance . content ) }
276
253
</ div >
277
254
</ div >
278
255
`
0 commit comments