All files / src/components SortControl.vue

100% Statements 6/6
100% Branches 12/12
100% Functions 3/3
100% Lines 5/5

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  327x                 11x   658x                             7x                                                                                             281x                                                      
<template>
  <div class="sort-control">
    <select
      :id="selectId"
      class="form-select sort-control-select"
      name="sort"
      :aria-label="label"
      :title="disabled ? disabledTitle : undefined"
      :value="fieldValue"
      :disabled="disabled"
      @change="$emit('update:fieldValue', ($event.target as HTMLSelectElement).value)"
    >
      <option
        v-for="option in options"
        :key="option.value"
        :value="option.value"
        :disabled="option.disabled"
      >
        {{ option.label }}
      </option>
    </select>
    <button
      v-if="showDirection"
      type="button"
      class="btn btn-outline-secondary sort-control-direction-btn"
      :disabled="directionDisabled"
      :title="direction === 'asc' ? t('common.sortAscending') : t('common.sortDescending')"
      @click="$emit('toggle-direction')"
    >
      <font-awesome-icon
        :icon="direction === 'asc' ? 'fa-solid fa-sort-up' : 'fa-solid fa-sort-down'"
      />
    </button>
  </div>
</template>
 
<script setup lang="ts">
import { useUILanguage } from '@/composables/useUILanguage'
 
export interface SortOption {
  value: string
  label: string
  disabled?: boolean
}
 
withDefaults(
  defineProps<{
    options: SortOption[]
    fieldValue: string
    direction: 'asc' | 'desc'
    // Accessible label for the field <select> (rendered as its aria-label).
    label: string
    directionDisabled?: boolean
    // Off for orders that only go one way (the Store's "Most liked")
    showDirection?: boolean
    // The whole control, with why, as a tooltip
    disabled?: boolean
    disabledTitle?: string
    selectId?: string
  }>(),
  {
    directionDisabled: false,
    showDirection: true,
    disabled: false,
    disabledTitle: undefined,
    selectId: undefined
  }
)
 
defineEmits<{
  'update:fieldValue': [value: string]
  'toggle-direction': []
}>()
 
const { t } = useUILanguage()
</script>
 
<style scoped>
.sort-control {
  display: flex;
  align-items: stretch;
  gap: 0.5rem;
  flex: 0 0 auto;
}
 
.sort-control-select,
.sort-control-direction-btn {
  height: 42px;
}
 
.sort-control-select {
  width: auto;
}
 
/* Match the <select>'s border: btn-outline-secondary's own border is lighter
   (dark theme) or darker (light theme) than form controls. Hover/focus/active
   keep the button's own highlight. */
.sort-control-direction-btn:not(:hover):not(:focus):not(:active) {
  border-color: var(--bw-border);
}
</style>