首页 > 解决方案 > 基于相关记录字段的 React Admin Change 字段

问题描述

假设我有一个名为“资产”的记录,其中有一列名为 deducitble。一项资产可以有一个保险公司。保险公司有一个布尔字段“allowOtherDeductible”。

编辑资产时,我希望能够首先检查关联保险公司是否将 allowOtherDeductible 设置为 true。如果是这样,我将允许一个 TextInput 用于deductible,如果为 false,则允许一个 SelectInput。

我怎样才能做到这一点?在有条件地呈现字段时,我看不到获取相关记录字段的方法。

标签: react-admin

解决方案


我会编写一个自定义输入,利用将其SimpleForm注入record所有孩子的事实:

const DeductibleInput = ({ record }) => {
    if (!record) return null;
    const { data, loaded } = useGetOne('insurers', record.insured_id);
    if (!loaded) return null; // or a loader component
    return data.otherDeductible 
      ? <NumberInput
          source="deductible"
          parse={val => dollarsToCents(val)}
          format={val => centsToDollars(val)}
        />
      : <SelectInput
          source="deductible"
          parse={val => dollarsToCents(val)}
          format={val => centsToDollars(val)}
          choices={getDeductibleChoices(record.insured_value)}
         />
}

推荐阅读