mock平台

MyPopConfirm.js 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import React, { PureComponent as Component } from 'react';
  2. import { Modal, Button } from 'antd';
  3. import PropTypes from 'prop-types';
  4. // 嵌入到 BrowserRouter 内部,覆盖掉默认的 window.confirm
  5. // http://reacttraining.cn/web/api/BrowserRouter/getUserConfirmation-func
  6. class MyPopConfirm extends Component {
  7. constructor(props) {
  8. super(props);
  9. this.state = {
  10. visible: true
  11. };
  12. }
  13. static propTypes = {
  14. msg: PropTypes.string,
  15. callback: PropTypes.func
  16. };
  17. yes = () => {
  18. this.props.callback(true);
  19. this.setState({ visible: false });
  20. }
  21. no = () => {
  22. this.props.callback(false);
  23. this.setState({ visible: false });
  24. }
  25. componentWillReceiveProps() {
  26. this.setState({ visible: true });
  27. }
  28. render() {
  29. if (!this.state.visible) {
  30. return null;
  31. }
  32. return (<Modal
  33. title="你即将离开编辑页面"
  34. visible={this.state.visible}
  35. onCancel={this.no}
  36. footer={[
  37. <Button key="back" onClick={this.no}>取 消</Button>,
  38. <Button key="submit" onClick={this.yes}>确 定</Button>
  39. ]}
  40. >
  41. <p>{this.props.msg}</p>
  42. </Modal>);
  43. }
  44. }
  45. export default MyPopConfirm;