Skip to content

Commit 91c61fc

Browse files
committed
improfed category filter
1 parent 8b3a7f3 commit 91c61fc

1 file changed

Lines changed: 46 additions & 30 deletions

File tree

apps/web/app/ui/ag-grid/category-filter.ts

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ export class SelectboxFilter<T> implements IFilterComp {
6363
click: () => this.toggleFilter(option.value),
6464
}
6565
})
66+
const splitOptions = options.length > 15
67+
const list1 = splitOptions ? options.filter((it) => it.active) : null
68+
const list2 = splitOptions ? options.filter((it) => !it.active) : options
6669
return m.fragment({}, [
6770
this.showSearch &&
6871
m(SearchControl, {
@@ -79,13 +82,14 @@ export class SelectboxFilter<T> implements IFilterComp {
7982
this.onFilterChanged()
8083
},
8184
}),
85+
list1 &&
8286
m(SelectControls, {
8387
overflow: false,
8488
options: options.filter((it) => it.active),
8589
}),
8690
m(SelectControls, {
8791
overflow: true,
88-
options: options.filter((it) => !it.active),
92+
options: list2,
8993
}),
9094
])
9195
},
@@ -153,8 +157,7 @@ export class SelectboxFilter<T> implements IFilterComp {
153157
values.set(option.value, option)
154158
})
155159
})
156-
this.options = Array.from(values.values())
157-
.sort((a, b) => String(a.label).localeCompare(String(b.label)))
160+
this.options = Array.from(values.values()).sort((a, b) => String(a.label).localeCompare(String(b.label)))
158161
}
159162

160163
protected extractOptionsFromNode(node: RowNode): SelectFilterOption[] {
@@ -163,15 +166,15 @@ export class SelectboxFilter<T> implements IFilterComp {
163166
return value.map((it) => {
164167
return {
165168
label: humanize(it),
166-
value: it
169+
value: it,
167170
}
168171
})
169172
}
170173
return [
171174
{
172175
label: this.getLabel(node, node.data, value),
173-
value: value
174-
}
176+
value: value,
177+
},
175178
]
176179
}
177180
protected getValue(node: RowNode, data: any) {
@@ -213,15 +216,23 @@ const ConditionControl: m.Component<ConditionControlAttrs> = {
213216
view: ({ attrs: { checked, onchange } }) => [
214217
m('div.form-control', [
215218
m('div.btn-group', [
216-
m('button.btn.btn-xs.flex-1', {
217-
class: !checked ? 'btn-active' : '',
218-
onclick: onchange,
219-
}, 'Any'),
220-
m('button.btn.btn-xs.flex-1', {
221-
class: checked ? 'btn-active' : '',
222-
onclick: onchange,
223-
}, 'All'),
224-
])
219+
m(
220+
'button.btn.btn-xs.flex-1',
221+
{
222+
class: !checked ? 'btn-active' : '',
223+
onclick: onchange,
224+
},
225+
'Any'
226+
),
227+
m(
228+
'button.btn.btn-xs.flex-1',
229+
{
230+
class: checked ? 'btn-active' : '',
231+
onclick: onchange,
232+
},
233+
'All'
234+
),
235+
]),
225236
]),
226237
],
227238
}
@@ -242,28 +253,32 @@ const SearchControl: m.Component<SearchControlAttrs> = {
242253
onchange((e.target as HTMLInputElement).value)
243254
},
244255
}),
245-
m('button.btn.btn-ghost.btn-xs', {
246-
onclick: () => onchange('')
247-
}, [
248-
m.trust(
249-
value
250-
? '<svg class="h-4 w-4" viewBox="0 0 320 512" fill="currentColor" stroke="currentColor"><path d="M310.6 361.4c12.5 12.5 12.5 32.75 0 45.25C304.4 412.9 296.2 416 288 416s-16.38-3.125-22.62-9.375L160 301.3L54.63 406.6C48.38 412.9 40.19 416 32 416S15.63 412.9 9.375 406.6c-12.5-12.5-12.5-32.75 0-45.25l105.4-105.4L9.375 150.6c-12.5-12.5-12.5-32.75 0-45.25s32.75-12.5 45.25 0L160 210.8l105.4-105.4c12.5-12.5 32.75-12.5 45.25 0s12.5 32.75 0 45.25l-105.4 105.4L310.6 361.4z"/></svg>'
251-
: '<svg class="h-4 w-4" viewBox="0 0 24 24" fill="none" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" /></svg>'
252-
),
253-
]),
256+
m(
257+
'button.btn.btn-ghost.btn-xs',
258+
{
259+
onclick: () => onchange(''),
260+
},
261+
[
262+
m.trust(
263+
value
264+
? '<svg class="h-4 w-4" viewBox="0 0 320 512" fill="currentColor" stroke="currentColor"><path d="M310.6 361.4c12.5 12.5 12.5 32.75 0 45.25C304.4 412.9 296.2 416 288 416s-16.38-3.125-22.62-9.375L160 301.3L54.63 406.6C48.38 412.9 40.19 416 32 416S15.63 412.9 9.375 406.6c-12.5-12.5-12.5-32.75 0-45.25l105.4-105.4L9.375 150.6c-12.5-12.5-12.5-32.75 0-45.25s32.75-12.5 45.25 0L160 210.8l105.4-105.4c12.5-12.5 32.75-12.5 45.25 0s12.5 32.75 0 45.25l-105.4 105.4L310.6 361.4z"/></svg>'
265+
: '<svg class="h-4 w-4" viewBox="0 0 24 24" fill="none" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" /></svg>'
266+
),
267+
]
268+
),
254269
]),
255270
]),
256271
],
257272
}
258273

259274
interface SelectControlAttrs {
260275
overflow: boolean
261-
options: Array<{ label: string; icon?: string, active: boolean; click: Function }>
276+
options: Array<{ label: string; icon?: string; active: boolean; click: Function }>
262277
}
263278
const SelectControls: m.Component<SelectControlAttrs, any> = {
264279
view: ({ attrs: { overflow, options } }) => [
265280
m(
266-
`ul.menu.menu-compact.rounded-md.flex-1${ overflow ? '.overflow-y-auto' : ''}`,
281+
`ul.menu.menu-compact.rounded-md.flex-1${overflow ? '.overflow-y-auto' : ''}`,
267282
options?.map((option) => {
268283
return m('li', [
269284
m(
@@ -273,10 +288,11 @@ const SelectControls: m.Component<SelectControlAttrs, any> = {
273288
onclick: option.click,
274289
},
275290
[
276-
option.icon && m('img.w-6.h-6', {
277-
src: option.icon
278-
}),
279-
option.label
291+
option.icon &&
292+
m('img.w-6.h-6', {
293+
src: option.icon,
294+
}),
295+
option.label || '-- empty --',
280296
]
281297
),
282298
])

0 commit comments

Comments
 (0)