Lists: an enum filter shows the list's static value as its default #208
No files matched your search
@@ -76,7 +76,7 @@ import {
|
||||
import { isPendingDelete, usePendingDeletes } from '@/lib/pendingDeletes';
|
||||
import { friendlySetError } from '@/lib/jmapErrors';
|
||||
import { coerceLabel } from '@/lib/objectOptions';
|
||||
import { buildJmapFilter } from '@/lib/listFilter';
|
||||
import { buildJmapFilter, staticFilterValue } from '@/lib/listFilter';
|
||||
import { useResetOnChange } from '@/hooks/useBufferedValue';
|
||||
|
||||
import { useSchemaStore } from '@/stores/schemaStore';
|
||||
@@ -973,14 +973,13 @@ export function DynamicList({ viewName }: DynamicListProps) {
|
||||
|
||||
case 'enum': {
|
||||
const enumVariants = schema!.enums[filterDef.enumName] ?? [];
|
||||
const fixed = staticFilterValue(resolved?.list?.filtersStatic, filterDef.field);
|
||||
const allOption = fixed ? [] : [{ value: '__all__', label: t('filters.all', 'All') }];
|
||||
if (enumVariants.length > ENUM_FILTER_COMBOBOX_THRESHOLD) {
|
||||
return wrapper(
|
||||
<Combobox
|
||||
options={[
|
||||
{ value: '__all__', label: t('filters.all', 'All') },
|
||||
...enumVariants.map((v) => ({ value: v.name, label: v.label })),
|
||||
]}
|
||||
value={value || '__all__'}
|
||||
options={[...allOption, ...enumVariants.map((v) => ({ value: v.name, label: v.label }))]}
|
||||
value={value || fixed || '__all__'}
|
||||
onValueChange={(v) => handleFilterSelectChange(filterDef.field, v)}
|
||||
searchPlaceholder={t('common.searchPlaceholder', 'Search...')}
|
||||
emptyText={t('field.noMatches', 'No matches')}
|
||||
@@ -988,12 +987,15 @@ export function DynamicList({ viewName }: DynamicListProps) {
|
||||
);
|
||||
}
|
||||
return wrapper(
|
||||
<Select value={value || '__all__'} onValueChange={(v) => handleFilterSelectChange(filterDef.field, v)}>
|
||||
<Select
|
||||
value={value || fixed || '__all__'}
|
||||
onValueChange={(v) => handleFilterSelectChange(filterDef.field, v)}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder={filterDef.label} />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="__all__">{t('filters.all', 'All')}</SelectItem>
|
||||
{!fixed && <SelectItem value="__all__">{t('filters.all', 'All')}</SelectItem>}
|
||||
{enumVariants.map((v) => (
|
||||
<SelectItem key={v.name} value={v.name}>
|
||||
{v.label}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import type { Filter } from '@/types/schema';
|
||||
import { buildJmapFilter } from './listFilter';
|
||||
import { buildJmapFilter, staticFilterValue } from './listFilter';
|
||||
|
||||
const dateFilter = [{ field: 'created', type: 'date' }] as Filter[];
|
||||
const x = (appliedFilters: Record<string, string>) =>
|
||||
@@ -84,3 +84,19 @@ describe('buildJmapFilter', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('staticFilterValue', () => {
|
||||
it('gives the string a static filter sets, and nothing otherwise', () => {
|
||||
const statics = { event: 'delivery.attempt-start', kind: 'remote', limit: 5 };
|
||||
expect(staticFilterValue(statics, 'kind')).toBe('remote');
|
||||
expect(staticFilterValue(statics, 'limit')).toBeUndefined();
|
||||
expect(staticFilterValue(statics, 'text')).toBeUndefined();
|
||||
expect(staticFilterValue(undefined, 'kind')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('lets the reader override it in the query', () => {
|
||||
expect(
|
||||
buildJmapFilter({ appliedFilters: { kind: 'any' }, filtersStatic: { kind: 'remote' }, isXPrefixed: true }),
|
||||
).toEqual({ kind: 'any' });
|
||||
});
|
||||
});
|
||||
@@ -72,3 +72,13 @@ export function buildJmapFilter({
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* The value a list's static filters already give a field, when it's a plain
|
||||
* string (delivery history's `kind`). An enum filter on that field shows it as
|
||||
* the default and offers no "All", since clearing the filter brings it back.
|
||||
*/
|
||||
export function staticFilterValue(filtersStatic: Record<string, unknown> | undefined, field: string): string | undefined {
|
||||
const value = filtersStatic?.[field];
|
||||
return typeof value === 'string' ? value : undefined;
|
||||
}
|
||||
Reference in new issue
Block a user