diff --git a/packages/amis-core/src/RootRenderer.tsx b/packages/amis-core/src/RootRenderer.tsx index 278ddbb05..840a6096e 100644 --- a/packages/amis-core/src/RootRenderer.tsx +++ b/packages/amis-core/src/RootRenderer.tsx @@ -26,18 +26,17 @@ export class RootRenderer extends React.Component { constructor(props: RootRendererProps) { super(props); - this.store = props.rootStore.addStore({ id: guid(), path: this.props.$path, storeType: RootStore.name, parentId: '' }) as IRootStore; - this.store.setContext(props.context); this.store.initData(props.data); this.store.updateLocation(props.location, this.props.env?.parseLocation); + // 将数据里面的函数批量的绑定到 this 上 bulkBindFunctions(this, [ 'handleAction', 'handleDialogConfirm', @@ -343,30 +342,118 @@ export class RootRenderer extends React.Component { scoped.reload(target, data); } + renderRuntimeError() { + const {render, ...rest} = this.props; + const {store} = this; + return render( + 'error', + { + type: 'alert', + level: 'danger' + }, + { + ...rest, + topStore: store, + body: ( + <> +

{store.runtimeError?.toString()}

+
+              {store.runtimeErrorStack.componentStack}
+            
+ + ) + } + ); + } + + renderSpinner() { + const {render, ...rest} = this.props; + const {store} = this; + return render( + 'spinner', + { + type: 'spinner' + }, + { + ...rest, + topStore: store, + show: store.loading + } + ); + } + + renderError() { + const {render, ...rest} = this.props; + const store = this.store; + return store.error + ? render( + 'error', + { + type: 'alert' + }, + { + ...rest, + topStore: this.store, + body: store.msg, + showCloseButton: true, + onClose: store.clearMessage + } + ) + : null; + } + + renderDialog() { + const {render, ...rest} = this.props; + const store = this.store; + return render( + 'dialog', + { + ...((store.action as ActionObject) && + ((store.action as ActionObject).dialog as object)), + type: 'dialog' + }, + { + ...rest, + key: 'dialog', + topStore: this.store, + data: store.dialogData, + onConfirm: this.handleDialogConfirm, + onClose: this.handleDialogClose, + show: store.dialogOpen, + onAction: this.handleAction + } + ); + } + + renderDrawer() { + const {render, ...rest} = this.props; + const store = this.store; + return render( + 'drawer', + { + ...((store.action as ActionObject) && + ((store.action as ActionObject).drawer as object)), + type: 'drawer' + }, + { + ...rest, + key: 'drawer', + topStore: this.store, + data: store.drawerData, + onConfirm: this.handleDrawerConfirm, + onClose: this.handleDrawerClose, + show: store.drawerOpen, + onAction: this.handleAction + } + ); + } + render() { const {pathPrefix, schema, render, ...rest} = this.props; const store = this.store; if (store.runtimeError) { - return render( - 'error', - { - type: 'alert', - level: 'danger' - }, - { - ...rest, - topStore: this.store, - body: ( - <> -

{this.store.runtimeError?.toString()}

-
-                {this.store.runtimeErrorStack.componentStack}
-              
- - ) - } - ); + this.renderRuntimeError(); } return ( @@ -380,71 +467,13 @@ export class RootRenderer extends React.Component { }) as JSX.Element } - {render( - 'spinner', - { - type: 'spinner' - }, - { - ...rest, - topStore: this.store, - show: store.loading - } - )} + {this.renderSpinner()} - {store.error - ? render( - 'error', - { - type: 'alert' - }, - { - ...rest, - topStore: this.store, - body: store.msg, - showCloseButton: true, - onClose: store.clearMessage - } - ) - : null} + {this.renderError()} - {render( - 'dialog', - { - ...((store.action as ActionObject) && - ((store.action as ActionObject).dialog as object)), - type: 'dialog' - }, - { - ...rest, - key: 'dialog', - topStore: this.store, - data: store.dialogData, - onConfirm: this.handleDialogConfirm, - onClose: this.handleDialogClose, - show: store.dialogOpen, - onAction: this.handleAction - } - )} + {this.renderDialog()} - {render( - 'drawer', - { - ...((store.action as ActionObject) && - ((store.action as ActionObject).drawer as object)), - type: 'drawer' - }, - { - ...rest, - key: 'drawer', - topStore: this.store, - data: store.drawerData, - onConfirm: this.handleDrawerConfirm, - onClose: this.handleDrawerClose, - show: store.drawerOpen, - onAction: this.handleAction - } - )} + {this.renderDrawer()} ); }