diff --git a/__tests__/__snapshots__/factory.test.tsx.snap b/__tests__/__snapshots__/factory.test.tsx.snap index 056b32c7..488f973c 100644 --- a/__tests__/__snapshots__/factory.test.tsx.snap +++ b/__tests__/__snapshots__/factory.test.tsx.snap @@ -1,5 +1,13 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`factory custom loadRenderer 1`] = ` +
+
+ Not Found +
+
+`; + exports[`factory unregistered Renderer 1`] = `
{ +test('factory unregistered Renderer', async () => { const { container, } = render(amisRender({ type: 'my-renderer', a: 23 })); + await wait(100); + expect(container).toMatchSnapshot(); // not found +}); +test('factory custom loadRenderer', async () => { + const { + container, + } = render(amisRender({ + type: 'my-renderer', + a: 23 + }, {}, makeEnv({ + loadRenderer: () => Promise.resolve(() => (
Not Found
)) + }))); + await wait(100); expect(container).toMatchSnapshot(); // not found }); diff --git a/src/factory.tsx b/src/factory.tsx index a2a28b70..ebb58027 100644 --- a/src/factory.tsx +++ b/src/factory.tsx @@ -47,6 +47,7 @@ import Scoped from './Scoped'; import { getTheme, ThemeInstance, ClassNamesFn, ThemeContext } from "./theme"; import find = require("lodash/find"); import Alert from "./components/Alert2"; +import { LazyComponent } from './components'; export interface TestFunc { (path: string, schema?: object): boolean; @@ -79,6 +80,7 @@ export interface RendererEnv { affixOffsetTop: number; affixOffsetBottom: number; richTextToken: string; + loadRenderer: (schema:Schema, path:string) => Promise; [propName:string]: any; }; @@ -132,6 +134,7 @@ export interface RenderOptions { rendererResolver?: (path:string, schema:Schema, props:any) => null | RendererConfig; copy?: (contents:string) => void; getModalContainer?: () => HTMLElement; + loadRenderer?: (schema:Schema, path: string) => Promise; affixOffsetTop?: number; affixOffsetBottom?: number; richTextToken?: string; @@ -423,11 +426,9 @@ class SchemaRenderer extends React.Component { }, schema) : schema.children; } else if (!this.renderer) { return ( - -

Error: 找不到对应的渲染器

-

Path: {$path}

-
{JSON.stringify(schema, null, 2)}
-
+ rest.env.loadRenderer(schema, $path)} + /> ); } @@ -621,6 +622,15 @@ const defaultOptions:RenderOptions = { affixOffsetTop: 50, affixOffsetBottom: 0, richTextToken: '', + loadRenderer(schema, path) { + return Promise.resolve(() => ( + +

Error: 找不到对应的渲染器

+

Path: {path}

+
{JSON.stringify(schema, null, 2)}
+
+ )); + }, fetcher() { return Promise.reject('fetcher is required'); },