|
| 1 | +<template> |
| 2 | + <div class="item-filter" :class="`item-filter-${label.toLowerCase().replace(/\s+/g, '-')}`"> |
| 3 | + <oc-filter-chip |
| 4 | + :filter-label="label" |
| 5 | + :selected-item-names="selectedItemNames" |
| 6 | + :close-on-click="!allowCustom" |
| 7 | + @clear-filter="clear" |
| 8 | + @show-drop="onShowDrop" |
| 9 | + > |
| 10 | + <div v-if="allowCustom" class="filter-select-custom"> |
| 11 | + <input |
| 12 | + :id="`filter-custom-${label.toLowerCase().replace(/\s+/g, '-')}`" |
| 13 | + ref="customInput" |
| 14 | + type="text" |
| 15 | + class="filter-select-custom-input" |
| 16 | + :value="customText" |
| 17 | + :placeholder="customPlaceholder || label" |
| 18 | + :aria-label="customPlaceholder || label" |
| 19 | + @input="customText = ($event.target as HTMLInputElement).value" |
| 20 | + @keyup.enter="applyCustom" |
| 21 | + @click.stop |
| 22 | + /> |
| 23 | + <button |
| 24 | + v-if="customText" |
| 25 | + type="button" |
| 26 | + class="oc-button oc-rounded oc-button-s oc-button-justify-content-center oc-button-gap-m oc-button-primary oc-button-primary-filled filter-select-custom-apply" |
| 27 | + @click.stop="applyCustom" |
| 28 | + > |
| 29 | + <span>OK</span> |
| 30 | + </button> |
| 31 | + </div> |
| 32 | + <oc-list class="item-filter-list"> |
| 33 | + <li v-for="opt in filteredOptions" :key="String(opt.value)" class="oc-my-xs"> |
| 34 | + <oc-button |
| 35 | + class="item-filter-list-item oc-flex oc-flex-middle oc-width-1-1 oc-p-xs oc-flex-between" |
| 36 | + :class="{ 'item-filter-list-item-active': String(opt.value) === String(modelValue) }" |
| 37 | + justify-content="space-between" |
| 38 | + appearance="raw" |
| 39 | + @click="select(opt.value)" |
| 40 | + > |
| 41 | + <div class="oc-flex oc-flex-middle oc-text-truncate"> |
| 42 | + <oc-checkbox |
| 43 | + size="large" |
| 44 | + class="item-filter-checkbox oc-mr-s" |
| 45 | + :label="opt.label" |
| 46 | + :model-value="String(opt.value) === String(modelValue)" |
| 47 | + :label-hidden="true" |
| 48 | + @update:model-value="select(opt.value)" |
| 49 | + @click.stop |
| 50 | + /> |
| 51 | + <div v-if="opt.icon"> |
| 52 | + <oc-icon :name="opt.icon" :color="opt.iconColor" size="large" /> |
| 53 | + </div> |
| 54 | + <div class="oc-text-truncate oc-ml-s"> |
| 55 | + <span>{{ opt.label }}</span> |
| 56 | + </div> |
| 57 | + </div> |
| 58 | + <div class="oc-flex"> |
| 59 | + <oc-icon |
| 60 | + v-if="String(opt.value) === String(modelValue)" |
| 61 | + name="check" |
| 62 | + /> |
| 63 | + </div> |
| 64 | + </oc-button> |
| 65 | + </li> |
| 66 | + </oc-list> |
| 67 | + </oc-filter-chip> |
| 68 | + </div> |
| 69 | +</template> |
| 70 | + |
| 71 | +<script setup lang="ts"> |
| 72 | +import { ref, computed, nextTick } from 'vue' |
| 73 | +
|
| 74 | +const props = withDefaults(defineProps<{ |
| 75 | + modelValue: string | number |
| 76 | + options: Array<{ value: string | number; label: string; icon?: string; iconColor?: string }> |
| 77 | + label: string |
| 78 | + defaultValue?: string | number |
| 79 | + ariaLabel?: string |
| 80 | + allowCustom?: boolean |
| 81 | + customPlaceholder?: string |
| 82 | +}>(), { |
| 83 | + defaultValue: undefined, |
| 84 | + ariaLabel: undefined, |
| 85 | + allowCustom: false, |
| 86 | + customPlaceholder: undefined, |
| 87 | +}) |
| 88 | +
|
| 89 | +const emit = defineEmits<{ |
| 90 | + (e: 'update:modelValue', value: string | number): void |
| 91 | +}>() |
| 92 | +
|
| 93 | +const customInput = ref<HTMLInputElement>() |
| 94 | +const customText = ref('') |
| 95 | +
|
| 96 | +const selectedItemNames = computed(() => { |
| 97 | + if (props.defaultValue === undefined) return [] |
| 98 | + if (String(props.modelValue) === String(props.defaultValue)) return [] |
| 99 | + const selected = props.options.find(o => String(o.value) === String(props.modelValue)) |
| 100 | + if (selected) return [selected.label] |
| 101 | + if (props.modelValue) return [String(props.modelValue)] |
| 102 | + return [] |
| 103 | +}) |
| 104 | +
|
| 105 | +const filteredOptions = computed(() => { |
| 106 | + if (!props.allowCustom || !customText.value) return props.options |
| 107 | + const search = customText.value.toLowerCase() |
| 108 | + return props.options.filter(o => String(o.label).toLowerCase().includes(search)) |
| 109 | +}) |
| 110 | +
|
| 111 | +function select(value: string | number) { |
| 112 | + emit('update:modelValue', value) |
| 113 | + customText.value = '' |
| 114 | +} |
| 115 | +
|
| 116 | +function applyCustom() { |
| 117 | + if (customText.value.trim()) { |
| 118 | + emit('update:modelValue', customText.value.trim()) |
| 119 | + customText.value = '' |
| 120 | + } |
| 121 | +} |
| 122 | +
|
| 123 | +function clear() { |
| 124 | + if (props.defaultValue !== undefined) { |
| 125 | + emit('update:modelValue', props.defaultValue) |
| 126 | + } |
| 127 | + customText.value = '' |
| 128 | +} |
| 129 | +
|
| 130 | +function onShowDrop() { |
| 131 | + customText.value = '' |
| 132 | + if (props.allowCustom) { |
| 133 | + nextTick(() => customInput.value?.focus()) |
| 134 | + } |
| 135 | +} |
| 136 | +</script> |
| 137 | + |
| 138 | +<style lang="scss"> |
| 139 | +.item-filter { |
| 140 | + &-list { |
| 141 | + li { |
| 142 | + &:first-child { |
| 143 | + margin-top: 0 !important; |
| 144 | + } |
| 145 | + &:last-child { |
| 146 | + margin-bottom: 0 !important; |
| 147 | + } |
| 148 | + } |
| 149 | +
|
| 150 | + &-item { |
| 151 | + line-height: 1.5; |
| 152 | + gap: 8px; |
| 153 | +
|
| 154 | + &:hover, |
| 155 | + &-active { |
| 156 | + background-color: var(--oc-color-background-hover) !important; |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | +} |
| 161 | +
|
| 162 | +.filter-select-custom { |
| 163 | + display: flex; |
| 164 | + gap: 4px; |
| 165 | + padding: 0 4px 8px; |
| 166 | + border-bottom: 1px solid var(--oc-color-border, #e0e0e0); |
| 167 | + margin-bottom: 4px; |
| 168 | +} |
| 169 | +
|
| 170 | +.filter-select-custom-input { |
| 171 | + flex: 1; |
| 172 | + padding: 6px 10px; |
| 173 | + border: 1px solid var(--oc-color-border, #ccc); |
| 174 | + border-radius: 4px; |
| 175 | + font-size: var(--oc-font-size-xsmall, 0.75rem); |
| 176 | + font-family: inherit; |
| 177 | + background: var(--oc-color-background-default, #fff); |
| 178 | + color: var(--oc-color-text-default, #333); |
| 179 | + outline: none; |
| 180 | +} |
| 181 | +
|
| 182 | +.filter-select-custom-input:focus { |
| 183 | + border-color: var(--oc-color-swatch-primary-default, #0070c0); |
| 184 | +} |
| 185 | +
|
| 186 | +.filter-select-custom-apply { |
| 187 | + flex-shrink: 0; |
| 188 | +} |
| 189 | +</style> |
0 commit comments