From d78c82f990695d8f2b254a71d4dbabe6d5886b7c Mon Sep 17 00:00:00 2001 From: liaoxuezhi <2betop.cn@gmail.com> Date: Wed, 23 Aug 2023 17:50:38 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=BC=B9=E7=AA=97?= =?UTF-8?q?=E4=B8=8B=E7=9B=B4=E6=8E=A5=E7=94=A8=20formitem=20=E4=B8=8D?= =?UTF-8?q?=E5=8C=85=E8=A3=B9=20form=20=E7=9A=84=E5=9C=BA=E6=99=AF?= =?UTF-8?q?=E4=B8=8B=E5=BC=B9=E7=AA=97=E6=8F=90=E4=BA=A4=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E4=B8=8D=E7=AC=A6=E5=90=88=E9=A2=84=E6=9C=9F=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20(#7913)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/amis-core/src/store/node.ts | 2 +- .../__snapshots__/dialog.test.tsx.snap | 28 +++---- .../__tests__/event-action/dialog.test.tsx | 82 ++++++++++++++++++- packages/amis/src/renderers/Dialog.tsx | 13 ++- 4 files changed, 103 insertions(+), 22 deletions(-) diff --git a/packages/amis-core/src/store/node.ts b/packages/amis-core/src/store/node.ts index 23ac261c2..35d584611 100644 --- a/packages/amis-core/src/store/node.ts +++ b/packages/amis-core/src/store/node.ts @@ -60,7 +60,7 @@ export const StoreNode = types callback?.(); } else if (!self.childrenIds.length) { const parent = self.parentStore; - parent?.onChildStoreDispose?.(self); + parent && isAlive(parent) && parent.onChildStoreDispose(self); destroy(self); callback?.(); // destroy(self); diff --git a/packages/amis/__tests__/event-action/__snapshots__/dialog.test.tsx.snap b/packages/amis/__tests__/event-action/__snapshots__/dialog.test.tsx.snap index f9a188a7f..b93248763 100644 --- a/packages/amis/__tests__/event-action/__snapshots__/dialog.test.tsx.snap +++ b/packages/amis/__tests__/event-action/__snapshots__/dialog.test.tsx.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`EventAction:dialog 1`] = ` +exports[`1. EventAction:dialog args 1`] = `
`; -exports[`EventAction:dialog 2`] = ` +exports[`1. EventAction:dialog args 2`] = `
`; -exports[`EventAction:dialog 3`] = ` +exports[`1. EventAction:dialog args 3`] = `
`; -exports[`EventAction:dialog 4`] = ` +exports[`1. EventAction:dialog args 4`] = `
`; -exports[`EventAction:dialog 5`] = ` +exports[`1. EventAction:dialog args 5`] = `
`; -exports[`EventAction:dialog 6`] = ` +exports[`1. EventAction:dialog args 6`] = `
`; -exports[`EventAction:dialog 7`] = ` +exports[`1. EventAction:dialog args 7`] = `
`; -exports[`EventAction:dialog args 1`] = ` +exports[`2. EventAction:dialog 1`] = `
`; -exports[`EventAction:dialog args 2`] = ` +exports[`2. EventAction:dialog 2`] = `
`; -exports[`EventAction:dialog args 3`] = ` +exports[`2. EventAction:dialog 3`] = `
`; -exports[`EventAction:dialog args 4`] = ` +exports[`2. EventAction:dialog 4`] = `
`; -exports[`EventAction:dialog args 5`] = ` +exports[`2. EventAction:dialog 5`] = `
`; -exports[`EventAction:dialog args 6`] = ` +exports[`2. EventAction:dialog 6`] = `
`; -exports[`EventAction:dialog args 7`] = ` +exports[`2. EventAction:dialog 7`] = `
{ +test('1. EventAction:dialog args', async () => { const notify = jest.fn(); const {getByText, container}: any = render( amisRender( @@ -220,7 +220,7 @@ test('EventAction:dialog args', async () => { expect(container).toMatchSnapshot(); }, 7000); -test('EventAction:dialog', async () => { +test('2. EventAction:dialog', async () => { const notify = jest.fn(); const {getByText, container}: any = render( amisRender( @@ -433,7 +433,7 @@ test('EventAction:dialog', async () => { expect(container).toMatchSnapshot(); }, 7000); -test('EventAction:dialog data', async () => { +test('3. EventAction:dialog data', async () => { const {getByText, container}: any = render( amisRender( { @@ -490,7 +490,7 @@ test('EventAction:dialog data', async () => { }); }, 7000); -test('EventAction:dialog data2', async () => { +test('4. EventAction:dialog data2', async () => { const {getByText, container}: any = render( amisRender( { @@ -639,3 +639,77 @@ test('EventAction:dialog data2', async () => { // // ); // // expect(confirm.mock.calls[0][1]).toEqual('操作确认'); // // }); + +test('5. EventAction:dialog formitem without form', async () => { + const onAction = jest.fn(); + const {getByText, container}: any = render( + amisRender( + { + type: 'page', + body: [ + { + type: 'button', + label: 'Dialog', + onEvent: { + click: { + actions: [ + { + actionType: 'dialog', + dialog: { + title: '表单页面', + body: [ + { + label: 'A', + type: 'input-text', + name: 'a' + }, + { + label: 'B', + type: 'input-text', + name: 'b' + } + ], + onEvent: { + confirm: { + actions: [ + { + actionType: 'custom', + script: onAction + } + ] + } + } + } + } + ] + } + } + } + ] + }, + {}, + makeEnv({ + getModalContainer: () => container + }) + ) + ); + + const button = getByText('Dialog'); + fireEvent.click(button); + await wait(200); + + fireEvent.change(container.querySelector('[name="a"]')!, { + target: {value: '1'} + }); + + await wait(200); + fireEvent.change(container.querySelector('[name="b"]')!, { + target: {value: '2'} + }); + + await wait(200); + fireEvent.click(getByText('确认')); + await wait(300); + expect(onAction).toHaveBeenCalled(); + expect(onAction.mock.calls[0][2].data).toMatchObject({a: '1', b: '2'}); +}); diff --git a/packages/amis/src/renderers/Dialog.tsx b/packages/amis/src/renderers/Dialog.tsx index 08dd5736e..46311f1e2 100644 --- a/packages/amis/src/renderers/Dialog.tsx +++ b/packages/amis/src/renderers/Dialog.tsx @@ -1,5 +1,10 @@ import React from 'react'; -import {ScopedContext, IScopedContext, filterTarget} from 'amis-core'; +import { + ScopedContext, + IScopedContext, + filterTarget, + setVariable +} from 'amis-core'; import {Renderer, RendererProps} from 'amis-core'; import {SchemaNode, Schema, ActionObject} from 'amis-core'; import {filter} from 'amis-core'; @@ -397,9 +402,11 @@ export default class Dialog extends React.Component { // 如果 dialog 里面不放 form,而是直接放表单项就会进到这里来。 if (typeof name === 'string') { - data = { - [name]: data + const mergedData = { + ...store.form }; + setVariable(mergedData, name, data); + data = mergedData; } store.setFormData(data);