Skip to content

Commit 554a351

Browse files
committed
feat: refactor square meters filter buttons to use dynamic options and improve selection handling
1 parent 12b1217 commit 554a351

File tree

1 file changed

+47
-49
lines changed

1 file changed

+47
-49
lines changed

adminforth/documentation/docs/tutorial/03-Customization/02-customFieldRendering.md

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -495,34 +495,17 @@ Sometimes standard filters are not enough, and you want to make a convenient UI
495495
<p class="font-medium mb-1 dark:text-white">{{ $t('Square meters filter') }}</p>
496496
<div class="flex gap-2">
497497
<button
498-
:class="[
499-
baseBtnClass,
500-
selected === 'small' ? activeBtnClass : inactiveBtnClass
501-
]"
502-
@click="select('small')"
498+
v-for="option in options"
499+
:key="option.value"
503500
type="button"
501+
class="flex gap-1 items-center py-1 px-3 text-sm font-medium rounded-default border focus:outline-none focus:z-10 focus:ring-4"
502+
:class="{
503+
'text-white bg-blue-500 border-blue-500 hover:bg-blue-600 focus:ring-blue-200 dark:focus:ring-blue-800': selected === option.value,
504+
'text-gray-900 bg-white border-gray-300 hover:bg-gray-100 hover:text-blue-500 focus:ring-gray-100 dark:focus:ring-gray-700 dark:bg-gray-800 dark:text-gray-400 dark:border-gray-600 dark:hover:text-white dark:hover:bg-gray-700': selected !== option.value
505+
}"
506+
@click="select(option.value)"
504507
>
505-
{{ $t('Small') }}
506-
</button>
507-
<button
508-
:class="[
509-
baseBtnClass,
510-
selected === 'medium' ? activeBtnClass : inactiveBtnClass
511-
]"
512-
@click="select('medium')"
513-
type="button"
514-
>
515-
{{ $t('Medium') }}
516-
</button>
517-
<button
518-
:class="[
519-
baseBtnClass,
520-
selected === 'large' ? activeBtnClass : inactiveBtnClass
521-
]"
522-
@click="select('large')"
523-
type="button"
524-
>
525-
{{ $t('Large') }}
508+
{{ $t(option.label) }}
526509
</button>
527510
</div>
528511
</div>
@@ -539,29 +522,44 @@ const props = defineProps<{
539522
540523
const selected = ref<string | null>(null);
541524
542-
const baseBtnClass =
543-
'flex gap-1 items-center py-1 px-3 text-sm font-medium rounded-default border focus:outline-none focus:z-10 focus:ring-4';
544-
const activeBtnClass =
545-
'text-white bg-blue-500 border-blue-500 hover:bg-blue-600 focus:ring-blue-200 dark:focus:ring-blue-800';
546-
const inactiveBtnClass =
547-
'text-gray-900 bg-white border-gray-300 hover:bg-gray-100 hover:text-blue-500 focus:ring-gray-100 dark:focus:ring-gray-700 dark:bg-gray-800 dark:text-gray-400 dark:border-gray-600 dark:hover:text-white dark:hover:bg-gray-700';
548-
549-
watch(
550-
() => props.modelValue,
551-
(val) => {
552-
if (!val || val.length === 0) {
553-
selected.value = null;
554-
return;
555-
}
556-
const ops = val.map((v) => `${v.operator}:${v.value}`);
557-
558-
if (ops.includes('lt:25')) selected.value = 'small';
559-
else if (ops.includes('gte:25') && ops.includes('lte:90')) selected.value = 'medium';
560-
else if (ops.includes('gt:90')) selected.value = 'large';
561-
else selected.value = null;
562-
},
563-
{ immediate: true }
564-
);
525+
const options = [
526+
{ value: 'small', label: 'Small' },
527+
{ value: 'medium', label: 'Medium' },
528+
{ value: 'large', label: 'Large' }
529+
];
530+
531+
onMounted(() => {
532+
const val = props.modelValue;
533+
if (!val || val.length === 0) {
534+
selected.value = null;
535+
return;
536+
}
537+
538+
const ops = val.map((v) => `${v.operator}:${v.value}`);
539+
540+
if (ops.includes('lt:25')) selected.value = 'small';
541+
else if (ops.includes('gte:25') && ops.includes('lte:90')) selected.value = 'medium';
542+
else if (ops.includes('gt:90')) selected.value = 'large';
543+
else selected.value = null;
544+
});
545+
546+
watch(selected, (size) => {
547+
if (!size) {
548+
emit('update:modelValue', []);
549+
return;
550+
}
551+
552+
const filters = {
553+
small: [{ operator: 'lt', value: 25 }],
554+
medium: [
555+
{ operator: 'gte', value: 25 },
556+
{ operator: 'lte', value: 90 }
557+
],
558+
large: [{ operator: 'gt', value: 90 }]
559+
};
560+
561+
emit('update:modelValue', filters[size]);
562+
});
565563
566564
function select(size: string) {
567565
selected.value = size;

0 commit comments

Comments
 (0)