vue2-client 1.16.13 → 1.16.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,461 +1,466 @@
1
- <script>
2
- import { v4 as uuidv4 } from 'uuid'
3
- import {
4
- FormModel,
5
- Input,
6
- Button,
7
- Row,
8
- Col,
9
- Card,
10
- Divider,
11
- message,
12
- Select,
13
- Spin
14
- } from 'ant-design-vue'
15
- import { getConfigByNameAsync, runLogic } from '@vue2-client/services/api/common'
16
- import { mapState } from 'vuex'
17
- import { debounce } from 'lodash'
18
-
19
- export default {
20
- name: 'HAddNativeForm',
21
- components: {
22
- 'a-form-model': FormModel,
23
- 'a-form-item': FormModel.Item,
24
- 'a-input': Input,
25
- 'a-button': Button,
26
- 'a-row': Row,
27
- 'a-col': Col,
28
- 'a-card': Card,
29
- 'a-divider': Divider,
30
- 'a-select': Select,
31
- 'a-select-option': Select.Option,
32
- 'a-spin': Spin
33
- },
34
- props: {
35
- queryParamsName: {
36
- type: String,
37
- required: true
38
- },
39
- serviceName: {
40
- type: String,
41
- default: process.env.VUE_APP_SYSTEM_NAME
42
- },
43
- formStyle: {
44
- type: String,
45
- default: 'defaultForm'
46
- }
47
- },
48
- data () {
49
- return {
50
- loading: true,
51
- config: {},
52
- formInstances: []
53
- }
54
- },
55
- computed: {
56
- formItemLayoutComputed () {
57
- // 如果是 inline 布局,则不绑定 formItemLayout,让其使用 Ant Design Vue 默认的 inline 样式
58
- if (this.config.xAddFormLayout === 'inline') {
59
- return {}
60
- }
61
- if (this.config.formItemLayout) {
62
- return {
63
- labelCol: { span: this.config.formItemLayout.labelCol },
64
- wrapperCol: { span: this.config.formItemLayout.wrapperCol }
65
- }
66
- }
67
- // 如果是 horizontal 布局但未提供 formItemLayout,则提供一个默认值
68
- return {
69
- labelCol: { span: 6 }, // 默认标签宽度
70
- wrapperCol: { span: 18 } // 默认控件宽度,确保输入框足够宽
71
- }
72
- },
73
- ...mapState('account', { currUser: 'user' })
74
- },
75
- created () {
76
- this.debouncedSearch = debounce(this.performSearch, 500)
77
- this.fetchConfigAndInitializeForms()
78
- },
79
- methods: {
80
- async fetchConfigAndInitializeForms () {
81
- try {
82
- this.loading = true
83
- const res = await getConfigByNameAsync(this.queryParamsName, this.serviceName)
84
- if (res) {
85
- this.config = res
86
- if (this.config.manyForm) {
87
- this.addFormInstance()
88
- } else {
89
- this.addFormInstance()
90
- }
91
- } else {
92
- console.error('HAddNativeForm: 未能获取到配置内容。')
93
- message.error('未能获取到表单配置,请检查配置名称或网络。')
94
- }
95
- } catch (error) {
96
- console.error('HAddNativeForm: 获取配置时发生错误:', error)
97
- message.error('获取表单配置时发生错误。')
98
- } finally {
99
- this.loading = false
100
- }
101
- },
102
- async getOptions (item, formDataContext, searchKeyword = '') {
103
- if (!item.keyName) {
104
- return
105
- }
106
-
107
- this.$set(item, 'loadingOptions', true)
108
- let fetchedOptions = []
109
-
110
- try {
111
- if (item.keyName.startsWith('logic@')) {
112
- const logicName = item.keyName.substring(6)
113
- const result = await runLogic(logicName, { searchKeyword: searchKeyword }, this.serviceName)
114
-
115
- if (Array.isArray(result)) {
116
- fetchedOptions = result.map(opt => ({
117
- label: opt.label || opt.name || opt.text,
118
- value: (opt.value || opt.id) + '',
119
- ...opt // 将所有原始属性也添加到选项中
120
- }))
121
- } else {
122
- console.warn(`Logic '${logicName}' did not return an array for options. Result:`, result)
123
- }
124
- } else if (item.keyName.startsWith('config@')) {
125
- const configName = item.keyName.substring(7)
126
- const res = await getConfigByNameAsync(configName, this.serviceName)
127
- if (res && res.value && Array.isArray(res.value)) {
128
- fetchedOptions = res.value.map(opt => ({
129
- label: opt.label || opt.name || opt.text,
130
- value: (opt.value || opt.id) + '',
131
- ...opt // 将所有原始属性也添加到选项中
132
- }))
133
- } else {
134
- console.warn(`Config '${configName}' did not return a valid 'value' array for options. Result:`, res)
135
- }
136
- } else {
137
- // 全局字典
138
- if (this.$appdata && typeof this.$appdata.getDictionaryList === 'function') {
139
- const dictionaryList = this.$appdata.getDictionaryList(item.keyName)
140
- if (Array.isArray(dictionaryList)) {
141
- fetchedOptions = dictionaryList.map(opt => ({
142
- label: opt.text,
143
- value: opt.value + '',
144
- ...opt // 将所有原始属性也添加到选项中
145
- }))
146
- } else {
147
- console.warn(`Global dictionary '${item.keyName}' did not return an array. Result:`, dictionaryList)
148
- }
149
- } else {
150
- console.error('$appdata.getDictionaryList is not available. Please ensure it\'s globally provided.')
151
- }
152
- }
153
- } catch (error) {
154
- console.error(`Error fetching options for item '${item.key}':`, error)
155
- fetchedOptions = []
156
- } finally {
157
- this.$set(item, 'options', fetchedOptions)
158
- this.$set(item, 'loadingOptions', false)
159
- }
160
- },
161
- // 搜索处理函数,使用 debounce 优化性能
162
- handleSelectSearch (value, item, formDataContext) {
163
- // 更新搜索关键字,并触发 debouncedSearch
164
- this.$set(item, 'searchKeyword', value)
165
- this.debouncedSearch(item, formDataContext)
166
- },
167
- // 执行实际的搜索操作
168
- performSearch (item, formDataContext) {
169
- this.getOptions(item, formDataContext, item.searchKeyword)
170
- },
171
- // 当选择框获得焦点时,重新加载选项(如果没有搜索关键字)
172
- handleSelectFocus (item, formDataContext) {
173
- if (!item.searchKeyword || item.searchKeyword === '') {
174
- this.getOptions(item, formDataContext)
175
- }
176
- },
177
- // 批量设置所有表单值
178
- setAllFormsData (dataList) {
179
- dataList.forEach((data, index) => {
180
- if (this.formInstances[index]) {
181
- Object.assign(this.formInstances[index].formData, data)
182
- }
183
- })
184
- this.$forceUpdate()
185
- },
186
- addFormInstance () {
187
- const newId = uuidv4()
188
- const newFormData = {}
189
- const clonedFormItems = this.config.formItem ? JSON.parse(JSON.stringify(this.config.formItem)) : []
190
-
191
- if (clonedFormItems.length > 0) {
192
- for (const item of clonedFormItems) {
193
- newFormData[item.key] = undefined
194
- if (item.formType === 'select' && item.keyName) {
195
- this.$set(item, 'options', [])
196
- this.$set(item, 'loadingOptions', false)
197
- this.$set(item, 'searchKeyword', '')
198
- this.getOptions(item, newFormData)
199
- }
200
- }
201
- }
202
-
203
- this.formInstances.push({
204
- id: newId,
205
- formData: newFormData,
206
- formItems: clonedFormItems
207
- })
208
- },
209
- deleteFormInstance (id) {
210
- if (this.formInstances.length > 1) {
211
- this.formInstances = this.formInstances.filter(instance => instance.id !== id)
212
- } else {
213
- message.warning('至少需要保留一个表单实例。')
214
- }
215
- },
216
- getRules (item) {
217
- const rules = []
218
- if (item.rule && (item.rule.required === true || item.rule.required === 'true')) {
219
- rules.push({
220
- required: true,
221
- message: `请输入${item.title}`,
222
- trigger: 'blur'
223
- })
224
- }
225
- return rules
226
- },
227
- async onSubmit () {
228
- let allFormsValid = true
229
- const allFormsData = []
230
-
231
- for (const instance of this.formInstances) {
232
- const formRef = this.$refs['formModel_' + instance.id][0]
233
- if (formRef) {
234
- try {
235
- await formRef.validate()
236
- allFormsData.push(instance.formData)
237
- } catch (error) {
238
- allFormsValid = false
239
- break
240
- }
241
- }
242
- }
243
-
244
- if (allFormsValid) {
245
- message.success('表单提交成功!')
246
- this.$emit('submit', allFormsData)
247
- } else {
248
- message.error('部分表单校验失败,请检查。')
249
- }
250
- },
251
- getAllFormsData () {
252
- return this.formInstances.map(instance => {
253
- return instance.formData
254
- })
255
- },
256
- handleSelectChange (value, option, selectItem, formData, allFormItems) {
257
- // 直接获取option.data.props中的完整数据
258
- const selectedOptionData = option?.data?.props
259
- if (selectedOptionData) {
260
- for (const key in selectedOptionData) {
261
- if (Object.prototype.hasOwnProperty.call(selectedOptionData, key) && !['label', 'value'].includes(key)) {
262
- const targetItem = allFormItems.find(item => item.key === key)
263
- if (targetItem) {
264
- formData[key] = selectedOptionData[key]
265
- this.$forceUpdate()
266
- }
267
- }
268
- }
269
- }
270
- }
271
- }
272
- }
273
- </script>
274
-
275
- <template>
276
- <div class="h-add-native-form-group" :class="[`h-add-native-form-${formStyle}`]">
277
- <a-skeleton :loading="loading" :paragraph="{ rows: 4 }" />
278
- <div v-if="!loading">
279
- <div v-for="(formInstance, formIndex) in formInstances" :key="formInstance.id" class="form-instance-wrapper">
280
- <!-- <div v-if="config.manyForm" class="form-instance-title">药品 {{ formIndex + 1 }}</div> -->
281
- <a-form-model
282
- :ref="'formModel_' + formInstance.id"
283
- :model="formInstance.formData"
284
- v-bind="formItemLayoutComputed"
285
- :layout="config.xAddFormLayout || 'horizontal'"
286
- >
287
- <a-row :gutter="16">
288
- <a-col
289
- v-for="(item, itemIndex) in formInstance.formItems"
290
- :key="itemIndex"
291
- :xs="24"
292
- :sm="12"
293
- :md="8"
294
- :lg="6"
295
- :xl="6"
296
- >
297
- <a-form-item
298
- :label="item.title"
299
- :prop="item.key"
300
- :rules="getRules(item)"
301
- >
302
- <a-input
303
- v-if="item.formType === 'input'"
304
- v-model="formInstance.formData[item.key]"
305
- :placeholder="'请输入' + item.title"
306
- style="width: 100%"
307
- />
308
- <a-select
309
- v-else-if="item.formType === 'select'"
310
- v-model="formInstance.formData[item.key]"
311
- :placeholder=" '请选择' + item.title"
312
- :options="item.options"
313
- :loading="item.loadingOptions"
314
- show-search
315
- allowClear
316
- :filter-option="false"
317
- @search="(value) => handleSelectSearch(value, item, formInstance.formData)"
318
- @focus="() => handleSelectFocus(item, formInstance.formData)"
319
- @change="(value, option) => handleSelectChange(value, option, item, formInstance.formData, formInstance.formItems)"
320
- :dropdownMatchSelectWidth="false"
321
- :dropdownStyle="{ minWidth: '200px' }"
322
- style="width: 100%"
323
- >
324
- <a-spin v-if="item.loadingOptions" slot="notFoundContent" size="small" />
325
- </a-select>
326
- <!-- 可以根据需要添加其他表单类型,例如 select, datePicker 等 -->
327
- </a-form-item>
328
- </a-col>
329
- </a-row>
330
- </a-form-model>
331
- <a-divider v-if="config.manyForm && formIndex < formInstances.length - 1" />
332
- </div>
333
-
334
- <!-- 全局添加/删除按钮,只在所有表单实例之后显示 -->
335
- <a-row v-if="config.manyForm" type="flex" justify="start" style="margin-top: 20px; gap: 10px;">
336
- <a-button v-if="config.showAddButton" class="form-action-button" icon="plus" @click="addFormInstance"></a-button>
337
- <a-button v-if="config.showDeleteButton && formInstances.length > 1" class="form-action-button" icon="minus" @click="deleteFormInstance(formInstances[formInstances.length - 1].id)"></a-button>
338
- </a-row>
339
-
340
- <a-row v-if="config.showSubmitBtn" type="flex" justify="start" style="margin-top: 20px;">
341
- <a-col>
342
- <a-button type="primary" @click="onSubmit">{{ config.btnName || '提交' }}</a-button>
343
- </a-col>
344
- </a-row>
345
- </div>
346
- </div>
347
- </template>
348
-
349
- <style scoped lang="less">
350
- .h-add-native-form-group {
351
- // 基础样式,对应 defaultForm
352
- &.h-add-native-form-defaultForm {
353
- padding: 0px;
354
- background-color: #fff;
355
- border-radius: 8px;
356
- }
357
- }
358
-
359
- .form-instance-wrapper {
360
- margin-bottom: 20px;
361
- /* 移除边框和背景色,使其看起来像一个整体 */
362
- /* border: 1px solid #e8e8e8;
363
- border-radius: 4px;
364
- background-color: #fff; */
365
- }
366
-
367
- .form-instance-title {
368
- font-weight: bold;
369
- font-size: 16px;
370
- margin-bottom: 10px;
371
- padding-left: 10px; /* 与表单项对齐 */
372
- color: rgba(0, 0, 0, 0.85);
373
- }
374
-
375
- .form-action-button {
376
- width: 40px;
377
- height: 40px;
378
- border-radius: 8px;
379
- border: 1px solid #d9d9d9;
380
- background-color: #fff;
381
- color: rgba(0, 0, 0, 0.65);
382
- display: flex;
383
- justify-content: center;
384
- align-items: center;
385
-
386
- &:hover {
387
- border-color: #40a9ff; /* 鼠标悬停时边框颜色 */
388
- color: #40a9ff; /* 鼠标悬停时图标颜色 */
389
- }
390
- }
391
-
392
- // 修复表单控件宽度问题
393
- :deep(.ant-form-item-control-wrapper) {
394
- width: 100%;
395
- }
396
-
397
- :deep(.ant-form-item-control) {
398
- width: 100%;
399
- }
400
-
401
- :deep(.ant-input),
402
- :deep(.ant-select) {
403
- width: 100% !important;
404
- }
405
-
406
- :deep(.ant-card-extra) {
407
- padding: 0;
408
- }
409
-
410
- :deep(.ant-form-item) {
411
- margin-bottom: 16px; // 增加表单项间距
412
- display: flex;
413
- flex-direction: column;
414
- margin-left: 120px;
415
- }
416
-
417
- :deep(.ant-form-item-label) {
418
- text-align: left;
419
- white-space: normal;
420
- }
421
-
422
- :deep(.ant-form-item-control-wrapper) {
423
- flex: 1;
424
- }
425
-
426
- // 针对 inline 布局的样式调整
427
- :deep(.ant-form-inline .ant-form-item) {
428
- margin-right: 0;
429
- margin-bottom: 16px;
430
- display: flex;
431
- flex-direction: row;
432
- align-items: flex-start;
433
- }
434
-
435
- :deep(.ant-form-inline .ant-form-item-label) {
436
- padding-right: 8px;
437
- flex: none;
438
- }
439
-
440
- :deep(.ant-form-inline .ant-form-item-control-wrapper) {
441
- flex: 1;
442
- min-width: 0;
443
- }
444
-
445
- :deep(.ant-form-inline .ant-input),
446
- :deep(.ant-form-inline .ant-select) {
447
- width: 100% !important;
448
- min-width: 120px; // 确保有最小宽度
449
- }
450
-
451
- // 响应式布局调整
452
- @media (max-width: 768px) {
453
- :deep(.ant-col-xs-24) {
454
- width: 100%;
455
- }
456
-
457
- :deep(.ant-form-item) {
458
- margin-bottom: 12px;
459
- }
460
- }
461
- </style>
1
+ <script>
2
+ import { v4 as uuidv4 } from 'uuid'
3
+ import {
4
+ FormModel,
5
+ Input,
6
+ Button,
7
+ Row,
8
+ Col,
9
+ Card,
10
+ Divider,
11
+ message,
12
+ Select,
13
+ Spin
14
+ } from 'ant-design-vue'
15
+ import { getConfigByNameAsync, runLogic } from '@vue2-client/services/api/common'
16
+ import { mapState } from 'vuex'
17
+ import { debounce } from 'lodash'
18
+
19
+ export default {
20
+ name: 'HAddNativeForm',
21
+ components: {
22
+ 'a-form-model': FormModel,
23
+ 'a-form-item': FormModel.Item,
24
+ 'a-input': Input,
25
+ 'a-button': Button,
26
+ 'a-row': Row,
27
+ 'a-col': Col,
28
+ 'a-card': Card,
29
+ 'a-divider': Divider,
30
+ 'a-select': Select,
31
+ 'a-select-option': Select.Option,
32
+ 'a-spin': Spin
33
+ },
34
+ props: {
35
+ queryParamsName: {
36
+ type: String,
37
+ required: true
38
+ },
39
+ serviceName: {
40
+ type: String,
41
+ default: process.env.VUE_APP_SYSTEM_NAME
42
+ },
43
+ formStyle: {
44
+ type: String,
45
+ default: 'defaultForm'
46
+ }
47
+ },
48
+ data () {
49
+ return {
50
+ loading: true,
51
+ config: {},
52
+ formInstances: []
53
+ }
54
+ },
55
+ computed: {
56
+ formItemLayoutComputed () {
57
+ // 如果是 inline 布局,则不绑定 formItemLayout,让其使用 Ant Design Vue 默认的 inline 样式
58
+ if (this.config.xAddFormLayout === 'inline') {
59
+ return {}
60
+ }
61
+ if (this.config.formItemLayout) {
62
+ return {
63
+ labelCol: { span: this.config.formItemLayout.labelCol },
64
+ wrapperCol: { span: this.config.formItemLayout.wrapperCol }
65
+ }
66
+ }
67
+ // 如果是 horizontal 布局但未提供 formItemLayout,则提供一个默认值
68
+ return {
69
+ labelCol: { span: 6 }, // 默认标签宽度
70
+ wrapperCol: { span: 18 } // 默认控件宽度,确保输入框足够宽
71
+ }
72
+ },
73
+ ...mapState('account', { currUser: 'user' })
74
+ },
75
+ created () {
76
+ this.debouncedSearch = debounce(this.performSearch, 500)
77
+ this.fetchConfigAndInitializeForms()
78
+ },
79
+ methods: {
80
+ async fetchConfigAndInitializeForms () {
81
+ try {
82
+ this.loading = true
83
+ const res = await getConfigByNameAsync(this.queryParamsName, this.serviceName)
84
+ if (res) {
85
+ this.config = res
86
+ if (this.config.manyForm) {
87
+ this.addFormInstance()
88
+ } else {
89
+ this.addFormInstance()
90
+ }
91
+ } else {
92
+ console.error('HAddNativeForm: 未能获取到配置内容。')
93
+ message.error('未能获取到表单配置,请检查配置名称或网络。')
94
+ }
95
+ } catch (error) {
96
+ console.error('HAddNativeForm: 获取配置时发生错误:', error)
97
+ message.error('获取表单配置时发生错误。')
98
+ } finally {
99
+ this.loading = false
100
+ }
101
+ },
102
+ async getOptions (item, formDataContext, searchKeyword = '') {
103
+ if (!item.keyName) {
104
+ return
105
+ }
106
+
107
+ this.$set(item, 'loadingOptions', true)
108
+ let fetchedOptions = []
109
+
110
+ try {
111
+ if (item.keyName.startsWith('logic@')) {
112
+ const logicName = item.keyName.substring(6)
113
+ const result = await runLogic(logicName, { searchKeyword: searchKeyword }, this.serviceName)
114
+
115
+ if (Array.isArray(result)) {
116
+ fetchedOptions = result.map(opt => ({
117
+ label: opt.label || opt.name || opt.text,
118
+ value: (opt.value || opt.id) + '',
119
+ ...opt // 将所有原始属性也添加到选项中
120
+ }))
121
+ } else {
122
+ console.warn(`Logic '${logicName}' did not return an array for options. Result:`, result)
123
+ }
124
+ } else if (item.keyName.startsWith('config@')) {
125
+ const configName = item.keyName.substring(7)
126
+ const res = await getConfigByNameAsync(configName, this.serviceName)
127
+ if (res && res.value && Array.isArray(res.value)) {
128
+ fetchedOptions = res.value.map(opt => ({
129
+ label: opt.label || opt.name || opt.text,
130
+ value: (opt.value || opt.id) + '',
131
+ ...opt // 将所有原始属性也添加到选项中
132
+ }))
133
+ } else {
134
+ console.warn(`Config '${configName}' did not return a valid 'value' array for options. Result:`, res)
135
+ }
136
+ } else {
137
+ // 全局字典
138
+ if (this.$appdata && typeof this.$appdata.getDictionaryList === 'function') {
139
+ const dictionaryList = this.$appdata.getDictionaryList(item.keyName)
140
+ if (Array.isArray(dictionaryList)) {
141
+ fetchedOptions = dictionaryList.map(opt => ({
142
+ label: opt.text,
143
+ value: opt.value + '',
144
+ ...opt // 将所有原始属性也添加到选项中
145
+ }))
146
+ } else {
147
+ console.warn(`Global dictionary '${item.keyName}' did not return an array. Result:`, dictionaryList)
148
+ }
149
+ } else {
150
+ console.error('$appdata.getDictionaryList is not available. Please ensure it\'s globally provided.')
151
+ }
152
+ }
153
+ } catch (error) {
154
+ console.error(`Error fetching options for item '${item.key}':`, error)
155
+ fetchedOptions = []
156
+ } finally {
157
+ this.$set(item, 'options', fetchedOptions)
158
+ this.$set(item, 'loadingOptions', false)
159
+ }
160
+ },
161
+ // 搜索处理函数,使用 debounce 优化性能
162
+ handleSelectSearch (value, item, formDataContext) {
163
+ // 更新搜索关键字,并触发 debouncedSearch
164
+ this.$set(item, 'searchKeyword', value)
165
+ this.debouncedSearch(item, formDataContext)
166
+ },
167
+ // 执行实际的搜索操作
168
+ performSearch (item, formDataContext) {
169
+ this.getOptions(item, formDataContext, item.searchKeyword)
170
+ },
171
+ // 当选择框获得焦点时,重新加载选项(如果没有搜索关键字)
172
+ handleSelectFocus (item, formDataContext) {
173
+ if (!item.searchKeyword || item.searchKeyword === '') {
174
+ this.getOptions(item, formDataContext)
175
+ }
176
+ },
177
+ // 批量设置所有表单值
178
+ setAllFormsData (dataList) {
179
+ // 确保有足够多的表单实例来容纳所有数据
180
+ while (this.formInstances.length < dataList.length) {
181
+ this.addFormInstance()
182
+ }
183
+ dataList.forEach((data, index) => {
184
+ if (this.formInstances[index]) {
185
+ Object.assign(this.formInstances[index].formData, data)
186
+ }
187
+ })
188
+ this.$forceUpdate()
189
+ },
190
+ addFormInstance () {
191
+ const newId = uuidv4()
192
+ const newFormData = {}
193
+ const clonedFormItems = this.config.formItem ? JSON.parse(JSON.stringify(this.config.formItem)) : []
194
+
195
+ if (clonedFormItems.length > 0) {
196
+ for (const item of clonedFormItems) {
197
+ newFormData[item.key] = undefined
198
+ if (item.formType === 'select' && item.keyName) {
199
+ this.$set(item, 'options', [])
200
+ this.$set(item, 'loadingOptions', false)
201
+ this.$set(item, 'searchKeyword', '')
202
+ this.getOptions(item, newFormData)
203
+ }
204
+ }
205
+ }
206
+
207
+ this.formInstances.push({
208
+ id: newId,
209
+ formData: newFormData,
210
+ formItems: clonedFormItems
211
+ })
212
+ },
213
+ deleteFormInstance (id) {
214
+ if (this.formInstances.length > 1) {
215
+ this.formInstances = this.formInstances.filter(instance => instance.id !== id)
216
+ } else {
217
+ message.warning('至少需要保留一个表单实例。')
218
+ }
219
+ },
220
+ getRules (item) {
221
+ const rules = []
222
+ if (item.rule && (item.rule.required === true || item.rule.required === 'true')) {
223
+ rules.push({
224
+ required: true,
225
+ message: `请输入${item.title}`,
226
+ trigger: 'blur'
227
+ })
228
+ }
229
+ return rules
230
+ },
231
+ async onSubmit () {
232
+ let allFormsValid = true
233
+ const allFormsData = []
234
+
235
+ for (const instance of this.formInstances) {
236
+ const formRef = this.$refs['formModel_' + instance.id][0]
237
+ if (formRef) {
238
+ try {
239
+ await formRef.validate()
240
+ allFormsData.push(instance.formData)
241
+ } catch (error) {
242
+ allFormsValid = false
243
+ break
244
+ }
245
+ }
246
+ }
247
+
248
+ if (allFormsValid) {
249
+ message.success('表单提交成功!')
250
+ this.$emit('submit', allFormsData)
251
+ } else {
252
+ message.error('部分表单校验失败,请检查。')
253
+ }
254
+ },
255
+ getAllFormsData () {
256
+ return this.formInstances.map(instance => {
257
+ return instance.formData
258
+ })
259
+ },
260
+ handleSelectChange (value, option, selectItem, formData, allFormItems) {
261
+ // 直接获取option.data.props中的完整数据
262
+ const selectedOptionData = option?.data?.props
263
+ if (selectedOptionData) {
264
+ for (const key in selectedOptionData) {
265
+ if (Object.prototype.hasOwnProperty.call(selectedOptionData, key) && !['label', 'value'].includes(key)) {
266
+ const targetItem = allFormItems.find(item => item.key === key)
267
+ if (targetItem) {
268
+ formData[key] = selectedOptionData[key]
269
+ this.$forceUpdate()
270
+ }
271
+ }
272
+ }
273
+ }
274
+ }
275
+ }
276
+ }
277
+ </script>
278
+
279
+ <template>
280
+ <div class="h-add-native-form-group" :class="[`h-add-native-form-${formStyle}`]">
281
+ <a-skeleton :loading="loading" :paragraph="{ rows: 4 }" />
282
+ <div v-if="!loading">
283
+ <div v-for="(formInstance, formIndex) in formInstances" :key="formInstance.id" class="form-instance-wrapper">
284
+ <!-- <div v-if="config.manyForm" class="form-instance-title">药品 {{ formIndex + 1 }}</div> -->
285
+ <a-form-model
286
+ :ref="'formModel_' + formInstance.id"
287
+ :model="formInstance.formData"
288
+ v-bind="formItemLayoutComputed"
289
+ :layout="config.xAddFormLayout || 'horizontal'"
290
+ >
291
+ <a-row :gutter="16">
292
+ <a-col
293
+ v-for="(item, itemIndex) in formInstance.formItems"
294
+ :key="itemIndex"
295
+ :xs="24"
296
+ :sm="12"
297
+ :md="8"
298
+ :lg="6"
299
+ :xl="6"
300
+ >
301
+ <a-form-item
302
+ :label="item.title"
303
+ :prop="item.key"
304
+ :rules="getRules(item)"
305
+ >
306
+ <a-input
307
+ v-if="item.formType === 'input'"
308
+ v-model="formInstance.formData[item.key]"
309
+ :placeholder="'请输入' + item.title"
310
+ style="width: 100%"
311
+ />
312
+ <a-select
313
+ v-else-if="item.formType === 'select'"
314
+ v-model="formInstance.formData[item.key]"
315
+ :placeholder=" '请选择' + item.title"
316
+ :options="item.options"
317
+ :loading="item.loadingOptions"
318
+ show-search
319
+ allowClear
320
+ :filter-option="false"
321
+ @search="(value) => handleSelectSearch(value, item, formInstance.formData)"
322
+ @focus="() => handleSelectFocus(item, formInstance.formData)"
323
+ @change="(value, option) => handleSelectChange(value, option, item, formInstance.formData, formInstance.formItems)"
324
+ :dropdownMatchSelectWidth="false"
325
+ :dropdownStyle="{ minWidth: '200px' }"
326
+ style="width: 100%"
327
+ >
328
+ <a-spin v-if="item.loadingOptions" slot="notFoundContent" size="small" />
329
+ </a-select>
330
+ <!-- 可以根据需要添加其他表单类型,例如 select, datePicker 等 -->
331
+ </a-form-item>
332
+ </a-col>
333
+ </a-row>
334
+ </a-form-model>
335
+ <a-divider style="margin: 10px" v-if="config.manyForm && formIndex < formInstances.length - 1" />
336
+ </div>
337
+
338
+ <!-- 全局添加/删除按钮,只在所有表单实例之后显示 -->
339
+ <a-row v-if="config.manyForm" type="flex" justify="start" style="margin-top: 20px; gap: 10px;">
340
+ <a-button v-if="config.showAddButton" class="form-action-button" icon="plus" @click="addFormInstance"></a-button>
341
+ <a-button v-if="config.showDeleteButton && formInstances.length > 1" class="form-action-button" style="margin-left: 0px" icon="minus" @click="deleteFormInstance(formInstances[formInstances.length - 1].id)"></a-button>
342
+ </a-row>
343
+
344
+ <a-row v-if="config.showSubmitBtn" type="flex" justify="start" style="margin-top: 20px;">
345
+ <a-col>
346
+ <a-button type="primary" @click="onSubmit">{{ config.btnName || '提交' }}</a-button>
347
+ </a-col>
348
+ </a-row>
349
+ </div>
350
+ </div>
351
+ </template>
352
+
353
+ <style scoped lang="less">
354
+ .h-add-native-form-group {
355
+ // 基础样式,对应 defaultForm
356
+ &.h-add-native-form-defaultForm {
357
+ padding: 0px;
358
+ // background-color: #fff;
359
+ border-radius: 8px;
360
+ }
361
+ }
362
+
363
+ .form-instance-wrapper {
364
+ margin-bottom: 0px;
365
+ /* 移除边框和背景色,使其看起来像一个整体 */
366
+ /* border: 1px solid #e8e8e8;
367
+ border-radius: 4px;
368
+ background-color: #fff; */
369
+ }
370
+
371
+ .form-instance-title {
372
+ font-weight: bold;
373
+ font-size: 16px;
374
+ margin-bottom: 10px;
375
+ padding-left: 10px; /* 与表单项对齐 */
376
+ color: rgba(0, 0, 0, 0.85);
377
+ }
378
+
379
+ .form-action-button {
380
+ width: 40px;
381
+ height: 40px;
382
+ border-radius: 8px;
383
+ border: 1px solid #d9d9d9;
384
+ background-color: #fff;
385
+ color: rgba(0, 0, 0, 0.65);
386
+ display: flex;
387
+ justify-content: center;
388
+ align-items: center;
389
+ margin-left: 95px;
390
+
391
+ &:hover {
392
+ border-color: #40a9ff; /* 鼠标悬停时边框颜色 */
393
+ color: #40a9ff; /* 鼠标悬停时图标颜色 */
394
+ }
395
+ }
396
+
397
+ // 修复表单控件宽度问题
398
+ :deep(.ant-form-item-control-wrapper) {
399
+ width: 100%;
400
+ }
401
+
402
+ :deep(.ant-form-item-control) {
403
+ width: 100%;
404
+ }
405
+
406
+ :deep(.ant-input),
407
+ :deep(.ant-select) {
408
+ width: 100% !important;
409
+ }
410
+
411
+ :deep(.ant-card-extra) {
412
+ padding: 0;
413
+ }
414
+
415
+ :deep(.ant-form-item) {
416
+ margin-bottom: 5px; // 增加表单项间距
417
+ display: flex;
418
+ flex-direction: column;
419
+ margin-left: 95px;
420
+ }
421
+
422
+ :deep(.ant-form-item-label) {
423
+ text-align: left;
424
+ white-space: normal;
425
+ }
426
+
427
+ :deep(.ant-form-item-control-wrapper) {
428
+ flex: 1;
429
+ }
430
+
431
+ // 针对 inline 布局的样式调整
432
+ :deep(.ant-form-inline .ant-form-item) {
433
+ margin-right: 0;
434
+ margin-bottom: 16px;
435
+ display: flex;
436
+ flex-direction: row;
437
+ align-items: flex-start;
438
+ }
439
+
440
+ :deep(.ant-form-inline .ant-form-item-label) {
441
+ padding-right: 8px;
442
+ flex: none;
443
+ }
444
+
445
+ :deep(.ant-form-inline .ant-form-item-control-wrapper) {
446
+ flex: 1;
447
+ min-width: 0;
448
+ }
449
+
450
+ :deep(.ant-form-inline .ant-input),
451
+ :deep(.ant-form-inline .ant-select) {
452
+ width: 100% !important;
453
+ min-width: 120px; // 确保有最小宽度
454
+ }
455
+
456
+ // 响应式布局调整
457
+ @media (max-width: 768px) {
458
+ :deep(.ant-col-xs-24) {
459
+ width: 100%;
460
+ }
461
+
462
+ :deep(.ant-form-item) {
463
+ margin-bottom: 12px;
464
+ }
465
+ }
466
+ </style>