schema 中的事件与渲染器中下发的事件串起来

This commit is contained in:
2betop 2020-01-03 12:04:19 +08:00
parent ff893806a3
commit 10df6f2a71
5 changed files with 42 additions and 35 deletions

View File

@ -2,7 +2,7 @@ import React from 'react';
import qs from 'qs';
import {RendererStore, IRendererStore, IIRendererStore} from './store/index';
import {getEnv} from 'mobx-state-tree';
import {Location, parsePath} from 'history';
import {Location} from 'history';
import {wrapFetcher} from './utils/api';
import {
createObject,
@ -13,9 +13,9 @@ import {
anyChanged,
syncDataFromSuper,
isObjectShallowModified,
isVisible,
isEmpty,
autobind
autobind,
chainEvents
} from './utils/helper';
import {
Api,
@ -24,9 +24,7 @@ import {
SchemaNode,
Schema,
Action,
ExtractProps,
Omit,
PlainObject,
RendererData
} from './types';
import {observer} from 'mobx-react';
@ -607,7 +605,7 @@ class SchemaRenderer extends React.Component<SchemaRendererProps, any> {
<Component
{...theme.getRendererConfig(renderer.name)}
{...restSchema}
{...rest}
{...chainEvents(rest, restSchema)}
defaultData={defaultData}
$path={$path}
ref={this.refFn}

View File

@ -1,14 +1,11 @@
import React from 'react';
import PropTypes from 'prop-types';
import Scoped, {ScopedContext, IScopedContext} from '../Scoped';
import {ScopedContext, IScopedContext} from '../Scoped';
import {Renderer, RendererProps} from '../factory';
import {ServiceStore, IServiceStore} from '../store/service';
import {observer} from 'mobx-react';
import {SchemaNode, Schema, Action} from '../types';
import {filter} from '../utils/tpl';
import Modal from '../components/Modal';
import findLast = require('lodash/findLast');
import {guid, chainFunctions, isVisible} from '../utils/helper';
import {guid, isVisible} from '../utils/helper';
import {reaction} from 'mobx';
import {Icon} from '../components/icons';
import {ModalStore, IModalStore} from '../store/modal';
@ -320,13 +317,9 @@ export default class Dialog extends React.Component<DialogProps, DialogState> {
...schema
};
// 同步数据到 Dialog 层,方便 actions 根据表单数据联动。
subProps.onChange = chainFunctions(
this.handleFormChange,
schema.onChange
);
subProps.onInit = chainFunctions(this.handleFormInit, schema.onInit);
subProps.onSaved = chainFunctions(this.handleFormSaved, schema.onSaved);
subProps.onChange = this.handleFormChange;
subProps.onInit = this.handleFormInit;
subProps.onSaved = this.handleFormSaved;
}
return render(`body${key ? `/${key}` : ''}`, schema, subProps);

View File

@ -1,14 +1,10 @@
import React from 'react';
import PropTypes from 'prop-types';
import Scoped, {ScopedContext, IScopedContext} from '../Scoped';
import {ScopedContext, IScopedContext} from '../Scoped';
import {Renderer, RendererProps} from '../factory';
import {ServiceStore, IServiceStore} from '../store/service';
import {observer} from 'mobx-react';
import {SchemaNode, Schema, Action} from '../types';
import cx from 'classnames';
import {default as DrawerContainer} from '../components/Drawer';
import findLast = require('lodash/findLast');
import {guid, chainFunctions, isVisible} from '../utils/helper';
import {guid, isVisible} from '../utils/helper';
import {reaction} from 'mobx';
import {findDOMNode} from 'react-dom';
import {IModalStore, ModalStore} from '../store/modal';
@ -280,12 +276,9 @@ export default class Drawer extends React.Component<DrawerProps, object> {
};
// 同步数据到 Dialog 层,方便 actions 根据表单数据联动。
subProps.onChange = chainFunctions(
this.handleFormChange,
schema.onChange
);
subProps.onInit = chainFunctions(this.handleFormInit, schema.onInit);
subProps.onSaved = chainFunctions(this.handleFormSaved, schema.onSaved);
subProps.onChange = this.handleFormChange;
subProps.onInit = this.handleFormInit;
subProps.onSaved = this.handleFormSaved;
}
return render(`body${key ? `/${key}` : ''}`, schema, subProps);

View File

@ -14,8 +14,6 @@ import {
noop,
isObject,
isVisible,
createObject,
extendObject,
cloneObject
} from '../../utils/helper';
import debouce = require('lodash/debounce');

View File

@ -1079,9 +1079,34 @@ export function object2formData(
export function chainFunctions(
...fns: Array<(...args: Array<any>) => void>
): (...args: Array<any>) => void {
return (...args: Array<any>) => {
fns.forEach(fn => fn && fn(...args));
};
return (...args: Array<any>) =>
fns.reduce(
(ret: any, fn: any) =>
ret === false
? false
: typeof fn == 'function'
? fn(...args)
: undefined,
undefined
);
}
export function chainEvents(props: any, schema: any) {
const ret: any = {};
Object.keys(props).forEach(key => {
if (
key.substr(0, 2) === 'on' &&
typeof props[key] === 'function' &&
typeof schema[key] === 'function'
) {
ret[key] = chainFunctions(schema[key], props[key]);
} else {
ret[key] = props[key];
}
});
return ret;
}
export function mapObject(value: any, fn: Function): any {