diff --git a/docs/zh-CN/concepts/event-action.md b/docs/zh-CN/concepts/event-action.md index a13b55904..9141b9111 100644 --- a/docs/zh-CN/concepts/event-action.md +++ b/docs/zh-CN/concepts/event-action.md @@ -1034,10 +1034,11 @@ run action ajax > `< 1.8.0 及以下版本`,以下属性与 args 同级。 -| 属性名 | 类型 | 默认值 | 说明 | -| ------ | -------- | ------ | ----------------------------------------------------------------------------------------------------------------- | -| link | `string` | `link` | 用来指定跳转地址,跟 url 不同的是,这是单页跳转方式,不会渲染浏览器,请指定 amis 平台内的页面。可用 `${xxx}` 取值 | -| params | `object` | - | 页面参数`{key:value}`,支持数据映射,`> 1.9.0 及以上版本` | +| 属性名 | 类型 | 默认值 | 说明 | +| ---------- | -------- | ------ | ----------------------------------------------------------------------------------------------------------------------------- | +| link | `string` | `link` | 用来指定跳转地址,跟 url 不同的是,这是单页跳转方式,不会渲染浏览器,请指定 amis 平台内的页面。可用 `${xxx}` 取值 | +| params | `object` | - | 页面参数`{key:value}`,支持数据映射,`> 1.9.0 及以上版本` | +| targetType | `string` | `page` | 默认为内容区打开`page`,可设置为新窗口打开`blank`,当前页签打开`self`,`blank\|self` 方式会重新渲染浏览器`> 6.1.0 及以上版本` | ### 浏览器回退 diff --git a/packages/amis-core/src/actions/LinkAction.ts b/packages/amis-core/src/actions/LinkAction.ts index 930f59fba..567461bff 100644 --- a/packages/amis-core/src/actions/LinkAction.ts +++ b/packages/amis-core/src/actions/LinkAction.ts @@ -38,16 +38,23 @@ export class LinkAction implements RendererAction { throw new Error('env.jumpTo is required!'); } + let apiParams = { + ...(action.args?.params ?? {}), + ...(action.data ?? {}) + }; + + if (action?.actionType === 'link' && apiParams?.targetType) { + // link动作新增打开方式targetType,buildApi不需要该参数 + delete apiParams.targetType; + } + // 通过buildApi兼容较复杂的url情况 let urlObj = buildApi( { url: (action.args?.url || action.args?.link) as string, method: 'get' }, - { - ...(action.args?.params ?? {}), - ...(action.data ?? {}) - }, + apiParams, { autoAppend: true } diff --git a/packages/amis-core/src/factory.tsx b/packages/amis-core/src/factory.tsx index 3d5aa09bb..fc31e808a 100644 --- a/packages/amis-core/src/factory.tsx +++ b/packages/amis-core/src/factory.tsx @@ -294,6 +294,20 @@ export const defaultOptions: RenderOptions = { action.blank === false ? (window.location.href = to) : window.open(to); return; } + // link动作新增了targetType属性,默认是内容区打开(page),在新窗口打开(blank);在当前页签打开(self) + if ( + action?.actionType === 'link' && + ['blank', 'self'].includes(action?.targetType) + ) { + if (action.targetType === 'self') { + // 当前页签打开,需要刷新页面 + window.history.pushState(null, '', to); + location.reload(); + } else { + window.open(to); + } + return; + } if (/^https?:\/\//.test(to)) { window.location.replace(to); } else { diff --git a/packages/amis/__tests__/event-action/url.test.tsx b/packages/amis/__tests__/event-action/url.test.tsx index eb57b040c..3a3ac4f45 100644 --- a/packages/amis/__tests__/event-action/url.test.tsx +++ b/packages/amis/__tests__/event-action/url.test.tsx @@ -40,6 +40,33 @@ test('EventAction:url & link', async () => { ] } } + }, + { + type: 'button', + label: '打开', + level: 'primary', + className: 'ml-2', + onEvent: { + click: { + actions: [ + { + actionType: 'link', + args: { + link: './expression', + targetType: 'page', + params: { + name: 'jack', + jon: '${myjon}' + } + }, + data: { + name: '${myname}', + age: 18 + } + } + ] + } + } } ] }, @@ -51,6 +78,7 @@ test('EventAction:url & link', async () => { ); fireEvent.click(getByText('跳转')); + fireEvent.click(getByText('打开')); await waitFor(() => { expect(jumpTo).toHaveBeenCalled(); }); @@ -71,4 +99,7 @@ test('EventAction:url & link', async () => { age: 18, name: 'lvxj' }); + expect(jumpTo.mock.calls[1][0]).toEqual( + './expression?name=lvxj&jon=player&age=18' + ); });