@@ -13,28 +13,8 @@ export type ModelEntryType = {
13
13
14
14
@customElement ( 'example-sorter-group' )
15
15
export class ExampleSorterGroup extends UmbElementMixin ( LitElement ) {
16
- @property ( { type : Array , attribute : false } )
17
- public get initialItems ( ) : ModelEntryType [ ] {
18
- return this . items ;
19
- }
20
- public set initialItems ( value : ModelEntryType [ ] ) {
21
- // Only want to set the model initially, cause any re-render should not set this again.
22
- if ( this . _items !== undefined ) return ;
23
- this . items = value ;
24
- }
25
-
26
- @property ( { type : Array , attribute : false } )
27
- public get items ( ) : ModelEntryType [ ] {
28
- return this . _items ?? [ ] ;
29
- }
30
- public set items ( value : ModelEntryType [ ] ) {
31
- const oldValue = this . _items ;
32
- this . _items = value ;
33
- this . #sorter. setModel ( this . _items ) ;
34
- this . requestUpdate ( 'items' , oldValue ) ;
35
- }
36
- private _items ?: ModelEntryType [ ] ;
37
-
16
+ //
17
+ // Sorter setup:
38
18
#sorter = new UmbSorterController < ModelEntryType , ExampleSorterItem > ( this , {
39
19
getUniqueOfElement : ( element ) => {
40
20
return element . name ;
@@ -46,26 +26,45 @@ export class ExampleSorterGroup extends UmbElementMixin(LitElement) {
46
26
itemSelector : 'example-sorter-item' ,
47
27
containerSelector : '.sorter-container' ,
48
28
onChange : ( { model } ) => {
49
- const oldValue = this . _items ;
50
- this . _items = model ;
51
- this . requestUpdate ( 'items' , oldValue ) ;
29
+ const oldValue = this . _value ;
30
+ this . _value = model ;
31
+ this . requestUpdate ( 'value' , oldValue ) ;
32
+ // Fire an event for the parent to know that the model has changed.
33
+ this . dispatchEvent ( new CustomEvent ( 'change' ) ) ;
52
34
} ,
53
35
} ) ;
54
36
37
+ @property ( { type : Array , attribute : false } )
38
+ public get value ( ) : ModelEntryType [ ] {
39
+ return this . _value ?? [ ] ;
40
+ }
41
+ public set value ( value : ModelEntryType [ ] ) {
42
+ const oldValue = this . _value ;
43
+ this . _value = value ;
44
+ this . #sorter. setModel ( this . _value ) ;
45
+ this . requestUpdate ( 'value' , oldValue ) ;
46
+ }
47
+ private _value ?: ModelEntryType [ ] ;
48
+
55
49
removeItem = ( item : ModelEntryType ) => {
56
- this . items = this . items . filter ( ( r ) => r . name !== item . name ) ;
50
+ this . value = this . value . filter ( ( r ) => r . name !== item . name ) ;
57
51
} ;
58
52
59
53
render ( ) {
60
54
return html `
61
55
<div class= "sorter-container" >
62
56
${ repeat (
63
- this . items ,
57
+ this . value ,
58
+ // Note: ideally you have an unique identifier for each item, but for this example we use the `name` as identifier.
64
59
( item ) => item . name ,
65
60
( item ) =>
66
61
html `<example-sor ter- item name= ${ item . name } >
67
62
<butto n slot= "action" @click = ${ ( ) => this . removeItem ( item ) } > Delete </ butto n>
68
- ${ item . children ? html `<example-sor ter- group .initialItems = ${ item . children } > </ example-sor ter- group> ` : '' }
63
+ <example-sor ter- group
64
+ .value = ${ item . children ?? [ ] }
65
+ @change = ${ ( e : Event ) => {
66
+ item . children = ( e . target as ExampleSorterGroup ) . value ;
67
+ } } > </ example-sor ter- group>
69
68
</ example-sor ter- item> ` ,
70
69
) }
71
70
</ div>
0 commit comments