如何在 react-admin 中使用相关字段的参考输入?

How to use on reference input to related filed in react-admin?

我正在使用 react-admin。

我有 3 个资源:学校、老师和 classes

在class的创作中,其中一个输入是老师,它需要是一个引用类型,但不是所有的老师只适用于属于这个class学校的人.

我应该如何支持它?

如何将school_id传递给参考输入?

谢谢!

这在文档中有解释:https://marmelab.com/react-admin/Inputs.html#referenceinput

总结:

/ you can filter the query used to populate the possible values. Use the
// `filter` prop for that.
<ReferenceInput
    source="teacher_id"
    reference="teachers"
    filter={{ school_id: values.school_id }}
>
    <SelectInput optionText="name" />
</ReferenceInput>

您可能想知道如何获得此 school_idhttps://marmelab.com/react-admin/Inputs.html#linking-two-inputs

import { useFormState } from 'react-final-form';

const TeacherInput = () => {
    const { values } = useFormState();

    return (
        <ReferenceInput
            source="teacher_id"
            reference="teachers"
            filter={{ school_id: school_id: values.school_id }}
        >
            <SelectInput optionText="name" />
        </ReferenceInput>
    );
}