Skip to content

Commit 9a5d34c

Browse files
committed
fix: add option to turn enum and foreign resource filter into singleselect
1 parent cf08c59 commit 9a5d34c

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

adminforth/modules/configValidator.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,18 @@ export default class ConfigValidator implements IConfigValidator {
664664
}
665665
}
666666

667+
if (col.multiselectFilter !== undefined) {
668+
if (typeof col.multiselectFilter !== 'boolean') {
669+
errors.push(`Resource "${res.resourceId}" column "${col.name}" has multiselectFilter that is not boolean`);
670+
}
671+
672+
if (!col.enum && !col.foreignResource) {
673+
errors.push(`Resource "${res.resourceId}" column "${col.name}" multiselectFilter should be set only for enum or foreign resource columns`);
674+
}
675+
} else if (col.enum || col.foreignResource) {
676+
col.multiselectFilter = true;
677+
}
678+
667679
if (inCol.inputPrefix || inCol.inputSuffix) {
668680
if (![AdminForthDataTypes.DECIMAL, AdminForthDataTypes.FLOAT, AdminForthDataTypes.INTEGER, AdminForthDataTypes.STRING, undefined].includes(col.type)) {
669681
if (inCol.type === AdminForthDataTypes.JSON) {

adminforth/spa/src/components/Filters.vue

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424

2525
<Select
2626
v-if="c.foreignResource"
27-
multiple
27+
:multiple="c.multiselectFilter"
2828
class="w-full"
2929
:options="columnOptions[c.name] || []"
30-
@update:modelValue="onFilterInput[c.name]({ column: c, operator: 'in', value: $event.length ? $event : undefined })"
31-
:modelValue="filtersStore.filters.find(f => f.field === c.name && f.operator === 'in')?.value || []"
30+
@update:modelValue="onFilterInput[c.name]({ column: c, operator: c.multiselectFilter ? 'in' : 'eq', value: c.multiselectFilter ? ($event.length ? $event : undefined) : $event || undefined })"
31+
:modelValue="filtersStore.filters.find(f => f.field === c.name && f.operator === (c.multiselectFilter ? 'in' : 'eq'))?.value || []"
3232
/>
3333
<Select
3434
multiple
@@ -44,13 +44,13 @@
4444
:modelValue="filtersStore.filters.find(f => f.field === c.name && f.operator === 'in')?.value || []"
4545
/>
4646

47-
<Select
48-
multiple
47+
<Select
48+
:multiple="c.multiselectFilter"
4949
class="w-full"
5050
v-else-if="c.enum"
5151
:options="c.enum"
52-
@update:modelValue="onFilterInput[c.name]({ column: c, operator: 'in', value: $event.length ? $event : undefined })"
53-
:modelValue="filtersStore.filters.find(f => f.field === c.name && f.operator === 'in')?.value || []"
52+
@update:modelValue="onFilterInput[c.name]({ column: c, operator: c.multiselectFilter ? 'in' : 'eq', value: c.multiselectFilter ? ($event.length ? $event : undefined) : $event || undefined })"
53+
:modelValue="filtersStore.filters.find(f => f.field === c.name && f.operator === (c.multiselectFilter ? 'in' : 'eq'))?.value || []"
5454
/>
5555

5656
<Input
@@ -118,7 +118,8 @@
118118
</template>
119119

120120
<script setup>
121-
import { watch, computed } from 'vue'
121+
import { watch, computed } from 'vue';
122+
import { useI18n } from 'vue-i18n';
122123
import CustomDateRangePicker from '@/components/CustomDateRangePicker.vue';
123124
import { callAdminForthApi } from '@/utils';
124125
import { useRouter } from 'vue-router';
@@ -130,6 +131,7 @@ import Select from '@/afcl/Select.vue';
130131
import debounce from 'debounce';
131132
132133
const filtersStore = useFiltersStore();
134+
const { t } = useI18n();
133135
134136
135137
// props: columns
@@ -163,6 +165,12 @@ const columnOptions = computedAsync(async () => {
163165
},
164166
});
165167
ret[column.name] = list.items;
168+
if (!column.multiselectFilter) {
169+
ret[column.name].push({
170+
label: t('Unset'),
171+
value: '',
172+
});
173+
}
166174
}
167175
})
168176
);

adminforth/types/Common.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,11 @@ export interface AdminForthResourceColumnInputCommon {
803803
*/
804804
foreignResource?: AdminForthForeignResourceCommon,
805805

806+
/**
807+
* Boolean value that determines what select input type to display on filter page.
808+
*/
809+
multiselectFilter?: boolean,
810+
806811
sortable?: boolean,
807812

808813

0 commit comments

Comments
 (0)