InputVariables.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <div class="space-y-3">
  3. <div class="flex items-center justify-between beautify">
  4. <label class="text-sm font-medium text-gray-700">输入变量</label>
  5. <ElButton @click="addVariable" class="p-1 rounded bg-gray-100 transition-colors" title="添加变量">
  6. <Icon icon="lucide:plus" :height="16" :width="16" class="text-gray-600" />
  7. </ElButton>
  8. </div>
  9. <div class="space-y-2">
  10. <div v-for="(variable, index) in variables" :key="variable.name" class="flex items-center gap-2 group">
  11. <ElInput v-model="variable.id" type="text" placeholder="变量名"
  12. class="w-1/3 text-sm border border-gray-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
  13. @input="handleVariableChange" />
  14. <ElInput class="w-1/3" v-model="variable.name" placeholder="设置变量值" />
  15. <ElButton @click="removeVariable(index)"
  16. class="w-1/3 p-1 opacity-50 group-hover:opacity-100 hover:bg-red-50 rounded transition-all"
  17. title="删除变量">
  18. <Icon icon="lucide:trash-2" :height="16" :width="16" class="text-red-500" />
  19. </ElButton>
  20. </div>
  21. <div v-if="variables.length === 0" class="text-sm text-gray-400 text-center py-4">
  22. <Icon icon="lucide:variable" :height="24" :width="25"></Icon>
  23. <p class="m-1">暂无输入变量</p>
  24. </div>
  25. </div>
  26. </div>
  27. </template>
  28. <script setup lang="ts">
  29. import { ref, watch } from 'vue'
  30. import { Icon } from '@iconify/vue'
  31. interface Variable {
  32. name: string
  33. id: string
  34. }
  35. interface Props {
  36. modelValue: Variable[]
  37. }
  38. interface Emits {
  39. (e: 'update:modelValue', value: Variable[]): void
  40. }
  41. const props = defineProps<Props>()
  42. const emit = defineEmits<Emits>()
  43. const variables = ref<Variable[]>(props.modelValue || [])
  44. watch(
  45. () => props.modelValue,
  46. (newVal) => {
  47. variables.value = newVal || []
  48. }
  49. )
  50. // 添加变量
  51. const addVariable = () => {
  52. const newVariable: Variable = {
  53. id: '',
  54. name: ''
  55. }
  56. variables.value.push(newVariable)
  57. handleVariableChange()
  58. }
  59. // 删除变量
  60. const removeVariable = (index: number) => {
  61. variables.value.splice(index, 1)
  62. handleVariableChange()
  63. }
  64. const handleVariableChange = () => {
  65. emit('update:modelValue', variables.value)
  66. }
  67. </script>
  68. <style lang="less" scoped>
  69. .beautify {
  70. padding-bottom: 10px;
  71. border-bottom: 1px solid #eee;
  72. }
  73. </style>