| :placeholder="placeholder" |
| 9 | :disabled="disabled" |
| 10 | @update:model-value="$emit('update:modelValue', $event)" |
| 11 | /> |
| 12 | <!-- number --> |
| 13 | <el-input |
| 14 | v-else-if="type === 'number'" |
| 15 | :model-value="modelValue != null ? String(modelValue) : ''" |
| 16 | :placeholder="placeholder" |
| 17 | :disabled="disabled" |
| 18 | @update:model-value="handleNumberInput($event)" |
| 19 | /> |
| 20 | <!-- switch --> |
| 21 | <div v-else-if="type === 'switch'" class="config-field-switch-wrap"> |
| 22 | <el-switch |
| 23 | :model-value="modelValue" |
| 24 | :disabled="disabled" |
| 25 | size="small" |
| 26 | @update:model-value="$emit('update:modelValue', $event)" |
| 27 | /> |
| 28 | <span v-if="tip" class="config-field-switch-tip">{{ tip }}</span> |
| 29 | </div> |
| 30 | <!-- select --> |
| 31 | <PopoverMenu |
| 32 | v-else-if="type === 'select'" |
| 33 | :model-value="modelValue" |
| 34 | :display-value="selectDisplayValue" |
| 35 | :disabled="disabled" |
| 36 | :width="selectWidth" |
| 37 | selectable |
| 38 | full-width |
| 39 | filterable |
| 40 | :filter-placeholder="placeholder || 'Select...'" |
| 41 | @update:model-value="$emit('update:modelValue', $event)" |
| 42 | > |
| 43 | <template #default="{ filterText }"> |
| 44 | <PopoverMenuItem |
| 45 | v-for="opt in filteredOptions(filterText)" |
| 46 | :key="opt.value" |
| 47 | :value="opt.value" |
| 48 | > |
| 49 | {{ opt.label }} |
| 50 | </PopoverMenuItem> |
| 51 | </template> |
| 52 | </PopoverMenu> |
| 53 | <!-- password --> |
| 54 | <el-input |
| 55 | v-else-if="type === 'password'" |
| 56 | :model-value="modelValue" |
| 57 | :placeholder="placeholder" |
| 58 | :disabled="disabled" |
| 59 | type="password" |
| 60 | show-password |
| 61 | @update:model-value="$emit('update:modelValue', $event)" |
| 62 | /> |
| 63 | <!-- kv --> |
| 64 | <KeyValueEditor |
| 65 | v-else-if="type === 'kv'" |
| 66 | :model-value="modelValue" |
| 67 | :key-placeholder="keyPlaceholder" |
| 68 | :value-placeholder="valuePlaceholder" |
| 69 | @update:model-value="$emit('update:modelValue', $event)" |
| 70 | /> |
| 71 | <!-- tags (string array) --> |
| 72 | <StringListEditor |
| 73 | v-else-if="type === 'tags'" |
| 74 | :model-value="modelValue || []" |
| 75 | :placeholder="placeholder" |
| 76 | @update:model-value="$emit('update:modelValue', $event)" |
| 77 | /> |
| 78 | <div v-if="tip && type !== 'switch'" class="config-field-tip">{{ tip }}</div> |
| 79 | </el-form-item> |
| 80 |
| 81 | <!-- Readonly mode: plain display --> |
| 82 | <div v-else class="config-field-readonly" :class="($attrs.class as string)"> |
| 83 | <div class="config-field-label">{{ label }}</div> |
| 84 | <!-- switch readonly --> |
| 85 | <el-switch |
| 86 | v-if="type === 'switch'" |
| 87 | :model-value="modelValue" |
| 88 | disabled |
| 89 | size="small" |
| 90 | /> |
| 91 | <!-- kv readonly --> |
| 92 | <KeyValueEditor |
| 93 | v-else-if="type === 'kv'" |
| 94 | :model-value="modelValue || []" |
| 95 | :key-placeholder="keyPlaceholder" |
| 96 | :value-placeholder="valuePlaceholder" |
| 97 | readonly |
| 98 | /> |
| 99 | <!-- tags readonly --> |
| 100 | <StringListEditor |
| 101 | v-else-if="type === 'tags'" |
| 102 | :model-value="modelValue || []" |
| 103 | readonly |
| 104 | /> |
| 105 | <!-- text/number/select/password readonly --> |
| 106 | <el-input |
| 107 | v-else |
| 108 | :model-value="displayValue" |
| 109 | disabled |
| 110 | /> |
| 111 | </div> |
| 112 | </template> |
| 113 |
| 114 | <script setup lang="ts"> |
| 115 | import { computed } from 'vue' |
| 116 | import KeyValueEditor from './KeyValueEditor.vue' |
| 117 | import StringListEditor from './StringListEditor.vue' |
| 118 | import PopoverMenu from '@shared/components/PopoverMenu.vue' |
| 119 | import PopoverMenuItem from '@shared/components/PopoverMenuItem.vue' |
| 120 |
| 121 | const props = withDefaults( |
| 122 | defineProps<{ |
| 123 | label: string |
| 124 | type?: 'text' | 'number' | 'switch' | 'select' | 'password' | 'kv' | 'tags' |
| 125 | readonly?: boolean |
| 126 | modelValue?: any |
| 127 | placeholder?: string |
| 128 | disabled?: boolean |
| 129 | tip?: string |
| 130 | prop?: string |
| 131 | options?: Array<{ label: string; value: string | number }> |
| 132 | min?: number |
| 133 | max?: number |
| 134 | keyPlaceholder?: string |
| 135 | valuePlaceholder?: string |
| 136 | }>(), |
| 137 | { |
| 138 | type: 'text', |
| 139 | readonly: false, |
| 140 | modelValue: undefined, |
| 141 | placeholder: '', |
| 142 | disabled: false, |
| 143 | tip: '', |
| 144 | prop: '', |
| 145 | options: () => [], |
| 146 | min: undefined, |
| 147 | max: undefined, |
| 148 | keyPlaceholder: 'Key', |
| 149 | valuePlaceholder: 'Value', |
| 150 | }, |
| 151 | ) |
| 152 |
| 153 | const emit = defineEmits<{ |
| 154 | 'update:modelValue': [value: any] |
| 155 | }>() |
| 156 |
| 157 | const handleNumberInput = (val: string) => { |
| 158 | if (val === '') { |
| 159 | emit('update:modelValue', undefined) |
| 160 | return |
| 161 | } |
| 162 | const num = Number(val) |
| 163 | if (!isNaN(num)) { |
| 164 | let clamped = num |
| 165 | if (props.min != null && clamped < props.min) clamped = props.min |
| 166 | if (props.max != null && clamped > props.max) clamped = props.max |
| 167 | emit('update:modelValue', clamped) |
| 168 | } |
| 169 | } |
| 170 |
| 171 | const selectDisplayValue = computed(() => { |
| 172 | const opt = props.options.find((o) => o.value === props.modelValue) |
| 173 | return opt ? opt.label : '' |
| 174 | }) |
| 175 |
| 176 | const selectWidth = computed(() => { |
| 177 | return Math.max(160, ...props.options.map((o) => o.label.length * 10 + 60)) |
| 178 | }) |
| 179 |
| 180 | const filteredOptions = (filterText: string) => { |
| 181 | if (!filterText) return props.options |
| 182 | const lower = filterText.toLowerCase() |
| 183 | return props.options.filter((o) => o.label.toLowerCase().includes(lower)) |
| 184 | } |
| 185 |
| 186 | const displayValue = computed(() => { |
| 187 | if (props.modelValue == null || props.modelValue === '') return '—' |
| 188 | if (props.type === 'select') { |
| 189 | const opt = props.options.find((o) => o.value === props.modelValue) |
| 190 | return opt ? opt.label : String(props.modelValue) |
| 191 | } |
| 192 | if (props.type === 'password') { |
| 193 | return props.modelValue ? '••••••' : '—' |
| 194 | } |
| 195 | return String(props.modelValue) |
| 196 | }) |
| 197 | </script> |
| 198 |
| 199 | <style scoped> |
| 200 | .config-field-switch-wrap { |
| 201 | display: flex; |
| 202 | align-items: center; |
| 203 | gap: 8px; |
| 204 | min-height: 32px; |
| 205 | width: 100%; |
| 206 | } |
| 207 |
| 208 | .config-field-switch-tip { |
| 209 | font-size: 12px; |
| 210 | color: var(--color-text-muted); |
| 211 | } |
| 212 |
| 213 | .config-field-tip { |
| 214 | font-size: 12px; |
| 215 | color: var(--el-text-color-secondary); |
| 216 | margin-top: 4px; |
| 217 | } |
| 218 |
| 219 | .config-field-readonly { |
| 220 | margin-bottom: 16px; |
| 221 | } |
| 222 |
| 223 | .config-field-label { |
| 224 | font-size: 13px; |
| 225 | font-weight: 500; |
| 226 | color: var(--color-text-secondary); |
| 227 | margin-bottom: 6px; |
| 228 | line-height: 1; |
| 229 | } |
| 230 |
| 231 | .config-field-readonly :deep(*) { |
| 232 | cursor: default !important; |
| 233 | } |
| 234 |
| 235 | .config-field-readonly :deep(.el-input.is-disabled .el-input__wrapper) { |
| 236 | background: var(--color-bg-tertiary); |
| 237 | box-shadow: 0 0 0 1px var(--color-border-lighter) inset; |
| 238 | } |
| 239 |
| 240 | .config-field-readonly :deep(.el-input.is-disabled .el-input__inner) { |
| 241 | color: var(--color-text-primary); |
| 242 | -webkit-text-fill-color: var(--color-text-primary); |
| 243 | font-family: ui-monospace, SFMono-Regular, Menlo, monospace; |
| 244 | } |
| 245 |
| 246 | .config-field-readonly :deep(.el-switch.is-disabled) { |
| 247 | opacity: 1; |
| 248 | } |
| 249 | </style> |