11import fuzzysort from "fuzzysort"
2- import { entries , flatMap , groupBy , map , pipe } from "remeda"
32import { createEffect , createMemo , createResource , on } from "solid-js"
43import { createStore } from "solid-js/store"
54import { createList } from "solid-list"
@@ -17,55 +16,69 @@ export interface FilteredListProps<T> {
1716 noInitialSelection ?: boolean
1817}
1918
19+ export function groupItems < T > (
20+ items : T [ ] ,
21+ props : Pick < FilteredListProps < T > , "groupBy" | "sortBy" | "sortGroupsBy" > ,
22+ ) {
23+ // A plain object keeps the group order of `Object.entries`, where numeric-looking categories sort first.
24+ const byCategory : Record < string , T [ ] > = { }
25+
26+ for ( const item of items ) {
27+ const category = props . groupBy ? props . groupBy ( item ) : ""
28+ byCategory [ category ] ??= [ ]
29+ byCategory [ category ] . push ( item )
30+ }
31+
32+ const groups = Object . entries ( byCategory ) . map ( ( [ category , group ] ) => ( {
33+ category,
34+ items : props . sortBy ? group . sort ( props . sortBy ) : group ,
35+ } ) )
36+
37+ return props . sortGroupsBy ? groups . sort ( props . sortGroupsBy ) : groups
38+ }
39+
2040export function useFilteredList < T > ( props : FilteredListProps < T > ) {
2141 const [ store , setStore ] = createStore < { filter : string } > ( { filter : "" } )
2242
2343 type Group = { category : string ; items : [ T , ...T [ ] ] }
2444
2545 const empty : Group [ ] = [ ]
2646
47+ const search = ( all : T [ ] , needle : string ) => {
48+ const skipFilter = props . skipFilter
49+ const filterable = skipFilter ? all . filter ( ( item ) => ! skipFilter ( item ) ) : all
50+ const skipped = skipFilter ? all . filter ( skipFilter ) : [ ]
51+
52+ // oxlint-disable-next-line anti-slop/no-runtime-typeof -- plain string lists have no keys to search by
53+ const strings = ! props . filterKeys && filterable . every ( ( e ) => typeof e === "string" )
54+
55+ // SAFETY: when `strings` holds, every element is a string, so the matched targets are the items.
56+ const filtered = strings
57+ ? ( fuzzysort . go ( needle , filterable as string [ ] ) . map ( ( x ) => x . target ) as T [ ] )
58+ : fuzzysort . go ( needle , filterable , { keys : props . filterKeys ! } ) . map ( ( x ) => x . obj )
59+
60+ return skipped . length ? [ ...filtered , ...skipped ] : filtered
61+ }
62+
2763 const [ grouped , { refetch } ] = createResource (
2864 ( ) => ( {
2965 filter : store . filter ,
66+ // oxlint-disable-next-line anti-slop/no-runtime-typeof -- `items` is either a list or a loader for one
3067 items : typeof props . items === "function" ? props . items ( store . filter ) : props . items ,
3168 } ) ,
3269 async ( { filter, items } ) => {
3370 const query = filter ?? ""
3471 const needle = query . toLowerCase ( )
3572 const all = ( await Promise . resolve ( items ) ) || [ ]
3673
37- const result = pipe (
38- all ,
39- ( x ) => {
40- if ( ! needle ) return x
41- const skipFilter = props . skipFilter
42- const filterable = skipFilter ? x . filter ( ( item ) => ! skipFilter ( item ) ) : x
43- const skipped = skipFilter ? x . filter ( skipFilter ) : [ ]
44-
45- const filtered =
46- ! props . filterKeys && Array . isArray ( filterable ) && filterable . every ( ( e ) => typeof e === "string" )
47- ? ( fuzzysort . go ( needle , filterable ) . map ( ( x ) => x . target ) as T [ ] )
48- : fuzzysort . go ( needle , filterable , { keys : props . filterKeys ! } ) . map ( ( x ) => x . obj )
49-
50- return skipped . length ? [ ...filtered , ...skipped ] : filtered
51- } ,
52- groupBy ( ( x ) => ( props . groupBy ? props . groupBy ( x ) : "" ) ) ,
53- entries ( ) ,
54- map ( ( [ k , v ] ) => ( { category : k , items : props . sortBy ? v . sort ( props . sortBy ) : v } ) ) ,
55- ( groups ) => ( props . sortGroupsBy ? groups . sort ( props . sortGroupsBy ) : groups ) ,
56- )
57-
58- return result
74+ const matched = needle ? search ( all , needle ) : all
75+
76+ return groupItems ( matched , props )
5977 } ,
6078 { initialValue : empty } ,
6179 )
6280
63- const flat = createMemo ( ( ) => {
64- return pipe (
65- grouped . latest || [ ] ,
66- flatMap ( ( x ) => x . items ) ,
67- )
68- } )
81+ const flat = createMemo ( ( ) => ( grouped . latest || [ ] ) . flatMap ( ( x ) => x . items ) )
6982
7083 function initialActive ( ) {
7184 if ( props . noInitialSelection ) return ""
0 commit comments