BasicForm.vue
7.29 KB
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
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
<script setup lang="ts">
import type { FormProps as AntFormProps, RowProps } from 'ant-design-vue'
import { Form, Row } from 'ant-design-vue'
import { cloneDeep } from 'lodash-es'
import { computed, onMounted, reactive, ref, unref, useAttrs, watch } from 'vue'
import type { FormExpose } from 'ant-design-vue/es/form/Form'
import FormItem from './components/FormItem.vue'
import { ComponentEnum, FormLayoutEnum } from './enum'
import { dateItemType } from './helper'
import type { BasicFormEmitType, FormActionType, FormProps, FormSchema } from './types/form'
import { BasicFormEventEnum } from './types/form'
import type { AdvanceState } from './types/hook'
import { useFormValues } from './hooks/useFormValues'
import type { UseFormEventsParamsType } from './hooks/useFormEvents'
import { useFormEvents } from './hooks/useFormEvents'
import { createFormContext } from './hooks/useFormContext'
import { useAutoFocus } from './hooks/useAutoFocus'
import { useAdvanced } from './hooks/useAdvanced'
import FormAction from './components/FormAction.vue'
import { FORM_ACTION_SLOTS, SlotNameEnum } from './slotHelp'
import { dateUtil } from '@/utils/dateUtil'
import { deepMerge } from '@/utils'
import { useDesign } from '@/hooks/useDesign'
import { DateFormatEnum } from '@/enums/timeEnum'
const props = withDefaults(
defineProps<FormProps>(),
{
model: () => ({}),
labelWidth: 0,
fieldMapToTime: () => [],
schemas: () => [],
mergetDynamicData: () => ({}),
autoSetPlaceholder: true,
autoSubmitOnEnter: false,
emptySpan: 0,
transformDateFunc: (date: any) => date?.format(DateFormatEnum.YYYY_MM_DD_HH_MM_SS) ?? date,
rulesMessageJoinLabel: true,
autoAdvancedLine: 3,
alwaysShowLines: 1,
showSubmitButton: true,
layout: FormLayoutEnum.HORIZONTAL,
placeholderJoinLabel: true,
},
)
const emit = defineEmits<BasicFormEmitType>()
const attrs = useAttrs()
const formModel = reactive<Recordable>({})
const advanceState = reactive<AdvanceState>({
isAdvanced: true,
hideAdvanceBtn: false,
isLoad: false,
actionSpan: 6,
})
const defaultValueRef = ref({})
const isInitedDefaultRef = ref(false)
const propsRef = ref<Partial<FormProps>>({})
const schemaRef = ref<Nullable<FormSchema[]>>(null)
const formElRef = ref<Nullable<FormExpose>>(null)
const { prefixCls } = useDesign('basic-form')
const getProps = computed(() => {
return { ...props, ...unref(propsRef) as FormProps } as FormProps
})
const getFormClass = computed(() => {
return [prefixCls, {
[`${prefixCls}--compact`]: unref(getProps).compact,
}]
})
const getRow = computed<RowProps>(() => {
const { baseRowStyle = {}, rowProps } = unref(getProps)
return {
style: baseRowStyle,
...rowProps,
}
})
const getBindValue = computed<AntFormProps>(() => ({ ...attrs, ...props, ...unref(getProps) }) as AntFormProps)
const getFormActionBindProps = computed(() => ({ ...getProps.value as Recordable, ...advanceState }))
const getSchema = computed<FormSchema[]>(() => {
const schemas: FormSchema[] = unref(schemaRef) || unref(getProps).schemas as any
for (const schema of schemas) {
const { defaultValue, component, isHandleDateDefaultValue = true } = schema
if (isHandleDateDefaultValue && defaultValue && dateItemType.includes(component)) {
if (!Array.isArray(defaultValue)) {
schema.defaultValue = dateUtil(defaultValue)
}
else {
const def: any[] = []
defaultValue.forEach((item) => {
def.push(dateUtil(item))
})
schema.defaultValue = def
}
}
}
if (unref(getProps).showAdvanceButton)
return cloneDeep(schemas.filter(schema => schema.component !== ComponentEnum.DIVIDER))
else
return cloneDeep(schemas)
})
const { handleToggleAdvanced, fieldsIsAdvancedMap } = useAdvanced({ advanceState, emit, getProps, getSchema, formModel, defaultValueRef })
const { handleFormValues, initDefault } = useFormValues({ getProps, defaultValueRef, getSchema, formModel })
useAutoFocus({ getSchema, getProps, isInitedDefault: isInitedDefaultRef, formElRef })
const {
handleSubmit,
clearValidate,
validate,
validateFields,
getFieldsValue,
updateSchema,
resetSchema,
appendSchemaByField,
removeSchemaByField,
resetFields,
setFieldsValue,
scrollToField,
} = useFormEvents({
emit,
getProps,
formModel,
getSchema,
defaultValueRef,
formElRef,
schemaRef,
handleFormValues,
} as UseFormEventsParamsType)
const formActionType: Partial<FormActionType> = {
getFieldsValue,
setFieldsValue,
resetFields,
updateSchema,
resetSchema,
setProps,
removeSchemaByField,
appendSchemaByField,
clearValidate,
validateFields,
validate,
submit: handleSubmit,
scrollToField,
}
createFormContext({
resetAction: resetFields!,
submitAction: handleSubmit,
})
watch(
() => unref(getProps).model,
() => {
const { model } = unref(getProps)
if (!model) return
setFieldsValue?.(model)
},
{
immediate: true,
},
)
watch(
() => unref(getSchema),
(schema) => {
if (unref(isInitedDefaultRef)) return
if (schema.length) {
initDefault()
isInitedDefaultRef.value = true
}
},
)
async function setProps(formProps: Partial<FormProps>) {
propsRef.value = deepMerge(unref(propsRef) || {}, formProps) as any
}
function setFormModel(key: string, value: any, schema: FormSchema) {
formModel[key] = value
emit(BasicFormEventEnum.FILE_VALUE_CHANGE, key, value)
if (schema && schema.itemProps && !schema.itemProps.autoLink)
validateFields?.([key]).catch(() => { })
}
function handleEnterPress(event: KeyboardEvent) {
const { autoSubmitOnEnter } = unref(getProps)
if (!autoSubmitOnEnter) return
if (event.key === 'Enter' && event.target && event.target instanceof HTMLElement) {
const target = event.target as HTMLElement
if (target && target.tagName && target.tagName.toUpperCase() === 'INPUT')
handleSubmit()
}
}
onMounted(() => {
initDefault()
emit(BasicFormEventEnum.REGISTER, formActionType as FormActionType)
})
defineExpose(formActionType)
</script>
<template>
<Form ref="formElRef" v-bind="getBindValue" :class="getFormClass" :model="formModel" @keypress.enter="handleEnterPress">
<Row v-bind="getRow">
<slot :name="SlotNameEnum.FORM_HEADER" />
<template v-for="schema in getSchema" :key="schema.field">
<FormItem
:is-advanced="fieldsIsAdvancedMap[schema.field]" :form-action-type="formActionType" :schema="schema"
:form-props="getProps" :all-default-values="defaultValueRef" :form-model="formModel"
:set-form-model="setFormModel"
>
<template v-for="item in Object.keys($slots)" #[item]="data">
<slot :name="item" v-bind="data || {}" />
</template>
</FormItem>
</template>
<FormAction v-bind="getFormActionBindProps" @toggle-advanced="handleToggleAdvanced">
<template v-for="item in FORM_ACTION_SLOTS" #[item]="data">
<slot :name="item" v-bind="data || {}" />
</template>
</FormAction>
<slot :name="SlotNameEnum.FORM_FOOTER" />
</Row>
</Form>
</template>
<style lang="less" scoped>
.@{namespace}-basic-form {
> :deep(.ant-row) {
>.ant-col {
width: 100%;
}
.ant-btn {
padding: 4px 15px;
}
.ant-switch {
border-radius: 100px;
}
.ant-input-number {
width: 100%;
}
}
}
</style>