All files / src/components SearchableSingleSelectFacet.vue

97.05% Statements 33/34
90.47% Branches 19/21
100% Functions 12/12
96.55% Lines 28/29

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                  2x 2x     1281x                                                                                     111x 111x 111x   111x 127x   16x       112x   111x           111x 128x 99x     29x 38x     29x       1x 1x   1x 1x       111x 126x 61x                         65x                     128x                           2x     2x      
<template>
  <HorizontalFacets
    :name="name"
    :options="facetOptions"
    @onCloseFacetDropdown="onDropdownClose"
  />
</template>
 
<script setup>
import HorizontalFacets from '@atomic-ui/horizontalFacets';
import {
  computed, h, ref, watch,
} from 'vue';
import SearchableSingleSelectDropdown from './SearchableSingleSelectDropdown';
 
const props = defineProps({
  modelValue: {
    type: [String, Number],
    default: '',
  },
  name: {
    type: String,
    default: 'searchable-single-select-facet',
  },
  options: {
    type: Array,
    default: () => [],
  },
  placeholder: {
    type: String,
    default: 'Select item',
  },
  searchPlaceholder: {
    type: String,
    default: 'Search...',
  },
  emptyMessage: {
    type: String,
    default: 'No options available',
  },
  noResultsMessage: {
    type: String,
    default: 'No results found',
  },
  closeOnSelect: {
    type: Boolean,
    default: true,
  },
  clearSearchOnClose: {
    type: Boolean,
    default: true,
  },
});
 
const emit = defineEmits(['update:modelValue']);
 
const selectedValue = ref(props.modelValue);
const dropdownRef = ref(null);
const shouldCloseDropdown = ref(false);
 
watch(
  () => props.modelValue,
  (newVal) => {
    selectedValue.value = newVal;
  },
);
 
const hasOptions = computed(() => Boolean(props.options && props.options.length > 0));
 
const normalizedOptions = computed(() => props.options.map((option, index) => ({
  id: `${props.name}-${index}`,
  label: option.displayValue || option.label || option.value || option,
  value: option.value || option,
})));
 
const displayText = computed(() => {
  if (!selectedValue.value) {
    return props.placeholder;
  }
 
  const selectedOption = normalizedOptions.value.find(
    (opt) => opt.value === selectedValue.value,
  );
 
  return selectedOption ? selectedOption.label : props.placeholder;
});
 
function handleSelect(value) {
  selectedValue.value = value;
  emit('update:modelValue', value);
 
  if (props.closeOnSelect) {
    shouldCloseDropdown.value = true;
  }
}
 
const DropdownContent = computed(() => {
  if (!hasOptions.value) {
    return h(
      'div',
      {
        class: 'facet-empty-message padding-2 text-center',
        style: {
          color: '#6c757d',
          fontSize: '14px',
        },
      },
      props.emptyMessage,
    );
  }
 
  return h(SearchableSingleSelectDropdown, {
    ref: dropdownRef,
    modelValue: selectedValue.value,
    'onUpdate:modelValue': handleSelect,
    options: props.options,
    name: props.name,
    searchPlaceholder: props.searchPlaceholder,
    noResultsMessage: props.noResultsMessage,
  });
});
 
const facetOptions = computed(() => [
  {
    id: `${props.name}-facet`,
    contents: {
      text: displayText.value,
    },
    dropdown: {
      customComponent: DropdownContent.value,
    },
    ...(shouldCloseDropdown.value && { forceClose: true }),
  },
]);
 
function onDropdownClose() {
  Iif (props.clearSearchOnClose && dropdownRef.value) {
    dropdownRef.value.searchTerm = '';
  }
  shouldCloseDropdown.value = false;
}
</script>