@@ -21,34 +21,40 @@ export default class PgTreeItem extends HTMLElement {
2121
2222 @Prop ( ) index : number ;
2323 @Prop ( ) label : string = '' ;
24+ @Prop ( ) selected : boolean = false ;
2425 @Prop ( ) icon : { path : string } ;
2526 @Prop ( ) actions : any [ ] = [ ] ;
27+ @Prop ( ) items : any [ ] = [ ] ;
2628
2729 @Part ( ) $item : HTMLDivElement ;
2830 @Part ( ) $input : HTMLInputElement ;
2931 @Part ( ) $button : HTMLButtonElement ;
3032 @Part ( ) $icon : PgIcon ;
3133 @Part ( ) $label : HTMLDivElement ;
3234 @Part ( ) $actions : HTMLDivElement ;
35+ @Part ( ) $items : HTMLDivElement ;
3336
3437 connectedCallback ( ) {
3538 this . $item . addEventListener ( 'action' , this . #handleAction. bind ( this ) ) ;
3639 this . $button . addEventListener ( 'dblclick' , this . #handleRename. bind ( this ) ) ;
40+ this . $button . addEventListener ( 'click' , this . #handleClick. bind ( this ) ) ;
3741 this . $item . addEventListener ( 'contextmenu' , this . #handleContextMenu. bind ( this ) ) ;
3842 this . $input . addEventListener ( 'blur' , this . #handleBlur. bind ( this ) ) ;
3943 this . $input . addEventListener ( 'keydown' , this . #handleKeyDown. bind ( this ) ) ;
44+ this . $items . addEventListener ( 'select' , this . #handleSelect. bind ( this ) ) ;
4045 forEach ( {
4146 container : this . $actions ,
4247 items : this . actions ,
4348 type : ( item ) => {
4449 return PgTreeButtonIcon ;
45- } ,
46- create : ( $item , item ) => {
47- // after creation of $item element
48- } ,
49- update : ( $item , item ) => {
50- // after every $item update
51- } ,
50+ }
51+ } ) ;
52+ forEach ( {
53+ container : this . $items ,
54+ items : this . items ,
55+ type : ( item ) => {
56+ return PgTreeItem ;
57+ }
5258 } ) ;
5359 }
5460
@@ -59,6 +65,19 @@ export default class PgTreeItem extends HTMLElement {
5965 if ( changes . icon && this . icon ) {
6066 this . $icon . path = this . icon . path ;
6167 }
68+ if ( changes . selected ) {
69+ this . $item . classList . toggle ( 'selected' , this . selected ) ;
70+ }
71+ }
72+
73+ #handleClick( ) {
74+ this . dispatchEvent ( new CustomEvent ( 'select' , {
75+ bubbles : true ,
76+ composed : true ,
77+ detail : {
78+ indexes : [ this . index ]
79+ }
80+ } ) ) ;
6281 }
6382
6483 #handleAction( e ) {
@@ -73,6 +92,10 @@ export default class PgTreeItem extends HTMLElement {
7392 } ) ) ;
7493 }
7594
95+ #handleSelect( e : any ) {
96+ e . detail . indexes . unshift ( this . index ) ;
97+ }
98+
7699 #handleContextMenu( e ) {
77100 e . preventDefault ( ) ;
78101 }
@@ -102,8 +125,17 @@ export default class PgTreeItem extends HTMLElement {
102125 }
103126
104127 #handleKeyDown( e : KeyboardEvent ) {
105- if ( e . key === 'Enter' ) {
106- this . #handleBlur( ) ;
128+ switch ( e . key ) {
129+ case 'Enter' :
130+ this . #handleBlur( ) ;
131+ break ;
132+ case 'Escape' :
133+ this . $button . classList . remove ( 'hide' ) ;
134+ this . $actions . classList . remove ( 'hide' ) ;
135+ this . $input . classList . add ( 'hide' ) ;
136+ this . $input . value = this . label ;
137+ this . $button . focus ( ) ;
138+ break ;
107139 }
108140 }
109141
0 commit comments