Line data Source code
1 16 : <script setup lang="ts">
2 : import { computed, ref, useTemplateRef } from 'vue'
3 : import { useI18n } from 'vue-i18n'
4 :
5 : import { cn } from '@comfyorg/tailwind-utils'
6 :
7 : import Loader from '@/components/loader/Loader.vue'
8 : import { WidgetInputBaseClass } from '@/renderer/extensions/vueNodes/widgets/components/layout'
9 : import type { FormDropdownInputProps } from './types'
10 :
11 : const {
12 : inputId,
13 : isOpen,
14 : placeholder = 'Select...',
15 : selectedItems,
16 : maxSelectable,
17 : uploadable,
18 : disabled,
19 : accept
20 : } = defineProps<FormDropdownInputProps>()
21 :
22 847 : const { t } = useI18n()
23 :
24 197 : const emit = defineEmits<{
25 : (e: 'select-click', event: MouseEvent): void
26 : (e: 'file-change', event: Event): void
27 : }>()
28 :
29 197 : const theButtonStyle = computed(() =>
30 : cn(
31 : 'border-0 bg-input text-muted-foreground outline-none',
32 : disabled ? 'cursor-not-allowed' : 'cursor-pointer hover:bg-muted',
33 : selectedItems.length > 0 && 'text-foreground'
34 : )
35 : )
36 :
37 197 : const buttonRef = ref<HTMLButtonElement>()
38 197 : const fileInputRef = useTemplateRef<HTMLInputElement>('fileInputRef')
39 :
40 17 : function focus() {
41 17 : buttonRef.value?.focus()
42 : }
43 :
44 : /**
45 : * Open the native file picker without a user click on the input itself.
46 : * Must be invoked synchronously from a user-initiated event handler so the
47 : * browser's transient activation requirement is satisfied. Falls back to
48 : * `click()` on browsers that predate showPicker (Chrome <99, Firefox <101,
49 : * Safari <16).
50 : */
51 0 : function showPicker() {
52 0 : const input = fileInputRef.value!
53 0 : if (typeof input.showPicker === 'function') {
54 0 : input.showPicker()
55 : } else {
56 0 : input.click()
57 : }
58 : }
59 :
60 197 : defineExpose({ focus, showPicker })
61 197 : </script>
62 :
63 : <template>
64 803 : <div
65 : :class="
66 : cn(WidgetInputBaseClass, 'flex text-xs leading-none', {
67 : 'cursor-not-allowed opacity-50 outline-node-border': disabled
68 : })
69 : "
70 : >
71 : <button
72 : :id="inputId"
73 : ref="buttonRef"
74 : :class="
75 : cn(
76 : theButtonStyle,
77 : 'flex h-8 min-w-0 flex-1 items-center justify-between gap-1 px-2',
78 : {
79 : 'rounded-l-lg': uploadable,
80 : 'rounded-lg': !uploadable
81 : }
82 : )
83 : "
84 : @click="emit('select-click', $event)"
85 : >
86 : <span class="min-w-0 flex-1 truncate py-2 text-left">
87 : <span v-if="!selectedItems.length">
88 : {{ placeholder }}
89 : </span>
90 : <span v-else>
91 : {{ selectedItems.map((item) => item.label ?? item.name).join(', ') }}
92 : </span>
93 : </span>
94 : <i
95 : class="icon-[lucide--chevron-down]"
96 : :class="
97 : cn(
98 : 'text-muted-foreground transition-transform duration-200',
99 : isOpen && 'rotate-180'
100 : )
101 : "
102 : />
103 : </button>
104 : <slot name="anchor" :element="buttonRef" />
105 : <label
106 : v-if="uploadable"
107 : :class="
108 : cn(
109 : theButtonStyle,
110 : 'relative',
111 : 'flex size-8 items-center justify-center rounded-r-lg border-l border-node-border'
112 : )
113 : "
114 : >
115 : <Loader v-if="isUploading" size="sm" />
116 : <i v-else class="icon-[lucide--folder-search]" aria-hidden="true" />
117 : <input
118 : ref="fileInputRef"
119 : type="file"
120 : class="absolute inset-0 -z-1 opacity-0"
121 : :aria-label="t('g.upload')"
122 : :multiple="maxSelectable > 1"
123 : :disabled="disabled"
124 : :accept="accept"
125 : @change="emit('file-change', $event)"
126 : />
127 : </label>
128 : </div>
129 : </template>
|