提交 | 用户 | 时间
|
314507
|
1 |
import type { Slots } from 'vue' |
H |
2 |
import { getSlot } from '@/utils/tsxHelper' |
|
3 |
import { PlaceholderModel } from './types' |
|
4 |
import { FormSchema } from '@/types/form' |
|
5 |
import { ColProps } from '@/types/components' |
|
6 |
|
|
7 |
/** |
|
8 |
* |
|
9 |
* @param schema 对应组件数据 |
|
10 |
* @returns 返回提示信息对象 |
|
11 |
* @description 用于自动设置placeholder |
|
12 |
*/ |
|
13 |
export const setTextPlaceholder = (schema: FormSchema): PlaceholderModel => { |
|
14 |
const { t } = useI18n() |
|
15 |
const textMap = ['Input', 'Autocomplete', 'InputNumber', 'InputPassword'] |
|
16 |
const selectMap = ['Select', 'SelectV2', 'TimePicker', 'DatePicker', 'TimeSelect', 'TimeSelect'] |
|
17 |
if (textMap.includes(schema?.component as string)) { |
|
18 |
return { |
|
19 |
placeholder: t('common.inputText') + schema.label |
|
20 |
} |
|
21 |
} |
|
22 |
if (selectMap.includes(schema?.component as string)) { |
|
23 |
// 一些范围选择器 |
|
24 |
const twoTextMap = ['datetimerange', 'daterange', 'monthrange', 'datetimerange', 'daterange'] |
|
25 |
if ( |
|
26 |
twoTextMap.includes( |
|
27 |
(schema?.componentProps?.type || schema?.componentProps?.isRange) as string |
|
28 |
) |
|
29 |
) { |
|
30 |
return { |
|
31 |
startPlaceholder: t('common.startTimeText'), |
|
32 |
endPlaceholder: t('common.endTimeText'), |
|
33 |
rangeSeparator: '-' |
|
34 |
} |
|
35 |
} else { |
|
36 |
return { |
|
37 |
placeholder: t('common.selectText') + schema.label |
|
38 |
} |
|
39 |
} |
|
40 |
} |
|
41 |
return {} |
|
42 |
} |
|
43 |
|
|
44 |
/** |
|
45 |
* |
|
46 |
* @param col 内置栅格 |
|
47 |
* @returns 返回栅格属性 |
|
48 |
* @description 合并传入进来的栅格属性 |
|
49 |
*/ |
|
50 |
export const setGridProp = (col: ColProps = {}): ColProps => { |
|
51 |
const colProps: ColProps = { |
|
52 |
// 如果有span,代表用户优先级更高,所以不需要默认栅格 |
|
53 |
...(col.span |
|
54 |
? {} |
|
55 |
: { |
|
56 |
xs: 24, |
|
57 |
sm: 12, |
|
58 |
md: 12, |
|
59 |
lg: 12, |
|
60 |
xl: 12 |
|
61 |
}), |
|
62 |
...col |
|
63 |
} |
|
64 |
return colProps |
|
65 |
} |
|
66 |
|
|
67 |
/** |
|
68 |
* |
|
69 |
* @param item 传入的组件属性 |
|
70 |
* @returns 默认添加 clearable 属性 |
|
71 |
*/ |
|
72 |
export const setComponentProps = (item: FormSchema): Recordable => { |
|
73 |
const notNeedClearable = ['ColorPicker'] |
|
74 |
const componentProps: Recordable = notNeedClearable.includes(item.component as string) |
|
75 |
? { ...item.componentProps } |
|
76 |
: { |
|
77 |
clearable: true, |
|
78 |
...item.componentProps |
|
79 |
} |
|
80 |
// 需要删除额外的属性 |
|
81 |
delete componentProps?.slots |
|
82 |
return componentProps |
|
83 |
} |
|
84 |
|
|
85 |
/** |
|
86 |
* |
|
87 |
* @param slots 插槽 |
|
88 |
* @param slotsProps 插槽属性 |
|
89 |
* @param field 字段名 |
|
90 |
*/ |
|
91 |
export const setItemComponentSlots = ( |
|
92 |
slots: Slots, |
|
93 |
slotsProps: Recordable = {}, |
|
94 |
field: string |
|
95 |
): Recordable => { |
|
96 |
const slotObj: Recordable = {} |
|
97 |
for (const key in slotsProps) { |
|
98 |
if (slotsProps[key]) { |
|
99 |
// 由于组件有可能重复,需要有一个唯一的前缀 |
|
100 |
slotObj[key] = (data: Recordable) => { |
|
101 |
return getSlot(slots, `${field}-${key}`, data) |
|
102 |
} |
|
103 |
} |
|
104 |
} |
|
105 |
return slotObj |
|
106 |
} |
|
107 |
|
|
108 |
/** |
|
109 |
* |
|
110 |
* @param schema Form表单结构化数组 |
|
111 |
* @param formModel FormModel |
|
112 |
* @returns FormModel |
|
113 |
* @description 生成对应的formModel |
|
114 |
*/ |
|
115 |
export const initModel = (schema: FormSchema[], formModel: Recordable) => { |
|
116 |
const model: Recordable = { ...formModel } |
|
117 |
schema.map((v) => { |
|
118 |
// 如果是hidden,就删除对应的值 |
|
119 |
if (v.hidden) { |
|
120 |
delete model[v.field] |
|
121 |
} else if (v.component && v.component !== 'Divider') { |
|
122 |
const hasField = Reflect.has(model, v.field) |
|
123 |
// 如果先前已经有值存在,则不进行重新赋值,而是采用现有的值 |
|
124 |
model[v.field] = hasField ? model[v.field] : v.value !== void 0 ? v.value : '' |
|
125 |
} |
|
126 |
}) |
|
127 |
return model |
|
128 |
} |
|
129 |
|
|
130 |
/** |
|
131 |
* @param slots 插槽 |
|
132 |
* @param field 字段名 |
|
133 |
* @returns 返回FormIiem插槽 |
|
134 |
*/ |
|
135 |
export const setFormItemSlots = (slots: Slots, field: string): Recordable => { |
|
136 |
const slotObj: Recordable = {} |
|
137 |
if (slots[`${field}-error`]) { |
|
138 |
slotObj['error'] = (data: Recordable) => { |
|
139 |
return getSlot(slots, `${field}-error`, data) |
|
140 |
} |
|
141 |
} |
|
142 |
if (slots[`${field}-label`]) { |
|
143 |
slotObj['label'] = (data: Recordable) => { |
|
144 |
return getSlot(slots, `${field}-label`, data) |
|
145 |
} |
|
146 |
} |
|
147 |
return slotObj |
|
148 |
} |