@@ -13,28 +13,8 @@ export type ModelEntryType = {
1313
1414@customElement ( 'example-sorter-group' )
1515export 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:
3818 #sorter = new UmbSorterController < ModelEntryType , ExampleSorterItem > ( this , {
3919 getUniqueOfElement : ( element ) => {
4020 return element . name ;
@@ -46,26 +26,45 @@ export class ExampleSorterGroup extends UmbElementMixin(LitElement) {
4626 itemSelector : 'example-sorter-item' ,
4727 containerSelector : '.sorter-container' ,
4828 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' ) ) ;
5234 } ,
5335 } ) ;
5436
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+
5549 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 ) ;
5751 } ;
5852
5953 render ( ) {
6054 return html `
6155 <div class= "sorter-container" >
6256 ${ 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.
6459 ( item ) => item . name ,
6560 ( item ) =>
6661 html `<example-sor ter- item name= ${ item . name } >
6762 <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>
6968 </ example-sor ter- item> ` ,
7069 ) }
7170 </ div>
0 commit comments