feat(web): visible_when 增强 — 支持 AND/OR/NOT/括号 表达式
- 新增 utils/exprEvaluator.ts 表达式解析器 - 支持 eq/and/or/not 四种节点类型和括号分组 - 支持 == 和 != 比较运算符 - PluginCRUDPage 替换简单正则为 evaluateVisibleWhen
This commit is contained in:
@@ -41,29 +41,11 @@ import {
|
||||
type PluginPageSchema,
|
||||
type PluginSectionSchema,
|
||||
} from '../api/plugins';
|
||||
import { evaluateVisibleWhen } from '../utils/exprEvaluator';
|
||||
|
||||
const { Search } = Input;
|
||||
const { TextArea } = Input;
|
||||
|
||||
/** visible_when 表达式解析 */
|
||||
function parseVisibleWhen(expression: string): { field: string; value: string } | null {
|
||||
const regex = /^(\w+)\s*==\s*'([^']*)'$/;
|
||||
const match = expression.trim().match(regex);
|
||||
if (!match) return null;
|
||||
return { field: match[1], value: match[2] };
|
||||
}
|
||||
|
||||
/** 判断字段是否应该显示 */
|
||||
function shouldShowField(
|
||||
allValues: Record<string, unknown>,
|
||||
visibleWhen: string | undefined,
|
||||
): boolean {
|
||||
if (!visibleWhen) return true;
|
||||
const parsed = parseVisibleWhen(visibleWhen);
|
||||
if (!parsed) return true;
|
||||
return String(allValues[parsed.field] ?? '') === parsed.value;
|
||||
}
|
||||
|
||||
interface PluginCRUDPageProps {
|
||||
/** 如果从 tabs/detail 页面内嵌使用,通过 props 传入配置 */
|
||||
pluginIdOverride?: string;
|
||||
@@ -608,7 +590,7 @@ export default function PluginCRUDPage({
|
||||
>
|
||||
{fields.map((field) => {
|
||||
// visible_when 条件显示
|
||||
const visible = shouldShowField(formValues, field.visible_when);
|
||||
const visible = evaluateVisibleWhen(field.visible_when, formValues);
|
||||
if (!visible) return null;
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user