admin on rest:在create form中显示默认值

时间:2018-02-02 01:56:52

标签: reactjs admin-on-rest

这里我使用react-material-responsive-grid来显示多行结构中的表单。但这不是问题..

我想在文本字段中显示默认值。我也提供了默认值,但它没有显示。

这是我的代码:

import React from 'react';
import { CheckboxGroupInput, BooleanField, Filter, List, Edit, Create, Datagrid, ReferenceField, EditButton, DisabledInput, LongTextInput, ReferenceInput, SelectInput, SimpleForm, TextInput } from 'admin-on-rest';
import { Field } from 'redux-form';
import { Grid, Row, Col } from 'react-material-responsive-grid';
import Checkbox from 'material-ui/Checkbox';
import { TextField, SelectField } from 'redux-form-material-ui';

const list = [
    "name1", 
    "name2", 
];


export const PostCreate = (props) => (
    <Create {...props} >
        <SimpleForm>
            <TextInput source="title" />
            <LongTextInput source="body" />

            <Grid>
                {list.map((selectItem) => {
                    return  <Row> 
                            <Col xs={1}>
                                <Field name={selectItem} defaultValue="default value" component={TextField} floatingLabelText="Select" hintText="Select"/>
                            </Col>
                            <Col sm={1}> <div>{selectItem} </div></Col>  
                        </Row>     
                })}
            </Grid>
        </SimpleForm>
    </Create>
);

1 个答案:

答案 0 :(得分:2)

redux-form defaultValue组件不支持<Field>道具。它是<Input>组件的 aor实现

但您可以尝试在redux表单的实例化中声明默认值,如下所示:

const list = ['name1', 'name2'];
const postDefaultValue = { name1: 'default value', name2: 'default value' };
export const PostCreate = (props) => (
    <Create {...props} >
        <SimpleForm defaultValue={postDefaultValue}>
        ...