mirror of
https://gitee.com/baidu/amis.git
synced 2024-11-29 18:48:45 +08:00
fix: 修复 replaceText 不支持 schemaApi 问题 (#4300)
This commit is contained in:
parent
acc101b425
commit
73b35ee49d
@ -263,9 +263,8 @@ const predefined = {
|
||||
label: '记住登录'
|
||||
},
|
||||
{
|
||||
type: 'submit',
|
||||
btnClassName: 'btn-default',
|
||||
label: '登录'
|
||||
type: 'static',
|
||||
value: 'AMIS_HOST'
|
||||
}
|
||||
]
|
||||
},
|
||||
@ -303,6 +302,10 @@ const predefined = {
|
||||
type: 'datetime',
|
||||
labelClassName: 'text-muted',
|
||||
name: 'date'
|
||||
},
|
||||
{
|
||||
type: 'static',
|
||||
value: 'AMIS_HOST'
|
||||
}
|
||||
]
|
||||
}
|
||||
|
@ -35,6 +35,7 @@ import {OnEventProps} from './utils/renderer-event';
|
||||
import {enableDebug} from './utils/debug';
|
||||
|
||||
import type {ToastLevel, ToastConf} from './components/Toast';
|
||||
import {replaceText} from './utils/replaceText';
|
||||
|
||||
export interface TestFunc {
|
||||
(
|
||||
@ -447,24 +448,7 @@ export function render(
|
||||
props.useMobileUI = true;
|
||||
}
|
||||
|
||||
// 进行文本替换
|
||||
if (env.replaceText && isObject(env.replaceText)) {
|
||||
const replaceKeys = Object.keys(env.replaceText);
|
||||
replaceKeys.sort((a, b) => b.length - a.length); // 避免用户将短的放前面
|
||||
const replaceTextIgnoreKeys = new Set(env.replaceTextIgnoreKeys || []);
|
||||
JSONTraverse(schema, (value: any, key: string, object: any) => {
|
||||
if (typeof value === 'string' && !replaceTextIgnoreKeys.has(key)) {
|
||||
for (const replaceKey of replaceKeys) {
|
||||
if (~value.indexOf(replaceKey)) {
|
||||
value = object[key] = value.replaceAll(
|
||||
replaceKey,
|
||||
env.replaceText[replaceKey]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
replaceText(schema, env.replaceText, env.replaceTextIgnoreKeys);
|
||||
|
||||
return (
|
||||
<EnvContext.Provider value={env}>
|
||||
|
@ -4,6 +4,7 @@ import {Api, ApiObject, Payload, fetchOptions} from '../types';
|
||||
import {extendObject, isEmpty, isObject} from '../utils/helper';
|
||||
import {ServerError} from '../utils/errors';
|
||||
import {normalizeApiResponseData} from '../utils/api';
|
||||
import {replaceText} from '../utils/replaceText';
|
||||
|
||||
export const ServiceStore = iRendererStore
|
||||
.named('ServiceStore')
|
||||
@ -402,6 +403,9 @@ export const ServiceStore = iRendererStore
|
||||
);
|
||||
} else {
|
||||
if (json.data) {
|
||||
const env = getEnv(self);
|
||||
replaceText(json.data, env.replaceText, env.replaceTextIgnoreKeys);
|
||||
|
||||
self.schema = Array.isArray(json.data)
|
||||
? json.data
|
||||
: {
|
||||
|
30
src/utils/replaceText.ts
Normal file
30
src/utils/replaceText.ts
Normal file
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* 对文本进行替换
|
||||
*/
|
||||
|
||||
import {isObject, JSONTraverse} from './helper';
|
||||
|
||||
export function replaceText(
|
||||
schema: any,
|
||||
replaceText: {[propName: string]: string},
|
||||
replaceTextIgnoreKeys: String[]
|
||||
) {
|
||||
// 进行文本替换
|
||||
if (replaceText && isObject(replaceText)) {
|
||||
const replaceKeys = Object.keys(replaceText);
|
||||
replaceKeys.sort((a, b) => b.length - a.length); // 避免用户将短的放前面
|
||||
const IgnoreKeys = new Set(replaceTextIgnoreKeys || []);
|
||||
JSONTraverse(schema, (value: any, key: string, object: any) => {
|
||||
if (typeof value === 'string' && !IgnoreKeys.has(key)) {
|
||||
for (const replaceKey of replaceKeys) {
|
||||
if (~value.indexOf(replaceKey)) {
|
||||
value = object[key] = value.replaceAll(
|
||||
replaceKey,
|
||||
replaceText[replaceKey]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user