|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { DataSourceMeta } from '../../engine' |
| 3 | +import { SelectContent, SelectIcon, SelectItem, SelectItemIndicator, SelectPortal, SelectRoot, SelectTrigger, SelectViewport } from 'reka-ui' |
| 4 | +import { computed } from 'vue' |
| 5 | +
|
| 6 | +const props = defineProps<{ |
| 7 | + sources: DataSourceMeta[] |
| 8 | + placeholder?: string |
| 9 | +}>() |
| 10 | +
|
| 11 | +const model = defineModel<string>() |
| 12 | +
|
| 13 | +/** Fallback icon when a source doesn't declare one. */ |
| 14 | +const FALLBACK_ICON = 'i-ph:database-duotone' |
| 15 | +
|
| 16 | +const active = computed(() => props.sources.find(s => s.id === model.value)) |
| 17 | +
|
| 18 | +function iconOf(source: DataSourceMeta | undefined): string { |
| 19 | + return source?.icon || FALLBACK_ICON |
| 20 | +} |
| 21 | +</script> |
| 22 | + |
| 23 | +<template> |
| 24 | + <SelectRoot v-model="model"> |
| 25 | + <SelectTrigger |
| 26 | + class="text-sm px-2.5 outline-none border border-base rounded bg-base inline-flex gap-2 h-9 min-w-52 max-w-80 transition items-center justify-between data-[disabled]:op50 focus-visible:ring-2 focus-visible:ring-primary-500/40" |
| 27 | + > |
| 28 | + <span class="inline-flex items-center gap-2 min-w-0"> |
| 29 | + <span class="shrink-0 color-active" :class="iconOf(active)" aria-hidden="true" /> |
| 30 | + <span class="truncate font-semibold text-primary">{{ active?.title ?? placeholder ?? 'Data source' }}</span> |
| 31 | + </span> |
| 32 | + <SelectIcon class="op-fade shrink-0"> |
| 33 | + <svg width="1em" height="1em" viewBox="0 0 24 24" aria-hidden="true"> |
| 34 | + <path fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" d="m6 9l6 6l6-6" /> |
| 35 | + </svg> |
| 36 | + </SelectIcon> |
| 37 | + </SelectTrigger> |
| 38 | + <SelectPortal> |
| 39 | + <SelectContent |
| 40 | + position="popper" |
| 41 | + :side-offset="6" |
| 42 | + class="border border-base rounded-lg bg-base min-w-[--reka-select-trigger-width] max-w-100 shadow-lg z-dropdown overflow-hidden" |
| 43 | + > |
| 44 | + <SelectViewport class="p-1"> |
| 45 | + <SelectItem |
| 46 | + v-for="source in sources" |
| 47 | + :key="source.id" |
| 48 | + :value="source.id" |
| 49 | + class="text-sm color-base py-1.5 pl-2 pr-2 outline-none rounded-md flex gap-2 cursor-pointer select-none transition items-start relative data-[highlighted]:bg-active" |
| 50 | + > |
| 51 | + <span class="shrink-0 mt-0.5 color-active" :class="iconOf(source)" aria-hidden="true" /> |
| 52 | + <span class="flex flex-col min-w-0 flex-1"> |
| 53 | + <span class="flex items-center gap-1.5"> |
| 54 | + <span class="truncate font-medium">{{ source.title }}</span> |
| 55 | + <span v-if="source.static" class="shrink-0 text-10px op-fade border border-base rounded px-1">static</span> |
| 56 | + </span> |
| 57 | + <span v-if="source.description" class="text-11px color-faint truncate">{{ source.description }}</span> |
| 58 | + </span> |
| 59 | + <SelectItemIndicator class="color-active inline-flex items-center shrink-0 mt-0.5"> |
| 60 | + <svg width="0.85em" height="0.85em" viewBox="0 0 24 24" aria-hidden="true"> |
| 61 | + <path fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" d="M20 6L9 17l-5-5" /> |
| 62 | + </svg> |
| 63 | + </SelectItemIndicator> |
| 64 | + </SelectItem> |
| 65 | + </SelectViewport> |
| 66 | + </SelectContent> |
| 67 | + </SelectPortal> |
| 68 | + </SelectRoot> |
| 69 | +</template> |
0 commit comments