antD4+,modal Form,成功示例;
来源:8-3 开通城市实现

慕容4009360
2020-03-13
有同学要的可以参考;谢谢老师的帮助。
import React from “react”;
import {Card, Form, Input, Button, Modal} from “antd”;
const FormItem = Form.Item;
export default class FormRef extends React.Component {
formRef = React.createRef();
state = {
visible: false
};
setVisible = (visible) => {
this.setState({
visible: visible
})
};
handleSubmit = (values) => {
let userInfo = this.formRef.current.getFieldsValue();
console.log(userInfo);
};
handleShowModal = () => {
this.setVisible(true)
};
render() {
return (
<div>
<Button onClick={this.handleShowModal}>show modal</Button>
<Card title="登录水平表单" style={{marginTop: 10}}>
</Card>
<Modal
visible={this.state.visible}
onOk={this.handleSubmit}
onCancel={this.setVisible.bind(this,false)} //注意这里要用上bind
>
<Form style={{width: 300}} ref={this.formRef}>
<FormItem name="username">
<Input placeholder="请输入用户名"/>
</FormItem>
<FormItem name="password">
<Input type="password" placeholder="请输入密码"/>
</FormItem>
</Form>
</Modal>
</div>
);
}
}
写回答
3回答
-
非常棒,学习就是要多分享。
022024-08-05 -
慕容4009360
提问者
2020-03-13
可以在modal里destroyOnClose={true}销毁,可以清除原有填写的数据
10 -
河畔一角
2020-03-27
表单重置:this.formRef.current.resetFields();
表单初始化值:
this.formRef.current.setFieldsValue({
note: 'Hello world!',
gender: 'male',
});
012020-12-09
相似问题