代码之家  ›  专栏  ›  技术社区  ›  singingwolfboy

如何将多个提交按钮与react final form一起使用?

  •  0
  • singingwolfboy  · 技术社区  · 7 年前

    我想用 react-final-form 它有多个提交按钮,其中每个提交按钮在表单中设置不同的值。本质上,我想创建一个在呈现的HTML中类似这样的表单:

    <form>
      Are you over 18 years old?
      <button type="submit">Yes</button>
      <button type="submit">No</button>
    </form>
    

    但是,我不知道如何使React最终表单将这些不同的提交按钮作为表单中的设置值。我尝试组合组件状态, <input type="hidden"> onClick 处理程序,如下所示:

    class FormWithMultipleSubmits extends React.Component {
      state = {
        value: undefined
      };
    
      setValue = value => this.setState({ value: value });
    
      render() {
        return (
          <Form>
            Are you over 18 years old?
            <Field
              name="adult"
              component="input"
              type="hidden"
              value={this.state.value}
            />
            <button type="submit" onClick={() => this.setValue(true)}>
              Yes
            </button>
            <button type="submit" onClick={() => this.setValue(false)}>
              No
            </button>
          </Form>
        );
      }
    }
    

    然而,这似乎不起作用——可能是因为 the value property on the <Field> component is only used for checkboxes and radio buttons .

    有人能把我推向正确的方向吗?怎样才能正确地解决这个问题?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Erik R.    7 年前

    这是一个 Sandbox 这说明了怎么做。

    推荐文章