Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/derived value #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 8 additions & 21 deletions packages/core/src/components/formItemWrapper/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, { useContext, useEffect, useMemo } from 'react';
import { Form, FormInstance, Col } from 'antd';
import { Form, Col } from 'antd';
import { debounce } from '../helpers';
import ExtraContext from '../../extraDataContext';
import internalWidgets from '../internalWidgets';
import type { ScopeType } from '../../expressionParser/fnExpressionTransformer';
import type { TransformedFnType } from '../../expressionParser/fnExpressionTransformer';
import PubSubCenter from '../../interaction/pubSubCenter';
import {
FieldItemMetaType,
Expand All @@ -20,6 +20,10 @@ export interface FormItemWrapperProps {
formItemMeta: FieldItemMetaType;
defaultSpan?: number;
getWidgets: GetWidgets;
calcDerivedValue: (
fieldName: string,
valueDerived: TransformedFnType
) => void;
publishServiceEvent: PubSubCenter['publishServiceEvent'];
onDerivedValueChange: (fieldName: string, value: any) => any;
valueGetter: (value) => any;
Expand All @@ -34,7 +38,7 @@ const FormItemWrapper: React.FC<FormItemWrapperProps> = (props) => {
publishServiceEvent,
valueGetter,
debounceSearch,
onDerivedValueChange,
calcDerivedValue,
} = props;
const {
fieldName,
Expand Down Expand Up @@ -81,23 +85,6 @@ const FormItemWrapper: React.FC<FormItemWrapperProps> = (props) => {
}
}, []);

/**
* 处理派生值的情况
*/
const deriveValue = (form: FormInstance) => {
if (valueDerived === null) return;

const scope: ScopeType = {
formData: form.getFieldsValue(),
extraDataRef: extraContext.extraDataRef,
};
const derivedValue = valueDerived(scope);
if (derivedValue !== form.getFieldValue(fieldName)) {
form.setFieldValue(fieldName, derivedValue);
onDerivedValueChange(fieldName, derivedValue);
}
};

const getServiceTriggerProps = (formData, extraData) => {
const serviceTriggerProps = {
onBlur: null,
Expand Down Expand Up @@ -157,7 +144,7 @@ const FormItemWrapper: React.FC<FormItemWrapperProps> = (props) => {
<FormItem noStyle shouldUpdate>
{(form) => {
if (valueGetter(destroy)) return null;
deriveValue(form as FormInstance);
calcDerivedValue(fieldName, valueDerived);
const { onBlur, onFocus, onSearch } = getServiceTriggerProps(
form.getFieldsValue(),
extraContext.extraDataRef
Expand Down
34 changes: 33 additions & 1 deletion packages/core/src/components/formRenderer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import ExtraContext, { useExtraData } from '../../extraDataContext';
import JsonConfigTransformer from '../../expressionParser/jsonConfigTransformer';
import PubSubCenter from '../../interaction/pubSubCenter';
import InteractionSubscriber from '../../interaction/interactionSubscriber';
import type { ScopeType } from '../../expressionParser/fnExpressionTransformer';
import type {
ScopeType,
TransformedFnType,
} from '../../expressionParser/fnExpressionTransformer';

const { useForm } = Form;

Expand Down Expand Up @@ -142,6 +145,26 @@ const FormRenderer: React.ForwardRefRenderFunction<
}
};

/**
* 处理派生值的情况
*/
const calcDerivedValue = (
fieldName: string,
valueDerived: TransformedFnType
) => {
if (valueDerived === null) return;

const scope: ScopeType = {
formData: form.getFieldsValue(),
extraDataRef: extraDataRef,
};
const derivedValue = valueDerived(scope);
if (derivedValue !== form.getFieldValue(fieldName)) {
form.setFieldValue(fieldName, derivedValue);
onDerivedValueChange(fieldName, derivedValue);
}
};

const onValuesChange = (changedValues, _values) => {
const changedFields = Object.keys(changedValues);
let interactFields = [];
Expand All @@ -153,6 +176,14 @@ const FormRenderer: React.ForwardRefRenderFunction<
interactFields = [...interactFields, ...fieldsName];
});

const derivedValueFields = formItemsMeta.filter(
(item) => item.valueDerived !== null
);

derivedValueFields.forEach((item) => {
calcDerivedValue(item.fieldName, item.valueDerived);
});

const shouldRenderFields = formItemsMeta
.filter((item) => {
return !valueGetter(item.destroy);
Expand Down Expand Up @@ -213,6 +244,7 @@ const FormRenderer: React.ForwardRefRenderFunction<
defaultSpan={defaultColSpan}
key={formItemMeta.fieldName}
formItemMeta={formItemMeta}
calcDerivedValue={calcDerivedValue}
onDerivedValueChange={onDerivedValueChange}
publishServiceEvent={
pubSubCenterRef.current.publishServiceEvent
Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/expressionParser/fnExpressionTransformer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { warning } from '../utils/report';
import { ExtraDataRefType } from '../extraDataContext';

export interface ScopeType {
Expand Down Expand Up @@ -42,8 +43,15 @@ class FnExpressionTransformer {
return (scope: ScopeType) => {
const proxy = this.createProxy(scope);
const fnBody = `with(scope) { return ${code} }`;
const fn = new Function('scope', fnBody);
return fn(proxy);
try {
const fn = new Function('scope', fnBody);
return fn(proxy);
} catch (error) {
warning(
`The function expression transform failed: "${code}". ErrorMessage: ${error}`,
'Expression'
);
}
};
};
}
Expand Down