fix: fix the form-api reactive failure inside the form (#4590)

* fix: fix the form-api reactive failure inside the form
This commit is contained in:
Vben
2024-10-08 22:43:02 +08:00
committed by GitHub
parent 8ad2b8665d
commit 2dbd323b2a
12 changed files with 157 additions and 93 deletions

View File

@@ -34,6 +34,7 @@
"@vben/types": "workspace:*",
"@vben/utils": "workspace:*",
"@vueuse/core": "catalog:",
"dayjs": "catalog:",
"echarts": "catalog:",
"vue": "catalog:",
"vxe-pc-ui": "catalog:",

View File

@@ -0,0 +1,17 @@
import type { VxeUIExport } from 'vxe-table';
import dayjs from 'dayjs';
export function initDefaultFormatter(vxeUI: VxeUIExport) {
vxeUI.formats.add('formatDate', {
tableCellFormatMethod({ cellValue }) {
return dayjs(cellValue).format('YYYY-MM-DD');
},
});
vxeUI.formats.add('formatDateTime', {
tableCellFormatMethod({ cellValue }) {
return dayjs(cellValue).format('YYYY-MM-DD HH:mm:ss');
},
});
}

View File

@@ -41,6 +41,8 @@ import {
VxeToolbar,
} from 'vxe-table';
import { initDefaultFormatter } from './formatter';
// 是否加载过
let isInit = false;
@@ -118,5 +120,6 @@ export function setupVbenVxeTable(setupOptions: SetupVxeTable) {
},
);
initDefaultFormatter(VxeUI);
configVxeTable(VxeUI);
}

View File

@@ -14,6 +14,7 @@ import {
toRaw,
useSlots,
useTemplateRef,
watch,
} from 'vue';
import { usePriorityValues } from '@vben/hooks';
@@ -53,7 +54,27 @@ const { isMobile } = usePreferences();
const slots = useSlots();
const [Form, formApi] = useTableForm({});
const [Form, formApi] = useTableForm({
handleSubmit: async () => {
const formValues = formApi.form.values;
props.api.reload(formValues);
},
handleReset: async () => {
await formApi.resetForm();
const formValues = formApi.form.values;
props.api.reload(formValues);
},
commonConfig: {
componentProps: {
class: 'w-full',
},
},
showCollapseButton: true,
submitButtonOptions: {
content: $t('common.query'),
},
wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3',
});
const showToolbar = computed(() => {
return !!slots['toolbar-actions']?.() || !!slots['toolbar-tools']?.();
@@ -136,40 +157,6 @@ const events = computed(() => {
};
});
const vbenFormOptions = computed(() => {
const defaultFormProps: VbenFormProps = {
handleSubmit: async () => {
const formValues = formApi.form.values;
props.api.reload(formValues);
},
handleReset: async () => {
await formApi.resetForm();
const formValues = formApi.form.values;
props.api.reload(formValues);
},
commonConfig: {
componentProps: {
class: 'w-full',
},
},
showCollapseButton: true,
submitButtonOptions: {
content: $t('common.query'),
},
wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3',
};
const finalFormOptions: VbenFormProps = mergeWithArrayOverride(
{},
formOptions.value,
defaultFormProps,
);
return {
...finalFormOptions,
collapseTriggerResize: !!finalFormOptions.showCollapseButton,
};
});
const delegatedSlots = computed(() => {
const resultSlots: string[] = [];
@@ -215,6 +202,27 @@ async function init() {
);
}
}
watch(
formOptions,
() => {
formApi.setState((prev) => {
const finalFormOptions: VbenFormProps = mergeWithArrayOverride(
{},
formOptions.value,
prev,
);
return {
...finalFormOptions,
collapseTriggerResize: !!finalFormOptions.showCollapseButton,
};
});
},
{
immediate: true,
},
);
onMounted(() => {
props.api?.mount?.(gridRef.value, formApi);
init();
@@ -247,7 +255,7 @@ onMounted(() => {
<template #form>
<div v-if="formOptions" class="relative rounded py-3 pb-4">
<slot name="form">
<Form v-bind="vbenFormOptions">
<Form>
<template
v-for="slotName in delegatedFormSlots"
:key="slotName"