如何从 Create 表单中的另一个组件更改 Admin on Rest 中的输入值?

And*_*ane 2 react-redux admin-on-rest

为了便于讨论,我将其简化为一个非常简单的案例。我有一个带有 1 个字段和 1 个按钮的简单创建表单。我希望按钮在不提交表单的情况下将 TextInput 的值设置为“Hello”。这在管理员休息时怎么可能?例如:

    export const TestCreate = (props) => (
    <Create title={<TestTitle />} {...props}>
        <SimpleForm>
            <TextInput source="title" />

            <TitleSetterButton />
        </SimpleForm>
    </Create>
    );
Run Code Online (Sandbox Code Playgroud)

已经为此苦苦挣扎了一段时间 - 它应该很简单,所以希望有一个简单的答案。

Tar*_*ani 5

我能够使用他们的示例应用程序设置一个示例表单

// in src/posts.js
import React from 'react';
import { List, Edit, Create, Datagrid, ReferenceField, TextField, EditButton, DisabledInput, LongTextInput, ReferenceInput, required, SelectInput, SimpleForm, TextInput } from 'admin-on-rest';

import FlatButton from 'material-ui/FlatButton';


export const PostList = (props) => (
    <List {...props}>
        <Datagrid>
            <TextField source="id" />
            <ReferenceField label="User" source="userId" reference="users">
                <TextField source="name" />
            </ReferenceField>
            <TextField source="title" />
            <TextField source="body" />
            <EditButton />
        </Datagrid>
    </List>
);

const PostTitle = ({ record }) => {
    return <span>Post {record ? `"${record.title}"` : ''}</span>;
};


export class Testing extends React.Component {
        render() {
            return <input type="text" />
        }
}

export class PostCreate extends React.Component {
    componentDidMount() {
        console.log(this)
    }

    constructor(props) {
        super(props);

        this.handleCustomClick = this.handleCustomClick.bind(this);
        // this.fieldOptions = this.fieldOptions.bind(this);
    }

    handleCustomClick() {
        this.fields.title.handleInputBlur("tarun lalwani");
        this.fields.body.handleInputBlur("this is how you change it!");
    }


    render () {

        let refOptions = {ref: (e) => {
            if (e && e.constructor && e.props && e.props.name) {
                this.fields = this.fields || {};
                this.fields[e.props.name] = e;
            }
        }}

        return (
    <Edit title={<PostTitle />} {...this.props}>
        <SimpleForm>
            <DisabledInput source="id" />
            <ReferenceInput label="User" source="userId" reference="users" validate={required}>
                <SelectInput optionText="name" />
            </ReferenceInput>
            <TextInput source="title" options={refOptions}/>
            <LongTextInput source="body" options={refOptions}/>
            <FlatButton primary label="Set Value" onClick={this.handleCustomClick} />
        </SimpleForm>
    </Edit>


            );
    }
}
Run Code Online (Sandbox Code Playgroud)

点击按钮前

设定值前

点击后 Set Value

设定值后

然后点击后Save你可以看到实际更改的值被发布

已发送更改的值