All files / src/components SearchFilterHeader.vue

100% Statements 38/38
100% Branches 29/29
100% Functions 9/9
100% Lines 30/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214                  10x 10x 10x 10x                                                                                                                                                     295x                       295x       295x     15x 15x       2x         295x 96x 96x     59x     96x   90x 96x   96x 158x 158x 158x 157x 156x 154x 84x 5x       295x 264x   295x 242x                                                                                                                                        
<template>
  <!-- In server mode the caller wraps this component in its own container-fluid
       alongside other row siblings (see ApiKeysPage/CatalogPage/EntitiesPage/
       EntityItemsPage), so no wrapper is added here to avoid nesting containers.
       In client mode this component also owns the list markup (via the default
       slot below), which must NOT be inside a Bootstrap container (to match
       VirtualCardGrid's own full-bleed wrapper) — so it provides its own
       container-fluid just for the search row instead. -->
  <div :class="{ 'container-fluid': mode === 'client' }" v-bind="$attrs">
    <div class="row mb-3">
      <div class="col-12">
        <div class="search-filter-row">
          <div class="search-filter-group">
            <span class="search-filter-icon" aria-hidden="true">
              <font-awesome-icon icon="fa-solid fa-search" class="search-filter-icon-glyph" />
            </span>
            <input
              :value="modelValue"
              type="text"
              name="search"
              autocomplete="off"
              class="form-control search-filter-input"
              :placeholder="placeholder"
              @input="onInput"
            />
            <button
              v-if="modelValue"
              class="btn btn-outline-secondary search-filter-clear"
              type="button"
              @click="clearSearch"
            >
              {{ t('common.clear') }}
            </button>
          </div>
          <slot name="extra" />
        </div>
        <small v-if="showCounts" class="text-muted d-block mt-2">
          {{
            t('common.filteredResults')
              .replace('{filtered}', String(displayFilteredCount))
              .replace('{total}', String(displayTotalCount))
          }}
        </small>
      </div>
    </div>
  </div>
 
  <!-- Client mode: the caller's list markup lives in this slot so it receives
       the filtered/sorted items on the very same render pass — no round trip
       through an emitted event and a parent ref, which would lag one tick
       behind (the list would briefly/incorrectly render as empty). -->
  <slot
    v-if="mode === 'client'"
    :filtered-items="clientFilteredItems"
    :filtered-count="displayFilteredCount"
    :total-count="displayTotalCount"
  />
</template>
 
<script setup lang="ts">
import { computed } from 'vue'
import { useUILanguage } from '@/composables/useUILanguage'
 
// Two root nodes in client mode (the search row and the list slot): attributes
// such as the walkthrough's data-tour go on the search row, never lost
defineOptions({ inheritAttrs: false })
 
interface Props {
  modelValue: string
  placeholder: string
  // 'server' (default): search/sort are applied externally (e.g. an API call) —
  // filteredCount/totalCount are supplied by the parent. 'client': this component
  // filters/sorts `items` itself in memory and hands the result to the default
  // slot (see #extra for the sort control, and the slot below for the list).
  mode?: 'server' | 'client'
  filteredCount?: number
  totalCount?: number
  // False for a list whose total is not known without reading it all (the Store)
  showCounts?: boolean
  // Client mode only
  items?: any[]
  searchFields?: ((item: any) => string)[]
  sortValue?: string
  sortDirection?: 'asc' | 'desc'
  getSortValue?: (item: any, field: string) => string | number | null | undefined
}
 
const props = withDefaults(defineProps<Props>(), {
  mode: 'server',
  filteredCount: 0,
  totalCount: 0,
  showCounts: true,
  items: () => [],
  searchFields: () => [],
  sortValue: undefined,
  sortDirection: 'asc',
  getSortValue: undefined
})
 
const emit = defineEmits<{
  'update:modelValue': [value: string]
}>()
 
const { t } = useUILanguage()
 
function onInput(event: Event) {
  const target = event.target as HTMLInputElement
  emit('update:modelValue', target.value)
}
 
function clearSearch() {
  emit('update:modelValue', '')
}
 
// Only ever read while mode is 'client' (see the template's slot and
// displayFilteredCount below), so no guard is needed for the other mode.
const clientFilteredItems = computed(() => {
  const query = props.modelValue.trim().toLowerCase()
  const filtered = !query
    ? props.items
    : props.items.filter((item) =>
        props.searchFields.some((getText) => getText(item).toLowerCase().includes(query))
      )
 
  if (!props.sortValue) return filtered
 
  const direction = props.sortDirection === 'desc' ? -1 : 1
  const getValue = props.getSortValue || ((item: any, field: string) => item[field])
 
  return [...filtered].sort((a, b) => {
    const valueA = getValue(a, props.sortValue!)
    const valueB = getValue(b, props.sortValue!)
    if (valueA == null && valueB == null) return 0
    if (valueA == null) return 1
    if (valueB == null) return -1
    if (valueA < valueB) return -1 * direction
    if (valueA > valueB) return 1 * direction
    return 0
  })
})
 
const displayFilteredCount = computed(() =>
  props.mode === 'client' ? clientFilteredItems.value.length : props.filteredCount
)
const displayTotalCount = computed(() =>
  props.mode === 'client' ? props.items.length : props.totalCount
)
</script>
 
<style scoped>
.search-filter-row {
  display: flex;
  align-items: stretch;
  gap: 0.5rem;
  width: 100%;
}
 
.search-filter-group {
  display: flex;
  align-items: stretch;
  flex: 1 1 auto;
  min-width: 0;
}
 
.search-filter-icon,
.search-filter-input,
.search-filter-clear {
  height: 42px;
}
 
.search-filter-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  border: 1px solid var(--bw-border);
  border-right: 0;
  border-radius: 0.375rem 0 0 0.375rem;
  background: var(--bw-surface-muted);
  color: var(--bw-text-muted);
}
 
.search-filter-input {
  border-radius: 0;
  border-left: 0;
  min-width: 0;
  background-color: var(--bw-surface);
  color: var(--bw-text);
  border-color: var(--bw-border);
}
 
.search-filter-input:focus {
  z-index: 2;
}
 
.search-filter-clear {
  border-radius: 0 0.375rem 0.375rem 0;
}
 
.search-filter-clear,
.search-filter-icon {
  display: flex;
  align-items: center;
}
 
.search-filter-icon-glyph {
  font-size: 0.95rem;
}
 
[data-bs-theme='dark'] .search-filter-clear {
  border-color: var(--bw-border);
}
</style>