有同学要的可以参考;谢谢老师的帮助。
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>
);
}
}