zhuoyuan.wang
2024-06-19 15ebe96f28cadec6a726c5324593a40bbf56205f
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<template>
  <el-form class="app-form-fields" ref="formRef" :model="model" v-bind="$attrs" @submit.prevent v-loading="fieldsLoading || false">
        <template v-for="group in groupList">
          <el-row v-if="!group.name">
            <fieldItem :fields="group.fields" :model="model" :subModelChange="props.subModelChange"
                       :subInputDialog="props.subInputDialog"/>
          </el-row>
          <app-collapse-form :title="group.name" v-if="group.name">
            <el-row>
              <fieldItem :fields="group.fields" :model="model" :subModelChange="props.subModelChange"
                         :subInputDialog="props.subInputDialog"/>
            </el-row>
          </app-collapse-form>
        </template>
  </el-form>
</template>
 
<script setup>
 
import FieldItem from './FieldItem';
 
import {context} from '@/hooks';
 
const {useForceUpdate} = context;
 
/**
 * field 类型 type
 * @type {UnwrapNestedRefs<*[]>}
 */
 
const props = defineProps({
  fields: {
    type: Array,
    default: []
  },
  subInputDialog: {
    type: Function,
    default: () => {
    }
  },
  subModelChange: {
    type: Function,
    default: () => {
    }
  }
});
 
const [$props, forceUpdate] = useForceUpdate(props);
 
const fieldsLoading = ref(!Boolean($props.fields.length));
 
const groupList = computed(() => {
  const group = [...new Set($props.fields.map(field => (field.group || '')))];
  const list = group.map(e => {
    return {
      name: e,
      fields: $props.fields.filter(field => (field.group || '') === e)
    }
  })
  return list;
});
 
const $forceUpdate = async (force) => {
  forceUpdate(force);
  if (fieldsLoading.value) {
    fieldsLoading.value = !Boolean(force.fields.length);
  }
  onReset();
}
 
const formRef = ref();
 
const model = ref({});
 
let defaultModel = {};
 
onMounted(() => {
  setDefaultValue();
});
 
const onValidate = () => new Promise((resolve, reject) => {
  formRef.value.validate((valid) => {
    if (valid) {
      const data = {...model.value};
      Object.keys(data).forEach((key) => {
        const field = $props.fields.find(e => e.prop === key);
        if (field?.format) {
          data[key] = field.format(data[key]);
        }
      })
      resolve(data);
    } else {
      reject('校验失败');
    }
  });
});
 
const assignModel = (data) => Object.assign(model.value, data);
 
const setDefaultModel = (data) => {
  formRef.value.resetFields();
  defaultModel = data;
  setDefaultValue();
}
 
const getModel = () => model.value;
 
const setModel = (data) => {
  model.value = data;
}
 
/**
 * 初始化默认值
 */
const setDefaultValue = () => {
  $props.fields.forEach(field => {
    let value = '';
    if (field.type === 'el-input-dialog') {
      value = {};
    }
    if (field?.defaultValue !== undefined) {
      value = field.defaultValue;
    }
    if (defaultModel[field.prop]) {
      value = defaultModel[field.prop];
    }
    model.value[field.prop] = value;
  });
}
 
 
const onReset = () => {
  formRef.value.resetFields();
  setDefaultValue();
}
 
 
const getDefaultModel = () => defaultModel;
/**
 * 暴露给父组件的参数和方法 (外部需要什么,都可以从这里暴露出去)
 */
defineExpose({
  /**
   * 提交
   */
  onValidate,
  /**
   * 合并model
   */
  assignModel,
  /**
   * 设置默认model
   */
  setDefaultModel,
  /**
   * 获取默认值
   */
  getDefaultModel,
  /**
   * 获取model
   */
  getModel,
 
  /**
   * 设置model
   */
  setModel,
  /**
   * 重制
   */
  onReset,
  $forceUpdate
});
 
</script>
 
<style lang='scss' scoped>
.row-bg {
  height: 100px;
}
</style>
 
<style lang="scss">
.app-form-fields {
  position: relative;
  background: #fff;
  height: 100%;
 
  > .el-row {
    min-height: 50px;
  }
}
</style>