fix(formula-field): formula field failed to real-time evaluating and support sub-form (#2983)

* fix:  formula field failed to real-time evaluating

* fix:  formula field failed to real-time evaluating
This commit is contained in:
katherinehhh 2023-11-07 21:03:49 +08:00 committed by GitHub
parent ee3d5af04d
commit adfb9a72ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -39,31 +39,16 @@ function useTargetCollectionField() {
}
return getCollectionField(`${collection.name}.${paths[paths.length - 1]}`);
}
function getValuesByPath(data, path) {
const keys = path.split('.');
let current = data;
for (const key of keys) {
if (current && typeof current === 'object') {
if (Array.isArray(current) && !isNaN(key)) {
const index = parseInt(key);
if (index >= 0 && index < current.length) {
current = current[index];
function getValuesByPath(values, key, index?) {
const targetValue = values[key];
if (Array.isArray(targetValue)) {
return targetValue[index];
}
if (targetValue && typeof targetValue === 'object') {
return targetValue;
} else {
return data;
return values;
}
} else if (key in current) {
current = current[key];
} else {
return data;
}
} else {
return data;
}
}
return current;
}
export function Result(props) {
@ -76,7 +61,8 @@ export function Result(props) {
const field = useField();
const path: any = field.path.entire;
const fieldPath = path?.replace(`.${fieldSchema.name}`, '');
const fieldName = fieldPath.split('.')[0];
const index = parseInt(fieldPath.split('.')?.[1]);
useEffect(() => {
setEditingValue(value);
}, [value]);
@ -90,8 +76,7 @@ export function Result(props) {
) {
return;
}
const scope = toJS(getValuesByPath(form.values, fieldPath));
const scope = toJS(getValuesByPath(form.values, fieldName, index));
let v;
try {
v = evaluate(expression, scope);
@ -103,6 +88,7 @@ export function Result(props) {
setEditingValue(v);
}
setEditingValue(v);
others?.onChange?.(v);
});
});
const Component = TypedComponents[dataType] ?? InputString;