refactor: sub-table/sub-form support formula field (#2449)
This commit is contained in:
parent
d8d01befdc
commit
9b00aa9cc0
@ -1,7 +1,7 @@
|
|||||||
import React, { useState, useEffect } from 'react';
|
import React, { useState, useEffect } from 'react';
|
||||||
import { onFormInputChange } from '@formily/core';
|
import { onFormInputChange } from '@formily/core';
|
||||||
import { toJS } from '@formily/reactive';
|
import { toJS } from '@formily/reactive';
|
||||||
import { useFieldSchema, useFormEffects, useForm } from '@formily/react';
|
import { useFieldSchema, useFormEffects, useField } from '@formily/react';
|
||||||
import {
|
import {
|
||||||
Checkbox,
|
Checkbox,
|
||||||
DatePicker,
|
DatePicker,
|
||||||
@ -39,6 +39,32 @@ function useTargetCollectionField() {
|
|||||||
return getCollectionField(`${collection.name}.${paths[paths.length - 1]}`);
|
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];
|
||||||
|
} 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) {
|
||||||
const { value, ...others } = props;
|
const { value, ...others } = props;
|
||||||
const fieldSchema = useFieldSchema();
|
const fieldSchema = useFieldSchema();
|
||||||
@ -46,6 +72,9 @@ export function Result(props) {
|
|||||||
const [editingValue, setEditingValue] = useState(value);
|
const [editingValue, setEditingValue] = useState(value);
|
||||||
const { evaluate } = (evaluators as Registry<Evaluator>).get(engine);
|
const { evaluate } = (evaluators as Registry<Evaluator>).get(engine);
|
||||||
const formBlockContext = useFormBlockContext();
|
const formBlockContext = useFormBlockContext();
|
||||||
|
const field = useField();
|
||||||
|
const path: any = field.path.entire;
|
||||||
|
const fieldPath = path?.replace(`.${fieldSchema.name}`, '');
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setEditingValue(value);
|
setEditingValue(value);
|
||||||
@ -60,7 +89,8 @@ export function Result(props) {
|
|||||||
) {
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const scope = toJS(form.values);
|
|
||||||
|
const scope = toJS(getValuesByPath(form.values, fieldPath));
|
||||||
let v;
|
let v;
|
||||||
try {
|
try {
|
||||||
v = evaluate(expression, scope);
|
v = evaluate(expression, scope);
|
||||||
|
Loading…
Reference in New Issue
Block a user