刘光辉
11 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<script lang="ts" setup>
import { computed, reactive, toRefs, unref } from 'vue';
 
import { useMessage } from '@jnpf/hooks';
import { BasicModal, useModalInner } from '@jnpf/ui/modal';
 
import { useDebounceFn } from '@vueuse/core';
import { cloneDeep } from 'lodash-es';
 
interface State {
  transferList: any[];
  fieldOptions: any[];
  formFieldsOptions: any[];
  type: string;
  allOptions: any[];
}
 
const emit = defineEmits(['register', 'change']);
const allowTransferJnpfKeySet = new Set([
  'areaSelect',
  'autoComplete',
  'billRule',
  'cascader',
  'checkbox',
  'colorPicker',
  'datePicker',
  'groupSelect',
  'input',
  'inputNumber',
  'location',
  'organizeSelect',
  'popupSelect',
  'posSelect',
  'radio',
  'rate',
  'roleSelect',
  'select',
  'slider',
  'switch',
  'textarea',
  'timePicker',
  'treeSelect',
  'userSelect',
]);
const state = reactive<State>({
  transferList: [],
  fieldOptions: [],
  formFieldsOptions: [],
  type: 'relationForm',
  allOptions: [],
});
const { transferList, fieldOptions } = toRefs(state);
const { createMessage } = useMessage();
const [registerModal, { closeModal }] = useModalInner(init);
const debounceOnSearch = useDebounceFn(onSearch, 300);
 
const getTitle = computed(() => (state.type == 'relationForm' ? '关联表单字段' : '数据接口字段'));
 
function init(data) {
  state.transferList = cloneDeep(data.transferList || []);
  state.fieldOptions = cloneDeep(data.fieldOptions || []);
  state.formFieldsOptions = cloneDeep(data.formFieldsOptions || []).filter((o) => allowTransferJnpfKeySet.has(o.__config__.jnpfKey));
  state.type = data.type || 'relationForm';
  if (!state.transferList.length) addTransferItem();
  if (state.type == 'popupSelect') state.allOptions = cloneDeep(data.fieldOptions || []);
}
function addTransferItem() {
  state.transferList.push({ targetField: '', sourceValue: '' });
}
function delTransferItem(index) {
  state.transferList.splice(index, 1);
}
function handleSubmit() {
  if (!formFieldListExist()) return;
  emit('change', state.transferList);
  closeModal();
}
function formFieldListExist() {
  const list = state.transferList;
  let isOk = true;
  for (const [i, e] of list.entries()) {
    if (!e.targetField) {
      createMessage.warning(`第${i + 1}行${unref(getTitle)}不能为空`);
      isOk = false;
      break;
    }
    if (!e.sourceValue) {
      createMessage.warning(`第${i + 1}行当前表单字段不能为空`);
      isOk = false;
      break;
    }
  }
  return isOk;
}
function onSearch(searchText: string) {
  state.fieldOptions = state.allOptions.filter((o) => o.value.toLowerCase().includes(searchText.toLowerCase()));
}
function onFocus(searchText: string) {
  onSearch(searchText);
}
function getTargetOptions(index: number) {
  const ignoreOptions: any[] = [];
  for (let i = 0; i < state.transferList.length; i++) {
    const e = state.transferList[i];
    if (e.sourceValue && index !== i) ignoreOptions.push(e.sourceValue);
  }
  const list = state.formFieldsOptions.filter((o) => !ignoreOptions.includes(o.id));
  return list;
}
</script>
<template>
  <BasicModal v-bind="$attrs" @register="registerModal" title="填充规则配置" :width="600" @ok="handleSubmit" destroy-on-close>
    <div class="condition-main">
      <a-row :gutter="8">
        <a-col :span="9">{{ getTitle }}</a-col>
        <a-col :span="4" />
        <a-col :span="9">当前表单字段</a-col>
      </a-row>
      <a-row :gutter="8" v-for="(item, index) in transferList" :key="index" class="mt-[10px]">
        <a-col :span="9">
          <jnpf-select
            v-if="state.type == 'relationForm'"
            v-model:value="item.targetField"
            :options="fieldOptions"
            placeholder="请选择"
            show-search
            allow-clear
            :disabled="item.required"
            :field-names="{ options: 'options1' }" />
          <a-auto-complete
            v-else
            v-model:value="item.targetField"
            placeholder="请输入"
            :options="fieldOptions"
            @focus="onFocus(item.targetField)"
            @search="debounceOnSearch(item.targetField)" />
        </a-col>
        <a-col :span="4" class="leading-[32px]">的值填充到</a-col>
        <a-col :span="9">
          <jnpf-select
            v-model:value="item.sourceValue"
            :options="getTargetOptions(index)"
            placeholder="请选择"
            show-search
            allow-clear
            :field-names="{ options: 'options1' }" />
        </a-col>
        <a-col :span="1" class="text-center" v-if="!item.required">
          <i class="icon-ym icon-ym-btn-clearn" @click="delTransferItem(index)"></i>
        </a-col>
      </a-row>
      <span class="link-text my-[10px] inline-block" @click="addTransferItem()"><i class="icon-ym icon-ym-btn-add mr-[4px] text-[14px]"></i>添加字段</span>
    </div>
  </BasicModal>
</template>