从“编辑”组件访问模型

时间:2017-03-20 16:15:21

标签: javascript admin-on-rest

我有以下编辑组件

const UserEdit = (props) => (
  <Edit {...props}>
    <TabbedForm >
      <FormTab label="User Account">
        <DisabledInput source="id" />
        <TextInput source="email" />
        <TextInput source="password" type="password"/>
        <TextInput source="name" />
        <TextInput source="phone" />
        <SelectInput source="role" optionValue="id" choices={choices} />
      </FormTab>
      <FormTab label="Customer Information">
        <BooleanInput label="New user" source="Customer.is_new" />
        <BooleanInput label="Grandfathered user" source="Customer.grandfathered" />
      </FormTab>
    </TabbedForm >
  </Edit>
);

第二个FormTab(客户信息)如果User模型有一些关联的信息(JSON就像),我只需要它出现:

{
   id: <int>,
   name: <string>,
   ....
   Customer: {
       is_new: true,
       grandfathered: true,
   }
}

我想知道我是否可以以某种方式访问​​模型信息(在这种情况下,如果客户密钥存在且有信息),以便能够呈现或不呈现<FormTab label="Customer Information">

我对全球redux状态有点失落。我知道数据处于状态,因为我已经使用Redux工具对其进行了调试。 (我试图查看this.props,但我找不到任何可以访问全局状态的内容)

感谢。

1 个答案:

答案 0 :(得分:4)

如果您在构建表单时需要该对象,则可以connect将其添加到redux存储区。

import { connect } from 'react-redux';

// this is just a form, note this is not being exported
const UserEditForm = (props) => {
    console.log(props.data); // your object will be in props.data.<id>
    let customFormTab;
    const id = props.params.id;

    if (typeof props.data === 'object' && && props.data.hasOwnProperty(id)) {
        if (props.data[id].something) {
            customFormTab = <FormTab>...;
        }
    }

    return <Edit {...props}>
          <Formtab...
          {customFormTab}
    </Edit>;
}

// this is your exported component
export const UserEdit = connect((state) => ({
    data: state.admin.User.data
}))(UserEditForm);

PS:我不确定这是不是一个好的或理想的解决方案(希望有人会纠正我,如果我做的事情不是根据redux或{{1 }} 被设计)。

相关问题