@@ -7,7 +7,8 @@ import type {
7
7
SlMenuItem ,
8
8
} from "@shoelace-style/shoelace" ;
9
9
import { serialize } from "@shoelace-style/shoelace/dist/utilities/form.js" ;
10
- import { css , html , nothing } from "lit" ;
10
+ import Fuse from "fuse.js" ;
11
+ import { css , html , nothing , type PropertyValues } from "lit" ;
11
12
import { customElement , property , query , state } from "lit/decorators.js" ;
12
13
import { when } from "lit/directives/when.js" ;
13
14
@@ -56,12 +57,62 @@ export class OrgsList extends BtrixElement {
56
57
@query ( "#orgDeleteButton" )
57
58
private readonly orgDeleteButton ?: SlButton | null ;
58
59
60
+ // For fuzzy search:
61
+ private readonly fuse = new Fuse ( this . orgList ?? [ ] , {
62
+ keys : [
63
+ "id" ,
64
+ "name" ,
65
+ "slug" ,
66
+ "users.name" ,
67
+ "users.email" ,
68
+ "subscription.subId" ,
69
+ "subscription.planId" ,
70
+ ] ,
71
+ useExtendedSearch : true ,
72
+ } ) ;
73
+
74
+ @state ( )
75
+ private search = "" ;
76
+
77
+ protected willUpdate ( changedProperties : PropertyValues < this> ) {
78
+ if ( changedProperties . has ( "orgList" ) ) {
79
+ this . fuse . setCollection ( this . orgList ?? [ ] ) ;
80
+ }
81
+ }
82
+
83
+ protected firstUpdated ( ) {
84
+ this . fuse . setCollection ( this . orgList ?? [ ] ) ;
85
+ }
86
+
59
87
render ( ) {
60
88
if ( this . skeleton ) {
61
89
return this . renderSkeleton ( ) ;
62
90
}
63
91
92
+ const orgs = this . search
93
+ ? this . fuse . search ( this . search ) . map ( ( { item } ) => item )
94
+ : this . orgList ;
95
+
64
96
return html `
97
+ < sl-input
98
+ value =${ this . search }
99
+ clearable
100
+ size ="small "
101
+ class ="mb-6 "
102
+ placeholder =${ msg (
103
+ "Search all orgs by name, id, slug, users, and subscriptions" ,
104
+ ) }
105
+ @sl-input =${ ( e : Event ) => {
106
+ this . search = ( e . target as SlInput ) . value . trim ( ) || "" ;
107
+ } }
108
+ >
109
+ < sl-icon
110
+ name ="search "
111
+ slot ="prefix "
112
+ aria-hidden ="true "
113
+ library ="default "
114
+ > </ sl-icon
115
+ > </ sl-input >
65
116
< btrix-table >
66
117
< btrix-table-head class ="mb-2 ">
67
118
< btrix-table-header-cell >
@@ -84,7 +135,7 @@ export class OrgsList extends BtrixElement {
84
135
</ btrix-table-header-cell >
85
136
</ btrix-table-head >
86
137
< btrix-table-body class ="rounded border ">
87
- ${ this . orgList ?. map ( this . renderOrg ) }
138
+ ${ orgs ?. map ( this . renderOrg ) }
88
139
</ btrix-table-body >
89
140
</ btrix-table >
90
141
0 commit comments