How to create a ReferenceInput wrapped component?

天大地大妈咪最大 提交于 2019-12-02 06:17:38

I found a (the?) solution.

We need to use a redux-form Field component in Simpleform.

  import { Field } from 'redux-form';

  export const commentsCreate = (props) => (
    <Create title = "Create" {...props} >
      <SimpleForm>
        <Field name="field" component={Prueba} {...props} />
      </SimpleForm>
    </Create>
  );

  const Prueba = (props) => (
    <ReferenceInput label="Posts" source="field" reference="posts"  allowEmpty {...props}>
      <AutocompleteInput optionText="name" {...props}/>
    </ReferenceInput>
  );

From https://redux-form.com/6.0.0-alpha.4/docs/api/field.md/

The Field component is how you connect each individual input to the Redux store. There are three fundamental things that you need to understand in order to use Field correctly:

The name prop is required. It is a string path, in dot-and-bracket notation, corresponding to a value in the form values. It may be as simple as 'firstName' or as complicated as contact.billing.address[2].phones[1].areaCode.

The component prop is required. It may be a Component, a stateless function, or a factory, like those provided under React.DOM. See Usage section below. To learn about the props that will provided to whatever your component is, see the Props section below.

All other props will be passed along to the element generated by the component prop.

is question actual now? Some time ago I divided component this way:

1) create file DescriptionField.js

2) write code into it

import React from 'react';
export const DescriptionField = ({ source = "Description" }) => <span><p/><p/>{source}<p/></span>;
export default DescriptionField;

(maybe it can be simple). Maybe you forgot export ?

3) and in parent component call it:

export const SomeMyCreate = (props) => (
  <Create {...props}>
    <SimpleForm>
      .....
      <DescriptionField source="Warn! Only one picture may be here!"/>
    </SimpleForm>
  </Create>
);

You can try to do it the same method. Please write your code Prueba component file here

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!