mirror of
https://gitee.com/baidu/amis.git
synced 2024-12-02 12:08:13 +08:00
环境覆盖使用 addSchemaFilter 实现 (#1742)
This commit is contained in:
parent
6c697187e1
commit
4b4138e50b
@ -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'}>
|
||||
|
@ -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;
|
||||
});
|
||||
|
@ -172,6 +172,8 @@ import {FormItem, registerFormItem} from './renderers/Form/Item';
|
||||
// 兼容旧版本用法
|
||||
import './compat';
|
||||
|
||||
import './envOverwrite';
|
||||
|
||||
import './themes/default';
|
||||
import './themes/cxd';
|
||||
import './themes/dark';
|
||||
|
Loading…
Reference in New Issue
Block a user