【问题标题】:How to submit the form by Material UI Dialog using ReactJS如何使用 ReactJS 通过 Material UI Dialog 提交表单
【发布时间】:2017-04-14 09:04:09
【问题描述】:

我使用 Material UI Dialog 制作了一个表单列表。官方案例:

<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提交我的表单数据?

----------------------------------- -更新----------------------------------------------------------

还有一个答案:

在对话框操作按钮中添加type和form的属性:

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>

【问题讨论】:

    标签: javascript reactjs material-ui


    【解决方案1】:

    您可以将

    放在 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')
    );
    

    【讨论】:

    • 如果我想让表单成为一个组件,并且自己有一个状态,我可以使用redux来构建数据状态吗?
    • 是的。该组件可以处理状态(如其打开状态),或者您可以将其设为无状态函数并将“打开”作为道具传递(并且该道具可以由 redux 设置)。这是上面转换为无状态函数的组件。你可以用 redux 的 connect() 包装它,将它连接到 store。 jsfiddle.net/b3ahf3se/2祝你好运!
    • 我把我的表单变成了一个组件&lt;UserForm /&gt;,像这样:&lt;Dialog&gt; &lt;UserForm/&gt; &lt;/Dialog&gt;,我怎样才能把动作放在表单中?
    • preventDefault 阻止表单提交......所以这不起作用。
    • 即使您像 JSON 一样自己发送,添加提交也是有益的,因为您可以使用 ENTER 按钮提交表单。
    【解决方案2】:

    在 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。

    【讨论】:

    • onSubmit={handleSubmit(this.onSubmit)} ...丑
    【解决方案3】:

    Dialog是material ui的一个ui组件,它不会自动提交你的form数据,如果你想创建一个form,在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>
    

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

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

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

    【讨论】:

    • 那么如何构建表单的状态呢?
    猜你喜欢
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 2018-11-11
    • 2020-11-26
    • 2017-02-07
    • 1970-01-01
    相关资源
    最近更新 更多