环境覆盖使用 addSchemaFilter 实现 (#1742)

This commit is contained in:
吴多益 2021-04-02 18:19:32 +08:00 committed by GitHub
parent 6c697187e1
commit 4b4138e50b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 22 deletions

View File

@ -4,7 +4,6 @@ import React from 'react';
import Alert from './components/Alert2';
import ImageGallery from './components/ImageGallery';
import {RendererEnv} from './env';
import {envOverwrite} from './envOverwrite';
import {RendererProps} from './factory';
import {LocaleContext, TranslateFn} from './locale';
import {RootRenderer} from './RootRenderer';
@ -59,9 +58,6 @@ export class Root extends React.Component<RootProps> {
const theme = env.theme;
// 根据环境覆盖 schema这个要在最前面做不然就无法覆盖 validations
envOverwrite(schema, locale);
return (
<RootStoreContext.Provider value={rootStore}>
<ThemeContext.Provider value={this.props.theme || 'default'}>

View File

@ -2,31 +2,23 @@
* @file 使
*/
import {findObjectsWithKey} from './utils/helper';
import {SchemaNode, Schema} from './types';
import {cloneDeep} from 'lodash';
import {RendererProps, RendererConfig, addSchemaFilter} from './factory';
const isMobile = (window as any).matchMedia?.('(max-width: 768px)').matches
? true
: false;
export const envOverwrite = (schema: any, locale?: string) => {
addSchemaFilter(function (schema: Schema, renderer, props) {
if (schema.mobile && isMobile) {
Object.assign(schema, schema.mobile);
delete schema.mobile;
return {...schema, ...schema.mobile};
}
if (locale) {
let schemaNodes = findObjectsWithKey(schema, locale);
for (let schemaNode of schemaNodes) {
Object.assign(schemaNode, schemaNode[locale]);
delete schemaNode[locale];
}
if (props.locale && schema[props.locale]) {
return {...schema, ...schema[props.locale]};
}
if (isMobile) {
let schemaNodes = findObjectsWithKey(schema, 'mobile');
for (let schemaNode of schemaNodes) {
Object.assign(schemaNode, schemaNode['mobile']);
delete schemaNode['mobile'];
}
}
};
return schema;
});

View File

@ -172,6 +172,8 @@ import {FormItem, registerFormItem} from './renderers/Form/Item';
// 兼容旧版本用法
import './compat';
import './envOverwrite';
import './themes/default';
import './themes/cxd';
import './themes/dark';