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 | 3x 3x 57x 57x 57x 59x 2x 176x 57x 70x 58x 12x 12x 1x 42x 70x 4x 4x 4x 57x | <template>
<div class="searchable-single-select-dropdown">
<div class="search-section">
<Search
v-model="searchTerm"
:placeholder="searchPlaceholder"
:data-testid="`${name}-search`"
/>
</div>
<div class="results-section">
<div
v-for="option in filteredOptions"
:key="option.id"
class="dropdown-item"
:class="{ selected: option.value === selectedValue }"
:data-testid="`${name}-option-${option.id}`"
@click="handleSelect(option.value)"
>
{{ option.label }}
</div>
<div
v-if="!hasResults"
class="no-results-message padding-2 text-center"
>
{{ noResultsMessage }}
</div>
</div>
</div>
</template>
<script setup>
import Search from '@atomic-ui/search';
import { computed, ref, watch } from 'vue';
const props = defineProps({
modelValue: {
type: [String, Number],
default: '',
},
options: {
type: Array,
default: () => [],
},
name: {
type: String,
default: 'searchable-single-select',
},
searchPlaceholder: {
type: String,
default: 'Search...',
},
noResultsMessage: {
type: String,
default: 'No results found',
},
});
const emit = defineEmits(['update:modelValue', 'select']);
const selectedValue = ref(props.modelValue);
const searchTerm = ref('');
watch(
() => props.modelValue,
(newVal) => {
selectedValue.value = newVal;
},
);
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 filteredOptions = computed(() => {
// No search term: show all
if (!searchTerm.value) {
return normalizedOptions.value;
}
// Trim and lowercase
const term = searchTerm.value.trim().toLowerCase();
// Whitespace-only: show none
if (!term) {
return [];
}
// Filter by term
return normalizedOptions.value.filter((opt) => String(opt.label).toLowerCase().includes(term));
});
const hasResults = computed(() => filteredOptions.value.length > 0);
function handleSelect(value) {
selectedValue.value = value;
emit('update:modelValue', value);
emit('select', value);
}
// eslint-disable-next-line no-undef
defineExpose({ searchTerm });
</script>
<style lang="scss" scoped>
.searchable-single-select-dropdown {
min-width: 280px;
}
.search-section {
padding: 12px;
border-bottom: 1px solid #e0e0e0;
position: sticky;
top: 0;
background: #fff;
z-index: 10;
:deep(.input-search) {
width: 100%;
}
}
.results-section {
max-height: 300px;
overflow-y: auto;
padding: 4px 0;
}
.dropdown-item {
padding: 10px 16px;
cursor: pointer;
border-bottom: 1px solid #f0f0f0;
transition: background-color 0.2s;
font-size: 14px;
color: #333;
&.selected {
background-color: #e3f2fd;
font-weight: 600;
}
&:hover {
background-color: #f8f9fa;
}
&:last-child {
border-bottom: none;
}
}
.no-results-message {
color: #6c757d;
font-size: 14px;
}
</style>
|