sin*_*boy 1 final-form react-final-form
我想使用带有多个提交按钮的react-final-form构建一个表单,其中每个提交按钮在表单中设置不同的值。本质上,我想创建一个在呈现的HTML中看起来像这样的表单:
<form>
Are you over 18 years old?
<button type="submit">Yes</button>
<button type="submit">No</button>
</form>
Run Code Online (Sandbox Code Playgroud)
但是,我不知道如何使react-final-form将这些不同的提交按钮视为表单中的设置值。我尝试将组件状态,<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>
);
}
}
Run Code Online (Sandbox Code Playgroud)
然而,这似乎并没有工作-这可能是因为在value该物业<Field>组件仅用于复选框和单选按钮。
有人可以按正确的方向向我推一下,以解决该问题吗?
| 归档时间: |
|
| 查看次数: |
2105 次 |
| 最近记录: |