WorkflowDataColumns.vue 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <template>
  2. <div class="flex mb-4">
  3. <ui-input
  4. v-model.lowercase="state.query"
  5. autofocus
  6. placeholder="Search or add column"
  7. class="mr-2 flex-1"
  8. @keyup.enter="addColumn"
  9. @keyup.esc="$emit('close')"
  10. />
  11. <ui-button variant="accent" @click="addColumn">Add</ui-button>
  12. </div>
  13. <ul
  14. class="space-y-2 overflow-y-auto scroll py-1"
  15. style="max-height: calc(100vh - 10rem)"
  16. >
  17. <li
  18. v-for="(column, index) in columns"
  19. :key="column.name"
  20. class="flex items-center space-x-2"
  21. >
  22. <ui-input v-model="columns[index].name" placeholder="Column name" />
  23. <ui-input v-model="columns[index].default" placeholder="Default value" />
  24. <button @click="state.columns.splice(index, 1)">
  25. <v-remixicon name="riDeleteBin7Line" />
  26. </button>
  27. </li>
  28. </ul>
  29. </template>
  30. <script setup>
  31. import { computed, onMounted, watch, reactive } from 'vue';
  32. import { debounce } from '@/utils/helper';
  33. const props = defineProps({
  34. workflow: {
  35. type: Object,
  36. default: () => ({}),
  37. },
  38. });
  39. const emit = defineEmits(['update', 'close']);
  40. const state = reactive({
  41. query: '',
  42. columns: [],
  43. });
  44. const columns = computed(() =>
  45. state.columns.filter(({ name }) => name.includes(state.query))
  46. );
  47. function addColumn() {
  48. const isColumnExists = state.columns.some(({ name }) => name === state.query);
  49. if (isColumnExists || state.query.trim() === '') return;
  50. state.columns.push({ name: state.query, default: '' });
  51. state.query = '';
  52. }
  53. watch(
  54. () => state.columns,
  55. debounce((newValue) => {
  56. emit('update', { dataColumns: newValue });
  57. }, 250),
  58. { deep: true }
  59. );
  60. onMounted(() => {
  61. const tempColumns = props.workflow.dataColumns;
  62. state.columns = Array.isArray(tempColumns)
  63. ? tempColumns
  64. : Object.values(tempColumns);
  65. });
  66. </script>