Skip to content

Commit

Permalink
fix: 修复变量问题
Browse files Browse the repository at this point in the history
  • Loading branch information
jianbing.chen committed Aug 10, 2024
1 parent d586633 commit 915883f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
12 changes: 6 additions & 6 deletions packages/editor/src/components/VariableBind/VariableSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,27 +139,27 @@ const SelectVariableModal = ({ onSelect }: { onSelect: (record: any) => void },

// 选择页面全局变量
if (node.type === 'Variable') {
form.setFieldValue('expression', `${beforeExpression} variable.${node.id}`.trimStart());
form.setFieldValue('expression', `${beforeExpression} context.variable.${node.id}`.trimStart());
return;
}
// 判断取值方式,如果是表单项,就按照表单的方式取值
if (node.type === 'PageVariable') {
form.setFieldValue('expression', `${beforeExpression} variable`);
form.setFieldValue('expression', `${beforeExpression} context.variable`);
} else if (node.type === 'EditTable') {
const name = elementsMap[node.id]?.config.props.field;
if (name) form.setFieldValue('expression', `${beforeExpression} ${node.parentId}.${name}`.trimStart());
if (name) form.setFieldValue('expression', `${beforeExpression} context.${node.parentId}.${name}`.trimStart());
} else if (node.type === 'Form') {
form.setFieldValue('expression', `${beforeExpression} ${node.id}`);
form.setFieldValue('expression', `${beforeExpression} context.${node.id}`);
} else {
const formItem = elementsMap[node.id]?.config.props.formItem;
form.setFieldValue('expression', `${beforeExpression} ${node.parentId}.${formItem.name}`.trimStart());
form.setFieldValue('expression', `${beforeExpression} context.${node.parentId}.${formItem.name}`.trimStart());
}
};

// 计算函数点击
const handleClick = (type: string) => {
const beforeExpression = form.getFieldValue('expression') ?? '';
form.setFieldValue('expression', `${beforeExpression} ${type}()`.trimStart());
form.setFieldValue('expression', `${beforeExpression} context.${type}()`.trimStart());
};

// 确认提交
Expand Down
8 changes: 4 additions & 4 deletions packages/editor/src/packages/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ export function renderFormula(formula: string, eventParams: any = {}) {
const originIds: Array<string> = [...new Set(formIds.map((id) => id.split('.')[0]))];
const fnParams: Array<string> = ['context', 'variable', 'eventParams', 'FORMAT'];
const data: Array<any> = [];
const pageStore = usePageStore.getState().page;
originIds.forEach((id: string) => {
fnParams.push(id);
const pageStore = usePageStore.getState().page;
// 如果绑定的是表单项,则通过Form实例对象获取对应表单值
const formValues = pageStore.formData?.[id] || {};
data.push(formValues);
Expand All @@ -226,10 +226,10 @@ export function renderFormula(formula: string, eventParams: any = {}) {
variable: variableData,
eventParams,
FORMAT,
...data,
...(pageStore.formData || {}),
};
const result = dynamicFunc(context, variableData, eventParams || {}, FORMAT, ...data);
if (typeof result === 'function') return result(context, variableData, eventParams || {}, FORMAT, ...data);
const result = dynamicFunc(context, eventParams || {});
if (typeof result === 'function') return result(context, eventParams || {});
return result;
} catch (error) {
console.error('表达式解析失败:', error);
Expand Down
13 changes: 9 additions & 4 deletions packages/materials/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ export function renderFormula(formula: string, eventParams?: any) {
const originIds: Array<string> = [...new Set(formIds.map((id) => id.split('.')[0]))];
const fnParams: Array<string> = ['variable', 'eventParams', 'FORMAT'];
const data: Array<any> = [];
const pageStore = usePageStore.getState().page;
originIds.forEach((id: string) => {
fnParams.push(id);
const pageStore = usePageStore.getState().page;
// 如果绑定的是表单项,则通过Form实例对象获取对应表单值
const formValues = pageStore.formData?.[id] || {};
data.push(formValues);
Expand All @@ -222,8 +222,14 @@ export function renderFormula(formula: string, eventParams?: any) {
const FORMAT = (date: any, fmt: string = 'YYYY-MM-DD HH:mm:ss') => {
return dayjs(date).format(fmt);
};
const result = dynamicFunc(variableData, eventParams || {}, FORMAT, ...data);
if (typeof result === 'function') return result(variableData, eventParams || {}, FORMAT, ...data);
const context = {
variable: variableData,
eventParams,
FORMAT,
...(pageStore.formData || {}),
};
const result = dynamicFunc(context, variableData, eventParams || {}, FORMAT, ...data);
if (typeof result === 'function') return result(context, variableData, eventParams || {}, FORMAT, ...data);
return result;
} catch (error) {
console.error('表达式解析失败:', error);
Expand Down Expand Up @@ -357,7 +363,6 @@ export const loadStyle = (id: string, src: string) => {
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = src;
link.id = id;
link.onload = resolve;
link.onerror = reject;

Expand Down

0 comments on commit 915883f

Please sign in to comment.