Javascript 如何使用ReactJS通过材质UI对话框提交表单
我使用“材质UI”对话框创建表单列表。在官方案例中:Javascript 如何使用ReactJS通过材质UI对话框提交表单,javascript,reactjs,material-ui,Javascript,Reactjs,Material Ui,我使用“材质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&
<Dialog
title="Dialog With Custom Width"
actions={actions}
modal={true}
open={this.state.open}
>
This dialog spans the entire width of the screen.
</Dialog>
并在对话框中为窗体指定属性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>
//这里可以放置子组件,即使表单位于子组件中,代码仍然可以工作
对话框是材质ui的ui组件,它不会自动提交表单数据,如果要创建表单,请在对话框中定义它,如下所示:
<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>
/*在这里创建表单UI*/
字段1
字段2
字段3
如果表单包含多个字段,则使用bool-in对话框使内容可滚动autoScrollBodyContent={true}
您定义了一个函数this.handleSubmit.bind(this)
,单击submit按钮,在此函数内调用api并提交您想要提交的数据,一旦api调用成功,关闭对话框
如果这解决了您的问题或您想要的任何其他详细信息,请发表意见。您可以在对话框中放置一个,但也必须在表单中放置{actions},而不是actions属性。您的提交操作按钮上应该有type=“Submit”(type=“reset”也受支持,如下所示)
jsFiddle:
const{
对话
TextField,
扁平按钮,
音乐提供者,
格特默姆,
}=材料;
类示例扩展了React.Component{
建造师(道具){
超级(道具);
this.state={open:true};
this.handleClose=this.\u handleClose.bind(this);
}
_handleClose(){
this.setState({open:false});
}
render(){
常量动作=[
,
,
,
];
返回(
{e.preventDefault();alert('Submitted form!');this.handleClose();}>
此对话框跨越屏幕的整个宽度。
{actions}
);
}
}
常量应用=()=>(
);
ReactDOM.render(
,
document.getElementById('容器')
);
在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。如果我想让表单成为一个组件并拥有一个状态本身,我可以使用redux来构建数据状态?是的。组件可以处理状态(比如它的打开状态),或者您可以将其设置为无状态函数,并将“打开”作为一个属性传入(该属性可以由redux设置)。下面是上面转换为无状态函数的组件。您可以使用redux的connect()将其包装起来,将其连接到商店。祝你好运我使我的表单成为一个组件
,如下:
,如何将操作放入表单中?preventDefault阻止表单提交。。。所以这不起作用。即使你自己像JSON一样发送,添加提交也是有益的,因为你可以使用ENTER按钮来提交表单。那么我如何构建表单的状态呢?onSubmit={handleSubmit(this.onSubmit)}
。。。丑陋的
<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>
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')
);
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>
);