add custom loadRenderer

This commit is contained in:
liaoxuezhi 2019-05-18 00:50:23 +08:00
parent c27a13e0af
commit 8d329c4c9a
3 changed files with 38 additions and 6 deletions

View File

@ -1,5 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`factory custom loadRenderer 1`] = `
<div>
<div>
Not Found
</div>
</div>
`;
exports[`factory unregistered Renderer 1`] = ` exports[`factory unregistered Renderer 1`] = `
<div> <div>
<div <div

View File

@ -6,15 +6,29 @@ import {
} from '../src/factory'; } from '../src/factory';
import React = require('react'); import React = require('react');
import {render, fireEvent, cleanup} from 'react-testing-library'; import {render, fireEvent, cleanup} from 'react-testing-library';
import { wait, makeEnv } from './helper';
test('factory unregistered Renderer', () => { test('factory unregistered Renderer', async () => {
const { const {
container, container,
} = render(amisRender({ } = render(amisRender({
type: 'my-renderer', type: 'my-renderer',
a: 23 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(() => (<div>Not Found</div>))
})));
await wait(100);
expect(container).toMatchSnapshot(); // not found expect(container).toMatchSnapshot(); // not found
}); });

View File

@ -47,6 +47,7 @@ import Scoped from './Scoped';
import { getTheme, ThemeInstance, ClassNamesFn, ThemeContext } from "./theme"; import { getTheme, ThemeInstance, ClassNamesFn, ThemeContext } from "./theme";
import find = require("lodash/find"); import find = require("lodash/find");
import Alert from "./components/Alert2"; import Alert from "./components/Alert2";
import { LazyComponent } from './components';
export interface TestFunc { export interface TestFunc {
(path: string, schema?: object): boolean; (path: string, schema?: object): boolean;
@ -79,6 +80,7 @@ export interface RendererEnv {
affixOffsetTop: number; affixOffsetTop: number;
affixOffsetBottom: number; affixOffsetBottom: number;
richTextToken: string; richTextToken: string;
loadRenderer: (schema:Schema, path:string) => Promise<React.ReactType>;
[propName:string]: any; [propName:string]: any;
}; };
@ -132,6 +134,7 @@ export interface RenderOptions {
rendererResolver?: (path:string, schema:Schema, props:any) => null | RendererConfig; rendererResolver?: (path:string, schema:Schema, props:any) => null | RendererConfig;
copy?: (contents:string) => void; copy?: (contents:string) => void;
getModalContainer?: () => HTMLElement; getModalContainer?: () => HTMLElement;
loadRenderer?: (schema:Schema, path: string) => Promise<React.ReactType>;
affixOffsetTop?: number; affixOffsetTop?: number;
affixOffsetBottom?: number; affixOffsetBottom?: number;
richTextToken?: string; richTextToken?: string;
@ -423,11 +426,9 @@ class SchemaRenderer extends React.Component<SchemaRendererProps, any> {
}, schema) : schema.children; }, schema) : schema.children;
} else if (!this.renderer) { } else if (!this.renderer) {
return ( return (
<Alert level="danger"> <LazyComponent
<p>Error: </p> getComponent={() => rest.env.loadRenderer(schema, $path)}
<p>Path: {$path}</p> />
<pre><code>{JSON.stringify(schema, null, 2)}</code></pre>
</Alert>
); );
} }
@ -621,6 +622,15 @@ const defaultOptions:RenderOptions = {
affixOffsetTop: 50, affixOffsetTop: 50,
affixOffsetBottom: 0, affixOffsetBottom: 0,
richTextToken: '', richTextToken: '',
loadRenderer(schema, path) {
return Promise.resolve(() => (
<Alert level="danger">
<p>Error: </p>
<p>Path: {path}</p>
<pre><code>{JSON.stringify(schema, null, 2)}</code></pre>
</Alert>
));
},
fetcher() { fetcher() {
return Promise.reject('fetcher is required'); return Promise.reject('fetcher is required');
}, },