fix: popmpt使用自定义confirm

This commit is contained in:
wenbo.dong 2017-09-29 13:53:53 +08:00
parent 3f4c787e57
commit 5185774cbe
3 changed files with 61 additions and 2 deletions

View File

@ -1,4 +1,5 @@
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { Route, BrowserRouter as Router } from 'react-router-dom';
@ -7,6 +8,7 @@ import User from './containers/User/User.js';
import Header from './components/Header/Header';
import Footer from './components/Footer/Footer';
import Loading from './components/Loading/Loading';
import MyPopConfirm from './components/MyPopConfirm/MyPopConfirm';
import { checkLoginState } from './reducer/modules/user';
import { requireAuthentication } from './components/AuthenticatedComponent';
@ -46,7 +48,13 @@ export default class App extends Component {
return <Loading visible />;
} else {
r = (
<Router >
<Router getUserConfirmation={(msg, callback) => {
let container = document.createElement('div');
document.body.appendChild(container);
ReactDOM.render((
<MyPopConfirm msg={msg} callback={callback} />
), container);
}}>
<div className="g-main">
<div className="router-main">
{this.props.loginState !== 1 ? <Header /> : null}

View File

@ -0,0 +1,50 @@
import React, { Component } from 'react';
import { Modal, Button } from 'antd';
import PropTypes from 'prop-types';
// 嵌入到 BrowserRouter 内部,覆盖掉默认的 window.confirm
// http://reacttraining.cn/web/api/BrowserRouter/getUserConfirmation-func
class MyPopConfirm extends Component {
constructor(props) {
super(props);
this.state = {
visible: true
};
}
static propTypes = {
msg: PropTypes.string,
callback: PropTypes.func
};
yes = () => {
this.props.callback(true);
this.setState({ visible: false });
}
no = () => {
this.props.callback(false);
this.setState({ visible: false });
}
componentWillReceiveProps() {
this.setState({ visible: true });
}
render() {
if (!this.state.visible) {
return null;
}
return (<Modal
title="你即将离开编辑页面"
visible={this.state.visible}
footer={[
<Button key="back" onClick={this.no}> </Button>,
<Button key="submit" onClick={this.yes}> </Button>
]}
>
<p>{this.props.msg}</p>
</Modal>);
}
}
export default MyPopConfirm;

View File

@ -135,7 +135,8 @@ class Content extends Component {
<Prompt
when={this.state.curtab === 'edit' && this.props.editStatus ? true : false}
message={() => {
this.showModal();
// this.showModal();
console.log('e');
return '离开页面会丢失当前编辑的内容,确定要离开吗?';
}}
/>