【问题标题】:Complex calculated fields and values with react-final-form inside Edit复杂的计算字段和值,在编辑中使用 react-final-form
【发布时间】:2020-01-23 22:00:59
【问题描述】:

我正在开发一个复杂的编辑表单,其中包含 200 多个字段,其中大约一半是通过使用其他字段的值计算得出的,在某些情况下,公式会引用来自多个其他字段的值。

我正在尝试在 react-admin 的编辑中使用 react-final-form 的 <Form\>。我无法弄清楚如何在装饰器中引用多个字段,出于某种原因,allValues 仅在更新语句中的“=>”之前可见。它似乎在 react-admin 之外工作正常,但在集成到 react-admin 的编辑中时却不行。

Object is possibly 'undefined'.  TS2532

    77 |        field: ["Field1"], 
    78 |        updates: {
  > 79 |            Field2: (Field1Value, allValues) => Field1Value + allValues.Field3 * 10
       |                                                              ^
    80 |        }
    81 |    }
    82 |

另一个问题是我找不到引用表单值的方法,当在 Edit 中使用表单时,不会填充“values”属性。

【问题讨论】:

    标签: react-admin


    【解决方案1】:

    您不能将数组用作字段值。 react final form decorator的概念是找到任何字段的变化,你可以相应地改变其他字段。这就是你不能将数组作为字段值传递的原因。

    {
      field: 'product_code', // if product_code changes
      updates: {
        bill_amount: (product_code, allValues) => do some calculations,
        total_discount: (product_code, allValues) => do some calculations
      }
    }
    

    我还为您准备了一些代码示例,因为我最近使用了最终表单装饰器

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    相关资源
    最近更新 更多