如何使用ReactJS通过Material UI Dialog提交表单

时间:2016-11-30 06:21:55

标签: javascript reactjs material-ui

我使用Material UI对话框制作表单列表。在官方案例中:

<Dialog
          title="Dialog With Custom Width"
          actions={actions}
          modal={true}
          open={this.state.open}
        >
          This dialog spans the entire width of the screen.
</Dialog>

行动是:

   const actions = [
      <FlatButton
        label="Cancel"
        primary={true}
        onTouchTap={this.handleClose}
      />,
      <FlatButton
        label="Submit"
        primary={true}
        onTouchTap={this.handleClose}
      />,
    ];

如何构建表单并让Dialog可以提交表单数据?

----------------------------------------------- -UPDATE ----------------------------------------------- < / p>

还有另一个答案:

在“对话框操作”按钮中添加typeform的属性:

const actions = [
      <FlatButton
        label="Cancel"
        primary={true}
        onTouchTap={this.handleClose}
      />,
      <FlatButton
        label="Submit"
        primary={true}
        onTouchTap={this.handleClose}
        type="submit"        //set the buttom type is submit
        form="myform"        //target the form which you want to sent 
      />,
    ];

并将属性id赋予对话框中的表单:

<Dialog
          title="Dialog With Custom Width"
          actions={actions}
          modal={true}
          open={this.state.open}
        >
        // here can put child component and the code still work even the form is in the child component
         <div className="deal_form">
          <form id="myform" onSubmit = {this.handleCreate} >
            <TextField value={this.state.staff_number} />
          </form>
        </div>
</Dialog>

3 个答案:

答案 0 :(得分:10)

你可以放一个&lt; form&gt;在Dialog中,你还必须将{actions}放在表单中,而不是actions属性。您的提交操作按钮上应该有type =“submit”(也支持type =“reset”,如下所示)。

jsFiddle:https://jsfiddle.net/14dugwp3/6/

const {
  Dialog,
  TextField,
  FlatButton,
  MuiThemeProvider,
  getMuiTheme,
} = MaterialUI;

class Example extends React.Component {
  constructor(props) {
    super(props);
    this.state = { open: true };
    this.handleClose = this._handleClose.bind(this);
  }

  _handleClose() {
    this.setState({ open: false });
  }

  render() {
    const actions = [
      <FlatButton
        type="reset"
        label="Reset"
        secondary={true}
        style={{ float: 'left' }}
        />,
      <FlatButton
        label="Cancel"
        primary={true}
        onClick={this.handleClose}
        />,
      <FlatButton
        type="submit"
        label="Submit"
        primary={true}
        />,
    ];

    return (
      <Dialog
        title="Dialog With Custom Width"
        modal={true}
        open={this.state.open}
        >
        <form action="/" method="POST" onSubmit={(e) => { e.preventDefault(); alert('Submitted form!'); this.handleClose(); } }>
          This dialog spans the entire width of the screen.
          <TextField name="email" hintText="Email" />
          <TextField name="pwd" type="password" hintText="Password" />
          <div style={{ textAlign: 'right', padding: 8, margin: '24px -24px -24px -24px' }}>
            {actions}
          </div>
        </form>
      </Dialog>
    );
  }
}

const App = () => (
  <MuiThemeProvider muiTheme={getMuiTheme() }>
    <Example />
  </MuiThemeProvider>
);

ReactDOM.render(
  <App />,
  document.getElementById('container')
);

答案 1 :(得分:3)

在HTML5中,form=""属性可用作对页面上任何表单的引用。因此,按钮获取form="my-form-id"属性,表单获取id="my-form-id"属性。

return (
  <Dialog
    open
    actions={[
      <RaisedButton type="submit" form="my-form-id" label="Submit" />
    ]}
  >
    <form id="my-form-id" onSubmit={handleSubmit(this.onSubmit)}>
      <TextField {...fields.username} floatingLabelText="Username" />
    </form>
  </Dialog>
);

我使用Material UI v0.20。

答案 2 :(得分:0)

Dialog是材料ui的ui组件,它不会自动提交表单数据,如果你想创建一个表单,在Dialog中定义它,如下所示:

<Dialog
      title="Dialog With Custom Width"
      actions={actions}
      modal={true}
      open={this.state.open}
    >
      /*CREATE THE FORM UI HERE*/
      <div>Field1</div>
      <div>Field2</div>
      <div>Field3</div>
</Dialog>

如果表单包含许多字段,则使用对话框中的bool使内容可滚动autoScrollBodyContent = {true}

您在提交按钮单击时定义了一个函数this.handleSubmit.bind(this),在此函数内调用api并提交您要提交的数据,一旦api调用成功,关闭对话框。

如果这可以解决您的问题或您想要的任何其他详细信息,请发表评论。