@@ -33,9 +33,16 @@ export function whereClausesEqual(where1?: WhereClause, where2?: WhereClause): b
33
33
)
34
34
}
35
35
36
- export function filtersToInitialState ( filters : MUIFilter < Record < string , any > > [ ] ) : FiltersState {
36
+ export function filtersAndColumnsToInitialState ( params : {
37
+ columns : MUIColumn < any > [ ]
38
+ filters : MUIFilter < Record < string , any > > [ ]
39
+ } ) : {
40
+ filtersState : FiltersState
41
+ orderByState : OrderByState
42
+ } {
43
+ const { columns, filters } = params
37
44
// remember, see `FiltersState` for instructions how to save the state.
38
- return filters . reduce < FiltersState > ( ( map , f , i ) => {
45
+ const _filtersState = filters . reduce < FiltersState > ( ( map , f , i ) => {
39
46
if ( f . type === 'radio' || f . type === 'select' ) {
40
47
const firstChecked = f . options ?. find ( ( o ) => o . checked )
41
48
if ( firstChecked ) map . set ( i , firstChecked . where )
@@ -55,6 +62,39 @@ export function filtersToInitialState(filters: MUIFilter<Record<string, any>>[])
55
62
}
56
63
return map
57
64
} , new Map ( ) )
65
+
66
+ // maybe we needed to clear other filters, let's do that for the first filter we find that needs it
67
+ const entryThatClearsOtherFilters = [ ..._filtersState . entries ( ) ] . find ( ( [ filterIndex ] ) => {
68
+ const filter = filters [ filterIndex ]
69
+ return filter ?. clearOtherFilters
70
+ } )
71
+ const filtersState = entryThatClearsOtherFilters
72
+ ? new Map ( [ entryThatClearsOtherFilters ] )
73
+ : _filtersState
74
+
75
+ // maybe we needed to clear other orderBy, let's do that for the first filter we find that needs it
76
+ const entryThatClearsOtherOrderBy = [ ...filtersState . entries ( ) ] . find ( ( [ filterIndex ] ) => {
77
+ const filter = filters [ filterIndex ]
78
+ return filter ?. clearOrderBy
79
+ } )
80
+
81
+ const _orderByState = entryThatClearsOtherOrderBy
82
+ ? new Map ( )
83
+ : sort ( columns )
84
+ // sort columns by sortable.position
85
+ . asc ( ( c ) => ( isPlainObject ( c . sortable ) ? c . sortable . position : - 1 ) )
86
+ // then grab each column's sortable.orderBy and save as "direction" in a map
87
+ . reduce < OrderByState > ( ( map , column ) => {
88
+ if ( isPlainObject ( column . sortable ) && column . sortable . orderBy && column . fieldPath ) {
89
+ map . set ( column . fieldPath , column . sortable . orderBy )
90
+ }
91
+ return map
92
+ } , new Map ( ) )
93
+
94
+ const newEntries = getRequiredOrderByBasedOnFilters ( filtersState )
95
+ const orderByState = newEntries . length ? mapUnshift ( _orderByState , ...newEntries ) : _orderByState
96
+
97
+ return { filtersState, orderByState }
58
98
}
59
99
60
100
/**
@@ -98,26 +138,6 @@ export function getRequiredOrderByBasedOnFilters(
98
138
} , [ ] )
99
139
}
100
140
101
- export function columnsToInitialOrderByState (
102
- columns : MUIColumn < any > [ ] ,
103
- initialFilterState : FiltersState
104
- ) : OrderByState {
105
- const orderByState = sort ( columns )
106
- // sort columns by sortable.position
107
- . asc ( ( c ) => ( isPlainObject ( c . sortable ) ? c . sortable . position : - 1 ) )
108
- // then grab each column's sortable.orderBy and save as "direction" in a map
109
- . reduce < OrderByState > ( ( map , column ) => {
110
- if ( isPlainObject ( column . sortable ) && column . sortable . orderBy && column . fieldPath ) {
111
- map . set ( column . fieldPath , column . sortable . orderBy )
112
- }
113
- return map
114
- } , new Map ( ) )
115
-
116
- const newEntries = getRequiredOrderByBasedOnFilters ( initialFilterState )
117
- if ( newEntries . length ) return mapUnshift ( orderByState , ...newEntries )
118
- return orderByState
119
- }
120
-
121
141
/** Clears JavaScript reference pointers */
122
142
export function carbonCopyMap < T extends Map < any , any > > ( map : T ) : T {
123
143
let newMap = new Map ( ) as T
0 commit comments