gi-component 0.0.26 → 0.0.27
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,426 +1,423 @@
|
|
|
1
|
-
<template>
|
|
2
|
-
<ElForm ref="formRef" :class="getClass" v-bind="formProps" :model="props.modelValue">
|
|
3
|
-
<Grid class="w-full" :col-gap="12" v-bind="props.gridProps" :collapsed="collapsed">
|
|
4
|
-
<template v-for="(item, index) in props.columns">
|
|
5
|
-
<GridItem v-if="item.type === 'title'" :key="`title${index}`" :span="
|
|
6
|
-
<ElFormItem label-width="0">
|
|
7
|
-
<el-alert :class="b('form-item__title')" :title="typeof item.label === 'string' ? item.label : ''"
|
|
8
|
-
type="info" :closable="false" />
|
|
9
|
-
</ElFormItem>
|
|
10
|
-
</GridItem>
|
|
11
|
-
|
|
12
|
-
<template v-else>
|
|
13
|
-
<GridItem v-if="!isHide(item)" :key="item.field + index" v-bind="item.gridItemProps || props.gridItemProps"
|
|
14
|
-
:span="item.span
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
">
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
<
|
|
21
|
-
<
|
|
22
|
-
</
|
|
23
|
-
<
|
|
24
|
-
<
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
<template v-if="
|
|
34
|
-
|
|
35
|
-
</template>
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
</
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
</template>
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
<ElButton
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
import
|
|
83
|
-
import
|
|
84
|
-
import {
|
|
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
|
-
const
|
|
120
|
-
|
|
121
|
-
const
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const
|
|
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
|
-
|
|
194
|
-
})
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
)
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
'input': El.
|
|
268
|
-
'
|
|
269
|
-
'
|
|
270
|
-
'
|
|
271
|
-
'
|
|
272
|
-
'
|
|
273
|
-
'
|
|
274
|
-
'
|
|
275
|
-
'
|
|
276
|
-
'
|
|
277
|
-
'
|
|
278
|
-
'
|
|
279
|
-
'
|
|
280
|
-
'
|
|
281
|
-
'
|
|
282
|
-
'
|
|
283
|
-
'
|
|
284
|
-
'
|
|
285
|
-
'
|
|
286
|
-
'
|
|
287
|
-
'
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
if (item.required) {
|
|
298
|
-
return [
|
|
299
|
-
{ required: true, message:
|
|
300
|
-
...
|
|
301
|
-
]
|
|
302
|
-
}
|
|
303
|
-
if (props.fc?.[item.field]?.required) {
|
|
304
|
-
return [
|
|
305
|
-
{
|
|
306
|
-
required: props.fc
|
|
307
|
-
message:
|
|
308
|
-
},
|
|
309
|
-
...
|
|
310
|
-
]
|
|
311
|
-
}
|
|
312
|
-
return item.rules
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
/** 隐藏表单项 */
|
|
316
|
-
function isHide(item: FormColumnItem) {
|
|
317
|
-
if (typeof item.hide === 'boolean') return item.hide
|
|
318
|
-
if (typeof item.hide === 'function') {
|
|
319
|
-
return item.hide(props.modelValue)
|
|
320
|
-
}
|
|
321
|
-
if (props.fc?.[item.field]?.hidden) return true
|
|
322
|
-
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
/** 禁用表单项 */
|
|
326
|
-
function isDisabled(item: FormColumnItem) {
|
|
327
|
-
if (item?.props?.disabled !== undefined) return item?.props?.disabled
|
|
328
|
-
if (props.fc?.[item.field]?.disabled === true) return true
|
|
329
|
-
return false
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
/** 表单数据更新 */
|
|
333
|
-
function updateModelValue(value: any, item: FormColumnItem) {
|
|
334
|
-
emit(
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
</style>
|
|
1
|
+
<template>
|
|
2
|
+
<ElForm ref="formRef" :class="getClass" v-bind="formProps" :model="props.modelValue">
|
|
3
|
+
<Grid class="w-full" :col-gap="12" v-bind="props.gridProps" :collapsed="collapsed">
|
|
4
|
+
<template v-for="(item, index) in props.columns">
|
|
5
|
+
<GridItem v-if="item.type === 'title'" :key="`title${index}`" :span="24">
|
|
6
|
+
<ElFormItem label-width="0">
|
|
7
|
+
<el-alert :class="b('form-item__title')" :title="typeof item.label === 'string' ? item.label : ''"
|
|
8
|
+
type="info" :closable="false" />
|
|
9
|
+
</ElFormItem>
|
|
10
|
+
</GridItem>
|
|
11
|
+
|
|
12
|
+
<template v-else>
|
|
13
|
+
<GridItem v-if="!isHide(item)" :key="item.field + index" v-bind="item.gridItemProps || props.gridItemProps"
|
|
14
|
+
:span="item.span || item.gridItemProps?.span || props?.gridItemProps?.span">
|
|
15
|
+
<ElFormItem :key="item.field + index" :prop="item.field" :label="item.label" :rules="getFormItemRules(item)"
|
|
16
|
+
v-bind="item.formItemProps">
|
|
17
|
+
<template v-if="item?.labelRender" #label>
|
|
18
|
+
<component :is="item.labelRender"></component>
|
|
19
|
+
</template>
|
|
20
|
+
<div v-if="item.type === 'slot'" class="w-full">
|
|
21
|
+
<slot :name="item.field" :item="item"></slot>
|
|
22
|
+
</div>
|
|
23
|
+
<template v-else>
|
|
24
|
+
<div :class="b('form-item__content')">
|
|
25
|
+
<div :class="b('form-item__component')">
|
|
26
|
+
<component :is="CompMap[item.type] || item.type" :disabled="isDisabled(item)" class="w-full"
|
|
27
|
+
v-bind="getComponentBindProps(item)" :model-value="props.modelValue[item.fieldName || item.field]"
|
|
28
|
+
@update:model-value="updateModelValue($event, item)">
|
|
29
|
+
<template v-for="(slotValue, slotKey) in item?.slots || {}" :key="slotKey" #[slotKey]="scope">
|
|
30
|
+
<template v-if="typeof slotValue === 'string'">
|
|
31
|
+
{{ slotValue }}
|
|
32
|
+
</template>
|
|
33
|
+
<template v-else-if="slotValue">
|
|
34
|
+
<component :is="slotValue(scope)"></component>
|
|
35
|
+
</template>
|
|
36
|
+
</template>
|
|
37
|
+
</component>
|
|
38
|
+
<ElText v-if="item.tip" :class="b('form-item__tip')" type="info" size="small">
|
|
39
|
+
{{ item.tip }}
|
|
40
|
+
</ElText>
|
|
41
|
+
</div>
|
|
42
|
+
<!-- 额外信息 -->
|
|
43
|
+
<div v-if="item.extra" :class="b('form-item__extra')">
|
|
44
|
+
<template v-if="typeof item.extra === 'string'">
|
|
45
|
+
<ElText type="info" size="small">
|
|
46
|
+
{{
|
|
47
|
+
item.extra
|
|
48
|
+
}}
|
|
49
|
+
</ElText>
|
|
50
|
+
</template>
|
|
51
|
+
<template v-else-if="item.extra">
|
|
52
|
+
<component :is="item.extra"></component>
|
|
53
|
+
</template>
|
|
54
|
+
</div>
|
|
55
|
+
</div>
|
|
56
|
+
</template>
|
|
57
|
+
</ElFormItem>
|
|
58
|
+
</GridItem>
|
|
59
|
+
</template>
|
|
60
|
+
</template>
|
|
61
|
+
|
|
62
|
+
<GridItem v-if="props.search" :suffix="props.search" :span="props?.gridItemProps?.span">
|
|
63
|
+
<ElSpace :class="b('form__search-btns')">
|
|
64
|
+
<ElButton type="primary" @click="emit('search')">
|
|
65
|
+
{{ searchText }}
|
|
66
|
+
</ElButton>
|
|
67
|
+
<ElButton @click="emit('reset')"> 重置 </ElButton>
|
|
68
|
+
<ElButton v-if="!props.hideFoldBtn" class="form__fold-btn" type="primary"
|
|
69
|
+
:icon="collapsed ? ArrowDown : ArrowUp" text size="small" @click="collapsed = !collapsed">
|
|
70
|
+
{{ collapsed ? '展开' : '收起' }}
|
|
71
|
+
</ElButton>
|
|
72
|
+
</ElSpace>
|
|
73
|
+
</GridItem>
|
|
74
|
+
</Grid>
|
|
75
|
+
</ElForm>
|
|
76
|
+
</template>
|
|
77
|
+
|
|
78
|
+
<script lang="tsx" setup>
|
|
79
|
+
import type { FormInstance } from 'element-plus'
|
|
80
|
+
import type { FormColumnItem, FormColumnType, FormProps } from './type'
|
|
81
|
+
import { ArrowDown, ArrowUp } from '@element-plus/icons-vue'
|
|
82
|
+
import { ElButton, ElForm, ElFormItem, ElMessage, ElSpace, ElText } from 'element-plus'
|
|
83
|
+
import * as El from 'element-plus'
|
|
84
|
+
import {
|
|
85
|
+
computed,
|
|
86
|
+
getCurrentInstance,
|
|
87
|
+
onMounted,
|
|
88
|
+
ref,
|
|
89
|
+
toRaw,
|
|
90
|
+
useAttrs,
|
|
91
|
+
watch
|
|
92
|
+
} from 'vue'
|
|
93
|
+
import { useBemClass } from '../../../hooks'
|
|
94
|
+
import { Grid, GridItem } from '../../grid'
|
|
95
|
+
import InputSearch from '../../input-search'
|
|
96
|
+
|
|
97
|
+
const props = withDefaults(defineProps<FormProps>(), {
|
|
98
|
+
columns: () => [],
|
|
99
|
+
labelWidth: 'auto',
|
|
100
|
+
scrollToError: true,
|
|
101
|
+
showMessage: true,
|
|
102
|
+
gridItemProps: () => ({ span: { xs: 24, sm: 24, md: 12 } }), // xs, sm, md, lg, xl, xxl
|
|
103
|
+
search: false,
|
|
104
|
+
searchText: '查询',
|
|
105
|
+
hideFoldBtn: false,
|
|
106
|
+
defaultCollapsed: undefined,
|
|
107
|
+
fc: () => ({})
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
const emit = defineEmits<{
|
|
111
|
+
(e: 'update:modelValue', value: any): void
|
|
112
|
+
(e: 'search'): void
|
|
113
|
+
(e: 'reset'): void
|
|
114
|
+
}>()
|
|
115
|
+
|
|
116
|
+
const attrs = useAttrs()
|
|
117
|
+
const { b } = useBemClass()
|
|
118
|
+
const collapsed = ref(props?.defaultCollapsed ?? props.search)
|
|
119
|
+
const instance = getCurrentInstance()
|
|
120
|
+
|
|
121
|
+
const globalConfig = instance?.appContext.config.globalProperties.$config
|
|
122
|
+
const clearable = globalConfig?.clearable ?? false
|
|
123
|
+
// 字典数据存储
|
|
124
|
+
const dictData = ref<Record<string, any[]>>({})
|
|
125
|
+
|
|
126
|
+
/** 组件静态配置 */
|
|
127
|
+
const STATIC_PROPS = new Map([
|
|
128
|
+
['input', { clearable, maxlength: 20 }],
|
|
129
|
+
[
|
|
130
|
+
'textarea',
|
|
131
|
+
{ clearable, type: 'textarea', maxlength: 200, showWordLimit: true }
|
|
132
|
+
],
|
|
133
|
+
['input-number', {}],
|
|
134
|
+
['input-tag', { clearable }],
|
|
135
|
+
['select', { clearable }],
|
|
136
|
+
['select-v2', { clearable }],
|
|
137
|
+
['tree-select', { clearable }],
|
|
138
|
+
['cascader', { clearable }],
|
|
139
|
+
['slider', {}],
|
|
140
|
+
['switch', {}],
|
|
141
|
+
['rate', {}],
|
|
142
|
+
['checkbox-group', {}],
|
|
143
|
+
['checkbox', {}],
|
|
144
|
+
['radio-group', {}],
|
|
145
|
+
['radio', {}],
|
|
146
|
+
['date-picker', { clearable }],
|
|
147
|
+
['time-picker', { clearable }],
|
|
148
|
+
['time-select', { clearable }],
|
|
149
|
+
['color-picker', {}],
|
|
150
|
+
['transfer', {}],
|
|
151
|
+
['autocomplete', {}],
|
|
152
|
+
['upload', {}],
|
|
153
|
+
['title', {}]
|
|
154
|
+
])
|
|
155
|
+
|
|
156
|
+
/** 占位符文本映射 */
|
|
157
|
+
const PLACEHOLDER_MAP = new Map<FormColumnType, (label?: string) => string | undefined>([
|
|
158
|
+
['input', (label) => `请输入${label}`],
|
|
159
|
+
['input-number', (label) => `请输入${label}`],
|
|
160
|
+
['input-tag', (label) => `请输入${label}`],
|
|
161
|
+
['textarea', (label) => `请填写${label}`],
|
|
162
|
+
['select', (label) => `请选择${label}`],
|
|
163
|
+
['select-v2', (label) => `请选择${label}`],
|
|
164
|
+
['tree-select', (label) => `请选择${label}`],
|
|
165
|
+
['cascader', (label) => `请选择${label}`],
|
|
166
|
+
['time-select', (label) => `请选择${label}`],
|
|
167
|
+
['input-search', (label) => `请选择${label}`],
|
|
168
|
+
['date-picker', () => '请选择日期'],
|
|
169
|
+
['time-picker', () => '请选择时间']
|
|
170
|
+
])
|
|
171
|
+
|
|
172
|
+
/** 获取字典数据 */
|
|
173
|
+
const loadDictData = async () => {
|
|
174
|
+
const dictCodes = props.columns
|
|
175
|
+
?.filter((item) => item.dictCode)
|
|
176
|
+
.map((item) => item.dictCode!)
|
|
177
|
+
.filter((code, index, arr) => arr.indexOf(code) === index) // 去重
|
|
178
|
+
|
|
179
|
+
if (!dictCodes?.length) return
|
|
180
|
+
if (!globalConfig?.dictRequest) {
|
|
181
|
+
ElMessage.error('请配置全局字典请求方法dictRequest')
|
|
182
|
+
return
|
|
183
|
+
}
|
|
184
|
+
try {
|
|
185
|
+
// 使用Promise.all并行处理所有字典请求
|
|
186
|
+
const dictResponses = await Promise.all(
|
|
187
|
+
dictCodes.map((code) =>
|
|
188
|
+
globalConfig
|
|
189
|
+
.dictRequest(code)
|
|
190
|
+
.then((response: any) => ({ code, response }))
|
|
191
|
+
.catch((error: any) => {
|
|
192
|
+
console.error(`获取字典 ${code} 失败:`, error)
|
|
193
|
+
return { code, response: [] }
|
|
194
|
+
})
|
|
195
|
+
)
|
|
196
|
+
)
|
|
197
|
+
// 处理所有响应结果
|
|
198
|
+
dictResponses.forEach(({ code, response }) => {
|
|
199
|
+
dictData.value[code] = response
|
|
200
|
+
})
|
|
201
|
+
} catch (error) {
|
|
202
|
+
console.error('获取字典数据失败:', error)
|
|
203
|
+
ElMessage.error('获取字典数据失败')
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// 组件挂载时获取字典数据
|
|
208
|
+
onMounted(() => {
|
|
209
|
+
loadDictData()
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
/** 获取占位文本 */
|
|
213
|
+
const getPlaceholder = (item: FormColumnItem): string | undefined => {
|
|
214
|
+
if (!item.type) return undefined
|
|
215
|
+
const placeholderFn = PLACEHOLDER_MAP.get(item.type)
|
|
216
|
+
return placeholderFn ? placeholderFn(item.label) : undefined
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** 组件的默认props配置 */
|
|
220
|
+
function getComponentBindProps(item: FormColumnItem) {
|
|
221
|
+
// 获取默认配置
|
|
222
|
+
const defaultProps: Record<string, any> = { ...(STATIC_PROPS.get(item.type) || {}) }
|
|
223
|
+
const placeholder = getPlaceholder(item)
|
|
224
|
+
if (placeholder) {
|
|
225
|
+
defaultProps.placeholder = placeholder
|
|
226
|
+
}
|
|
227
|
+
// 日期选择器格式化
|
|
228
|
+
if (item.type === 'date-picker') {
|
|
229
|
+
defaultProps.valueFormat = item?.props?.type === 'datetime' ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD'
|
|
230
|
+
}
|
|
231
|
+
// 如果配置了dictCode且存在对应的字典数据,设置options
|
|
232
|
+
if (item.dictCode && dictData.value[item.dictCode]) {
|
|
233
|
+
defaultProps.options = dictData.value[item.dictCode]
|
|
234
|
+
}
|
|
235
|
+
// 合并默认配置和自定义配置
|
|
236
|
+
return { ...defaultProps, ...(item?.props || {}) }
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const formProps = computed(() => {
|
|
240
|
+
const {
|
|
241
|
+
columns,
|
|
242
|
+
gridProps,
|
|
243
|
+
gridItemProps,
|
|
244
|
+
search,
|
|
245
|
+
searchText,
|
|
246
|
+
hideFoldBtn,
|
|
247
|
+
defaultCollapsed,
|
|
248
|
+
modelValue,
|
|
249
|
+
fc,
|
|
250
|
+
...restProps
|
|
251
|
+
} = props
|
|
252
|
+
return { ...attrs, ...restProps }
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
const getClass = computed(() => {
|
|
256
|
+
const arr: string[] = [b('form')]
|
|
257
|
+
if (props.search) {
|
|
258
|
+
arr.push(b('form--search'))
|
|
259
|
+
}
|
|
260
|
+
return arr.join(' ')
|
|
261
|
+
})
|
|
262
|
+
|
|
263
|
+
const CompMap: Record<Exclude<FormColumnType, 'slot'>, any> = {
|
|
264
|
+
'input': El.ElInput,
|
|
265
|
+
'textarea': El.ElInput,
|
|
266
|
+
'input-number': El.ElInputNumber,
|
|
267
|
+
'input-tag': El.ElInputTag,
|
|
268
|
+
'input-search': InputSearch,
|
|
269
|
+
'select': El.ElSelect,
|
|
270
|
+
'select-v2': El.ElSelectV2,
|
|
271
|
+
'tree-select': El.ElTreeSelect,
|
|
272
|
+
'cascader': El.ElCascader,
|
|
273
|
+
'slider': El.ElSlider,
|
|
274
|
+
'switch': El.ElSwitch,
|
|
275
|
+
'rate': El.ElRate,
|
|
276
|
+
'checkbox-group': El.ElCheckboxGroup,
|
|
277
|
+
'checkbox': El.ElCheckbox,
|
|
278
|
+
'radio-group': El.ElRadioGroup,
|
|
279
|
+
'radio': El.ElRadio,
|
|
280
|
+
'date-picker': El.ElDatePicker,
|
|
281
|
+
'time-picker': El.ElTimePicker,
|
|
282
|
+
'time-select': El.ElTimeSelect,
|
|
283
|
+
'color-picker': El.ElColorPicker,
|
|
284
|
+
'transfer': El.ElTransfer,
|
|
285
|
+
'autocomplete': El.ElAutocomplete,
|
|
286
|
+
'upload': El.ElUpload,
|
|
287
|
+
'title': El.ElAlert
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const formRef = ref<FormInstance>()
|
|
291
|
+
|
|
292
|
+
/** 表单项校验规则 */
|
|
293
|
+
function getFormItemRules(item: FormColumnItem) {
|
|
294
|
+
const rules = Array.isArray(item.rules) ? item.rules : []
|
|
295
|
+
const requiredMessage = `${item.label}为必填项`
|
|
296
|
+
|
|
297
|
+
if (item.required) {
|
|
298
|
+
return [
|
|
299
|
+
{ required: true, message: requiredMessage },
|
|
300
|
+
...rules
|
|
301
|
+
]
|
|
302
|
+
}
|
|
303
|
+
if (props.fc?.[item.field]?.required) {
|
|
304
|
+
return [
|
|
305
|
+
{
|
|
306
|
+
required: props.fc[item.field].required,
|
|
307
|
+
message: requiredMessage
|
|
308
|
+
},
|
|
309
|
+
...rules
|
|
310
|
+
]
|
|
311
|
+
}
|
|
312
|
+
return item.rules
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** 隐藏表单项 */
|
|
316
|
+
function isHide(item: FormColumnItem): boolean {
|
|
317
|
+
if (typeof item.hide === 'boolean') return item.hide
|
|
318
|
+
if (typeof item.hide === 'function') {
|
|
319
|
+
return item.hide(props.modelValue)
|
|
320
|
+
}
|
|
321
|
+
if (props.fc?.[item.field]?.hidden) return true
|
|
322
|
+
return false
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** 禁用表单项 */
|
|
326
|
+
function isDisabled(item: FormColumnItem) {
|
|
327
|
+
if (item?.props?.disabled !== undefined) return item?.props?.disabled
|
|
328
|
+
if (props.fc?.[item.field]?.disabled === true) return true
|
|
329
|
+
return false
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/** 表单数据更新 */
|
|
333
|
+
function updateModelValue(value: any, item: FormColumnItem) {
|
|
334
|
+
emit('update:modelValue', { ...props.modelValue, [item.field]: value })
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
if (import.meta.env.DEV) {
|
|
338
|
+
watch(
|
|
339
|
+
() => props.modelValue,
|
|
340
|
+
() => {
|
|
341
|
+
// eslint-disable-next-line no-console
|
|
342
|
+
console.log('form', toRaw(props.modelValue))
|
|
343
|
+
},
|
|
344
|
+
{ deep: true }
|
|
345
|
+
)
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
defineExpose({ formRef })
|
|
349
|
+
</script>
|
|
350
|
+
|
|
351
|
+
<style lang="scss" scoped>
|
|
352
|
+
@use '../../../styles/var.scss' as a;
|
|
353
|
+
|
|
354
|
+
.el-form {
|
|
355
|
+
width: 100%;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
:deep(.el-form-item) {
|
|
359
|
+
align-items: center;
|
|
360
|
+
|
|
361
|
+
.el-form-item__label {
|
|
362
|
+
height: inherit;
|
|
363
|
+
line-height: inherit;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
:deep(.hide-label) {
|
|
368
|
+
|
|
369
|
+
// 隐藏el-form-item__label才能完整占满插槽宽度
|
|
370
|
+
.el-form-item__label {
|
|
371
|
+
display: none;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
.#{a.$prefix}-form {
|
|
376
|
+
&-item {
|
|
377
|
+
&__content {
|
|
378
|
+
width: 100%;
|
|
379
|
+
display: flex;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
&__component {
|
|
383
|
+
flex: 1;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
&__tip {
|
|
387
|
+
line-height: 1.5;
|
|
388
|
+
color: var(--el-color-info-light-3);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
&__extra {
|
|
392
|
+
margin-left: 6px;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
&__search-btns {
|
|
397
|
+
margin-bottom: 8px;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
.#{a.$prefix}-form--search {
|
|
402
|
+
:deep(.el-form-item) {
|
|
403
|
+
margin-bottom: 8px;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
:deep(.w-full) {
|
|
408
|
+
width: 100%;
|
|
409
|
+
|
|
410
|
+
.el-date-editor {
|
|
411
|
+
width: 100%;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
:deep(.#{a.$prefix}-form-item__title) {
|
|
416
|
+
border-radius: 0;
|
|
417
|
+
|
|
418
|
+
.el-alert__title {
|
|
419
|
+
color: var(--el-text-color-primary);
|
|
420
|
+
font-weight: 600;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
</style>
|