|
| 1 | +<script setup> |
| 2 | +import { ref, watch, computed, defineProps, defineEmits } from 'vue'; |
| 3 | +
|
| 4 | +const props = defineProps({ |
| 5 | + modelValue: { type: Boolean, default: false }, |
| 6 | + formFields: { type: Array, required: true }, |
| 7 | + location: { type: String, default: 'right' }, |
| 8 | +
|
| 9 | + // Optional: customize the open button text |
| 10 | + buttonLabel: { type: String, default: 'Advanced Search' }, |
| 11 | +
|
| 12 | + // NEW: show summary chips outside the drawer |
| 13 | + showSearchChips: { type: Boolean, default: true }, |
| 14 | +}); |
| 15 | +
|
| 16 | +const emit = defineEmits(['update:modelValue', 'search']); |
| 17 | +
|
| 18 | +const isOpen = ref(props.modelValue); |
| 19 | +
|
| 20 | +// Drawer state |
| 21 | +const searchQuery = ref(''); |
| 22 | +const selectedFields = ref([]); |
| 23 | +
|
| 24 | +// NEW: “applied” search state for chips (so chips reflect what was applied) |
| 25 | +const appliedQuery = ref(''); |
| 26 | +const appliedFields = ref([]); |
| 27 | +
|
| 28 | +// Sync v-model <-> internal drawer state |
| 29 | +watch( |
| 30 | + () => props.modelValue, |
| 31 | + (val) => (isOpen.value = val) |
| 32 | +); |
| 33 | +
|
| 34 | +watch(isOpen, (val) => emit('update:modelValue', val)); |
| 35 | +
|
| 36 | +function openDrawer() { |
| 37 | + isOpen.value = true; |
| 38 | +} |
| 39 | +
|
| 40 | +function closeDrawer() { |
| 41 | + isOpen.value = false; |
| 42 | +} |
| 43 | +
|
| 44 | +function removeField(field) { |
| 45 | + selectedFields.value = selectedFields.value.filter((f) => f !== field); |
| 46 | +} |
| 47 | +
|
| 48 | +function getLabel(value) { |
| 49 | + const match = props.formFields.find((f) => f.value === value); |
| 50 | + return match ? match.label : value; |
| 51 | +} |
| 52 | +
|
| 53 | +/** |
| 54 | + * Build “summary chips” for the applied search: |
| 55 | + * - if fields exist: one chip per field: "Field Label: term" |
| 56 | + * - if no fields but term exists: one chip: "Any field: term" |
| 57 | + * - if term empty but fields exist: chips "Field Label" (field-only filter) |
| 58 | + */ |
| 59 | +const hasAppliedTerm = computed(() => { |
| 60 | + return Boolean((appliedQuery.value ?? '').trim()); |
| 61 | +}); |
| 62 | +
|
| 63 | +const appliedFieldChips = computed(() => { |
| 64 | + return (appliedFields.value || []).map((f) => ({ |
| 65 | + key: `field:${f}`, |
| 66 | + field: f, |
| 67 | + label: getLabel(f), |
| 68 | + })); |
| 69 | +}); |
| 70 | +
|
| 71 | +function emitSearchAndApply() { |
| 72 | + const payload = { |
| 73 | + value: searchQuery.value, |
| 74 | + fields: selectedFields.value, |
| 75 | + }; |
| 76 | +
|
| 77 | + // Update applied state (chips reflect “applied” search, not “in-progress” edits) |
| 78 | + appliedQuery.value = payload.value; |
| 79 | + appliedFields.value = [...payload.fields]; |
| 80 | +
|
| 81 | + emit('search', payload); |
| 82 | + closeDrawer(); |
| 83 | +} |
| 84 | +
|
| 85 | +function removeAppliedTerm() { |
| 86 | + appliedQuery.value = ''; |
| 87 | + searchQuery.value = ''; |
| 88 | +
|
| 89 | + emit('search', { |
| 90 | + value: '', |
| 91 | + fields: appliedFields.value, |
| 92 | + }); |
| 93 | +} |
| 94 | +
|
| 95 | +function removeAppliedField(field) { |
| 96 | + appliedFields.value = appliedFields.value.filter((f) => f !== field); |
| 97 | + selectedFields.value = [...appliedFields.value]; |
| 98 | +
|
| 99 | + emit('search', { |
| 100 | + value: appliedQuery.value, |
| 101 | + fields: appliedFields.value, |
| 102 | + }); |
| 103 | +} |
| 104 | +
|
| 105 | +function clearAllApplied() { |
| 106 | + appliedQuery.value = ''; |
| 107 | + appliedFields.value = []; |
| 108 | + searchQuery.value = ''; |
| 109 | + selectedFields.value = []; |
| 110 | +
|
| 111 | + emit('search', { value: '', fields: [] }); |
| 112 | +} |
| 113 | +</script> |
| 114 | +
|
| 115 | +<template> |
| 116 | + <div class="d-flex align-center flex-wrap ga-2"> |
| 117 | + <!-- Open button --> |
| 118 | + <v-btn |
| 119 | + class="md-ml-2 mt-1" |
| 120 | + prepend-icon="mdi:mdi-magnify" |
| 121 | + variant="text" |
| 122 | + no-caps |
| 123 | + @click="openDrawer" |
| 124 | + > |
| 125 | + {{ buttonLabel }} |
| 126 | + </v-btn> |
| 127 | +
|
| 128 | + <!-- NEW: Applied search chips --> |
| 129 | + <template v-if="showSearchChips"> |
| 130 | + <!-- Search term chip --> |
| 131 | + <v-chip |
| 132 | + v-if="hasAppliedTerm" |
| 133 | + closable |
| 134 | + class="ma-1" |
| 135 | + color="primary" |
| 136 | + variant="outlined" |
| 137 | + @click:close="removeAppliedTerm" |
| 138 | + > |
| 139 | + Search: "{{ appliedQuery }}" |
| 140 | + </v-chip> |
| 141 | +
|
| 142 | + <!-- Field chips --> |
| 143 | + <v-chip |
| 144 | + v-for="chip in appliedFieldChips" |
| 145 | + :key="chip.key" |
| 146 | + closable |
| 147 | + class="ma-1" |
| 148 | + color="primary" |
| 149 | + variant="outlined" |
| 150 | + @click:close="removeAppliedField(chip.field)" |
| 151 | + > |
| 152 | + Field: {{ chip.label }} |
| 153 | + </v-chip> |
| 154 | +
|
| 155 | + <!-- Clear all --> |
| 156 | + <v-btn |
| 157 | + v-if="hasAppliedTerm || appliedFieldChips.length" |
| 158 | + variant="text" |
| 159 | + density="compact" |
| 160 | + class="ml-1" |
| 161 | + @click="clearAllApplied" |
| 162 | + > |
| 163 | + Clear |
| 164 | + </v-btn> |
| 165 | + </template> |
| 166 | + <!-- Drawer --> |
| 167 | + <v-navigation-drawer |
| 168 | + v-model="isOpen" |
| 169 | + :location="location" |
| 170 | + temporary |
| 171 | + width="400" |
| 172 | + > |
| 173 | + <v-card flat> |
| 174 | + <v-card-title class="text-h6 d-flex justify-space-between align-center"> |
| 175 | + Advanced Search |
| 176 | + <v-btn icon="mdi:mdi-close" variant="text" @click="closeDrawer" /> |
| 177 | + </v-card-title> |
| 178 | +
|
| 179 | + <v-divider /> |
| 180 | +
|
| 181 | + <v-card-text> |
| 182 | + <!-- Search Input --> |
| 183 | + <v-text-field |
| 184 | + v-model="searchQuery" |
| 185 | + label="Search term" |
| 186 | + prepend-inner-icon="mdi:mdi-magnify" |
| 187 | + clearable |
| 188 | + class="mb-3" |
| 189 | + /> |
| 190 | +
|
| 191 | + <!-- Field Selection --> |
| 192 | + <v-select |
| 193 | + v-model="selectedFields" |
| 194 | + :items="formFields" |
| 195 | + label="Search in fields" |
| 196 | + multiple |
| 197 | + chips |
| 198 | + item-title="label" |
| 199 | + item-value="value" |
| 200 | + prepend-inner-icon="mdi:mdi-filter-variant" |
| 201 | + variant="outlined" |
| 202 | + class="mb-3" |
| 203 | + :menu-props="{ maxHeight: 300 }" |
| 204 | + /> |
| 205 | +
|
| 206 | + <!-- Active Field Chips (in-drawer selection) --> |
| 207 | + <div class="d-flex flex-wrap"> |
| 208 | + <v-chip |
| 209 | + v-for="field in selectedFields" |
| 210 | + :key="field" |
| 211 | + closable |
| 212 | + class="ma-1" |
| 213 | + color="primary" |
| 214 | + variant="outlined" |
| 215 | + @click:close="removeField(field)" |
| 216 | + > |
| 217 | + {{ getLabel(field) }} |
| 218 | + </v-chip> |
| 219 | + </div> |
| 220 | +
|
| 221 | + <!-- Search Button --> |
| 222 | + <v-btn color="primary" class="mt-4" block @click="emitSearchAndApply"> |
| 223 | + Apply Search |
| 224 | + </v-btn> |
| 225 | + </v-card-text> |
| 226 | + </v-card> |
| 227 | + </v-navigation-drawer> |
| 228 | + </div> |
| 229 | +</template> |
0 commit comments