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