All files / src/components MultiSelectFacet.vue

76.31% Statements 29/38
100% Branches 15/15
73.33% Functions 11/15
75.86% Lines 22/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 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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236                  2x 2x 2x     301x                                                                                               28x   28x   5x                 28x 29x   1x       28x   76x             1x 1x                             28x   28x 30x 1x                               29x           29x   29x                                                                                                                                                                         30x                              
<template>
  <HorizontalFacets
    :name="name"
    :options="facetOptions"
    @onCloseFacetDropdown="onDropdownClose"
  />
</template>
 
<script setup>
import Checkbox from '@atomic-ui/checkbox';
import HorizontalFacets from '@atomic-ui/horizontalFacets';
import {
  computed, h, ref, watch,
} from 'vue';
import useMultiSelectDisplay from '../composables/useMultiSelectDisplay';
 
const props = defineProps({
  modelValue: {
    type: Array,
    default: () => [],
  },
  label: {
    type: String,
    default: 'Select Recipe IDs',
  },
  name: {
    type: String,
    default: 'recipe-facet',
  },
  options: {
    type: Array,
    default: () => [],
  },
  placeholder: {
    type: String,
    default: 'Select items',
  },
  emptyMessage: {
    type: String,
    default: 'No options available',
  },
  showCount: {
    type: Boolean,
    default: false,
  },
  displayFormat: {
    type: String,
    default: 'badge',
    validator: (val) => ['badge', 'inline', 'custom'].includes(val),
  },
  maxDisplay: {
    type: Number,
    default: 2,
  },
  showSelectActions: {
    type: Boolean,
    default: false,
  },
});
 
const emit = defineEmits(['update:modelValue']);
 
const selectedValues = ref([...props.modelValue]);
 
const { displayText, displayCount } = useMultiSelectDisplay(
  selectedValues,
  computed(() => props.options),
  {
    placeholder: props.placeholder,
    maxDisplay: props.maxDisplay,
    showCount: props.showCount,
    format: props.displayFormat,
  },
);
 
watch(
  () => props.modelValue,
  (newVal) => {
    selectedValues.value = [...newVal];
  },
);
 
const hasOptions = computed(() => props.options && props.options.length > 0);
 
const checkboxOptions = computed(() => props.options.map((option, index) => ({
  id: `${props.name}-${index}`,
  label: option.displayValue || option.label || option.value || option,
  value: option.value || option,
})));
 
function handleCheckboxChange(newValues) {
  selectedValues.value = newValues;
  emit('update:modelValue', newValues);
}
 
function handleSelectAll() {
  const allValues = checkboxOptions.value.map((opt) => opt.value);
  selectedValues.value = allValues;
  emit('update:modelValue', allValues);
}
 
function handleClearAll() {
  selectedValues.value = [];
  emit('update:modelValue', []);
}
 
// Stop propagation to prevent HorizontalFacets document listener from closing dropdown
const stopPropagation = (e) => e.stopPropagation();
 
const DropdownContent = computed(() => {
  if (!hasOptions.value) {
    return h(
      'div',
      {
        class: 'facet-empty-message',
        onClick: stopPropagation,
        style: {
          padding: '20px',
          textAlign: 'center',
          color: '#6c757d',
          fontSize: '14px',
        },
      },
      props.emptyMessage,
    );
  }
 
  const checkboxElement = h(Checkbox, {
    modelValue: selectedValues.value,
    'onUpdate:modelValue': handleCheckboxChange,
    options: checkboxOptions.value,
  });
 
  if (!props.showSelectActions) {
    // Wrap checkbox in container that stops propagation
    return h(
      'div',
      {
        class: 'facet-checkbox-wrapper',
        onClick: stopPropagation,
      },
      checkboxElement,
    );
  }
 
  const actionButtons = h(
    'div',
    {
      class: 'facet-select-actions',
      style: {
        display: 'flex',
        justifyContent: 'center',
        gap: '24px',
        padding: '10px 12px',
        borderBottom: '1px solid #e0e0e0',
        marginBottom: '8px',
      },
    },
    [
      h(
        'button',
        {
          type: 'button',
          class: 'facet-action-btn',
          'data-testid': 'facet-select-all-btn',
          style: {
            background: 'none',
            border: 'none',
            color: '#0066cc',
            cursor: 'pointer',
            fontSize: '13px',
            fontWeight: '500',
            padding: '4px 8px',
          },
          onClick: handleSelectAll,
        },
        `Select All (${checkboxOptions.value.length})`,
      ),
      h(
        'span',
        {
          style: {
            color: '#e0e0e0',
            fontSize: '13px',
          },
        },
        '|',
      ),
      h(
        'button',
        {
          type: 'button',
          class: 'facet-action-btn',
          'data-testid': 'facet-clear-all-btn',
          style: {
            background: 'none',
            border: 'none',
            color: '#0066cc',
            cursor: 'pointer',
            fontSize: '13px',
            fontWeight: '500',
            padding: '4px 8px',
          },
          onClick: handleClearAll,
        },
        'Clear All',
      ),
    ],
  );
 
  return h(
    'div',
    {
      class: 'facet-dropdown-content',
      onClick: stopPropagation,
    },
    [actionButtons, checkboxElement],
  );
});
 
const facetOptions = computed(() => [
  {
    id: `${props.name}-facet`,
    contents: {
      text: displayText.value,
      ...(displayCount.value && { number: displayCount.value }),
    },
    dropdown: {
      customComponent: DropdownContent.value,
    },
  },
]);
 
function onDropdownClose() {}
</script>