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
exports[`factory custom loadRenderer 1`] = `
<div>
<div>
Not Found
</div>
</div>
`;
exports[`factory unregistered Renderer 1`] = `
<div>
<div

View File

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

View File

@ -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<React.ReactType>;
[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<React.ReactType>;
affixOffsetTop?: number;
affixOffsetBottom?: number;
richTextToken?: string;
@ -423,11 +426,9 @@ class SchemaRenderer extends React.Component<SchemaRendererProps, any> {
}, schema) : schema.children;
} else if (!this.renderer) {
return (
<Alert level="danger">
<p>Error: </p>
<p>Path: {$path}</p>
<pre><code>{JSON.stringify(schema, null, 2)}</code></pre>
</Alert>
<LazyComponent
getComponent={() => rest.env.loadRenderer(schema, $path)}
/>
);
}
@ -621,6 +622,15 @@ const defaultOptions:RenderOptions = {
affixOffsetTop: 50,
affixOffsetBottom: 0,
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() {
return Promise.reject('fetcher is required');
},