fix(tmagic-form): runtime刷新导致root丢失

This commit is contained in:
roymondchen 2025-12-09 16:18:35 +08:00
parent 83664cd440
commit d039cee913
3 changed files with 22 additions and 6 deletions

View File

@ -1,5 +1,5 @@
{
"version": "1.1.3",
"version": "1.1.4",
"name": "@tmagic/tmagic-form-runtime",
"type": "module",
"main": "dist/tmagic-form-runtime.umd.cjs",

View File

@ -12,7 +12,7 @@ import { useFormConfig } from './useFormConfig';
const props = defineProps<AppProps>();
const { mForm, formConfig, config, values } = useFormConfig(props);
const { formConfig, config, values } = useFormConfig(props);
watch(formConfig, async () => {
setTimeout(() => {

View File

@ -73,8 +73,15 @@ export const useFormConfig = (props: AppProps) => {
return nextTick().then(() => getElById()(document, `${id}`) as HTMLElement);
},
add({ config, parentId }: UpdateData) {
if (!root.value) throw new Error('error');
add({ config, parentId, root: appConfig }: UpdateData) {
if (!root.value) {
if (appConfig) {
root.value = appConfig;
resetValues();
return;
}
throw new Error('error');
}
if (!selectedId.value) throw new Error('error');
if (!parentId) throw new Error('error');
@ -97,8 +104,17 @@ export const useFormConfig = (props: AppProps) => {
resetValues();
},
update({ config, parentId }: UpdateData) {
if (!root.value || !app) throw new Error('error');
update({ config, parentId, root: appConfig }: UpdateData) {
if (!root.value) {
if (appConfig) {
root.value = appConfig;
resetValues();
return;
}
throw new Error('error');
}
if (!app) throw new Error('error');
const newNode = app.dataSourceManager?.compiledNode(config) || config;
replaceChildNode(reactive(newNode), [root.value], parentId);