| | |
| | | <template> |
| | | <Form v-bind="{ ...$attrs, ...$props }" :class="getFormClass" ref="formElRef" :model="formModel"> |
| | | <Row :style="getRowWrapStyle"> |
| | | <slot name="formHeader" /> |
| | | <Form |
| | | v-bind="{ ...$attrs, ...$props, ...getProps }" |
| | | :class="getFormClass" |
| | | ref="formElRef" |
| | | :model="formModel" |
| | | @keypress.enter="handleEnterPress" |
| | | > |
| | | <Row v-bind="{ ...getRow }"> |
| | | <slot name="formHeader"></slot> |
| | | <template v-for="schema in getSchema" :key="schema.field"> |
| | | <FormItem |
| | | :tableAction="tableAction" |
| | |
| | | :setFormModel="setFormModel" |
| | | > |
| | | <template #[item]="data" v-for="item in Object.keys($slots)"> |
| | | <slot :name="item" v-bind="data" /> |
| | | <slot :name="item" v-bind="data"></slot> |
| | | </template> |
| | | </FormItem> |
| | | </template> |
| | | |
| | | <FormAction |
| | | v-bind="{ ...getProps, ...advanceState }" |
| | | @toggle-advanced="handleToggleAdvanced" |
| | | /> |
| | | <slot name="formFooter" /> |
| | | <FormAction v-bind="{ ...getProps, ...advanceState }" @toggle-advanced="handleToggleAdvanced"> |
| | | <template |
| | | #[item]="data" |
| | | v-for="item in ['resetBefore', 'submitBefore', 'advanceBefore', 'advanceAfter']" |
| | | > |
| | | <slot :name="item" v-bind="data"></slot> |
| | | </template> |
| | | </FormAction> |
| | | <slot name="formFooter"></slot> |
| | | </Row> |
| | | </Form> |
| | | </template> |
| | | <script lang="ts"> |
| | | import type { FormActionType, FormProps, FormSchema } from './types/form'; |
| | | import type { AdvanceState } from './types/hooks'; |
| | | import type { CSSProperties, Ref, WatchStopHandle } from 'vue'; |
| | | import type { CSSProperties, Ref } from 'vue'; |
| | | |
| | | import { defineComponent, reactive, ref, computed, unref, onMounted, watch, toRefs } from 'vue'; |
| | | import { defineComponent, reactive, ref, computed, unref, onMounted, watch, nextTick } from 'vue'; |
| | | import { Form, Row } from 'ant-design-vue'; |
| | | import FormItem from './components/FormItem'; |
| | | import FormItem from './components/FormItem.vue'; |
| | | import FormAction from './components/FormAction.vue'; |
| | | |
| | | import { dateItemType } from './helper'; |
| | | import moment from 'moment'; |
| | | import { cloneDeep } from 'lodash-es'; |
| | | import { dateUtil } from '/@/utils/dateUtil'; |
| | | |
| | | // import { cloneDeep } from 'lodash-es'; |
| | | import { deepMerge } from '/@/utils'; |
| | | |
| | | import { useFormValues } from './hooks/useFormValues'; |
| | |
| | | import { useFormEvents } from './hooks/useFormEvents'; |
| | | import { createFormContext } from './hooks/useFormContext'; |
| | | import { useAutoFocus } from './hooks/useAutoFocus'; |
| | | import { useModalContext } from '/@/components/Modal'; |
| | | |
| | | import { basicProps } from './props'; |
| | | import { useDesign } from '/@/hooks/web/useDesign'; |
| | | |
| | | import type { RowProps } from 'ant-design-vue/lib/grid/Row'; |
| | | |
| | | export default defineComponent({ |
| | | name: 'BasicForm', |
| | |
| | | emits: ['advanced-change', 'reset', 'submit', 'register'], |
| | | setup(props, { emit }) { |
| | | const formModel = reactive<Recordable>({}); |
| | | const modalFn = useModalContext(); |
| | | |
| | | const advanceState = reactive<AdvanceState>({ |
| | | isAdvanced: true, |
| | |
| | | const { prefixCls } = useDesign('basic-form'); |
| | | |
| | | // Get the basic configuration of the form |
| | | const getProps = computed( |
| | | (): FormProps => { |
| | | return deepMerge(cloneDeep(props), unref(propsRef)); |
| | | } |
| | | ); |
| | | const getProps = computed((): FormProps => { |
| | | return { ...props, ...unref(propsRef) } as FormProps; |
| | | }); |
| | | |
| | | const getFormClass = computed(() => { |
| | | return [ |
| | |
| | | ]; |
| | | }); |
| | | |
| | | // Get uniform row style |
| | | const getRowWrapStyle = computed( |
| | | (): CSSProperties => { |
| | | const { baseRowStyle = {} } = unref(getProps); |
| | | return baseRowStyle; |
| | | } |
| | | ); |
| | | // Get uniform row style and Row configuration for the entire form |
| | | const getRow = computed((): CSSProperties | RowProps => { |
| | | const { baseRowStyle = {}, rowProps } = unref(getProps); |
| | | return { |
| | | style: baseRowStyle, |
| | | ...rowProps, |
| | | }; |
| | | }); |
| | | |
| | | const getSchema = computed((): FormSchema[] => { |
| | | const schemas: FormSchema[] = unref(schemaRef) || (unref(getProps).schemas as any); |
| | |
| | | // handle date type |
| | | if (defaultValue && dateItemType.includes(component)) { |
| | | if (!Array.isArray(defaultValue)) { |
| | | schema.defaultValue = moment(defaultValue); |
| | | schema.defaultValue = dateUtil(defaultValue); |
| | | } else { |
| | | const def: moment.Moment[] = []; |
| | | defaultValue.forEach((item) => { |
| | | def.push(moment(item)); |
| | | def.push(dateUtil(item)); |
| | | }); |
| | | schema.defaultValue = def; |
| | | } |
| | |
| | | defaultValueRef, |
| | | }); |
| | | |
| | | const { transformDateFunc, fieldMapToTime, autoFocusFirstItem } = toRefs(props); |
| | | |
| | | const { handleFormValues, initDefault } = useFormValues({ |
| | | transformDateFuncRef: transformDateFunc, |
| | | fieldMapToTimeRef: fieldMapToTime, |
| | | getProps, |
| | | defaultValueRef, |
| | | getSchema, |
| | | formModel, |
| | |
| | | |
| | | useAutoFocus({ |
| | | getSchema, |
| | | autoFocusFirstItem, |
| | | getProps, |
| | | isInitedDefault: isInitedDefaultRef, |
| | | formElRef: formElRef as Ref<FormActionType>, |
| | | }); |
| | |
| | | validateFields, |
| | | getFieldsValue, |
| | | updateSchema, |
| | | resetSchema, |
| | | appendSchemaByField, |
| | | removeSchemaByFiled, |
| | | resetFields, |
| | |
| | | } |
| | | ); |
| | | |
| | | const stopWatch: WatchStopHandle = watch( |
| | | watch( |
| | | () => getSchema.value, |
| | | (schema) => { |
| | | nextTick(() => { |
| | | // Solve the problem of modal adaptive height calculation when the form is placed in the modal |
| | | modalFn?.redoModalHeight?.(); |
| | | }); |
| | | if (unref(isInitedDefaultRef)) { |
| | | return stopWatch(); |
| | | return; |
| | | } |
| | | if (schema?.length) { |
| | | initDefault(); |
| | |
| | | formModel[key] = value; |
| | | } |
| | | |
| | | function handleEnterPress(e: KeyboardEvent) { |
| | | const { autoSubmitOnEnter } = unref(getProps); |
| | | if (!autoSubmitOnEnter) return; |
| | | if (e.key === 'Enter' && e.target && e.target instanceof HTMLElement) { |
| | | const target: HTMLElement = e.target as HTMLElement; |
| | | if (target && target.tagName && target.tagName.toUpperCase() == 'INPUT') { |
| | | handleSubmit(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | const formActionType: Partial<FormActionType> = { |
| | | getFieldsValue, |
| | | setFieldsValue, |
| | | resetFields, |
| | | updateSchema, |
| | | resetSchema, |
| | | setProps, |
| | | removeSchemaByFiled, |
| | | appendSchemaByField, |
| | |
| | | |
| | | return { |
| | | handleToggleAdvanced, |
| | | handleEnterPress, |
| | | formModel, |
| | | defaultValueRef, |
| | | advanceState, |
| | | getRowWrapStyle, |
| | | getRow, |
| | | getProps, |
| | | formElRef, |
| | | getSchema, |
| | |
| | | }); |
| | | </script> |
| | | <style lang="less"> |
| | | @import (reference) '../../../design/index.less'; |
| | | @prefix-cls: ~'@{namespace}-basic-form'; |
| | | |
| | | .@{prefix-cls} { |
| | |
| | | display: flex; |
| | | } |
| | | |
| | | .ant-form-item-control { |
| | | margin-top: 4px; |
| | | } |
| | | |
| | | .suffix { |
| | | display: inline-block; |
| | | display: inline-flex; |
| | | padding-left: 6px; |
| | | margin-top: 1px; |
| | | line-height: 1; |
| | | align-items: center; |
| | | } |
| | | } |
| | | } |