|
1 | 1 | import React, { Fragment, createFactory } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
2 | 3 | import { Prompt as ReactRouterPrompt } from 'react-router'; |
3 | | -import invariant from 'invariant'; |
4 | 4 |
|
5 | | -const __DEV__ = process.env.NODE_ENV !== 'production'; |
6 | | -const GoodByeContext = React.createContext(); |
| 5 | +import GoodByeContext from './GoodByeContext'; |
| 6 | +import Provider from './GoodByeProvider'; |
| 7 | +import withGoodBye from './withGoodBye'; |
7 | 8 |
|
8 | | -export class Provider extends React.Component { |
9 | | - constructor(props) { |
10 | | - super(props); |
11 | | - this.state = { |
12 | | - isShow: false |
13 | | - }; |
14 | | - this.pass = undefined; |
15 | | - invariant( |
16 | | - React.createContext, |
17 | | - 'react-goodbye only support React 16.3+ context api, please upgrade your react to the latest version.' |
18 | | - ) |
19 | | - } |
| 9 | +const __DEV__ = process.env.NODE_ENV !== 'production'; |
20 | 10 |
|
21 | | - handleGetUserConfirm = (message, pass) => { |
22 | | - this.pass = pass; |
23 | | - this.setState({ |
24 | | - isShow: true |
25 | | - }); |
| 11 | +class GoodBye extends React.Component { |
| 12 | + handleBeforeUnload = evt => { |
| 13 | + const { alertMessage } = this.props; |
| 14 | + evt.returnValue = alertMessage; |
| 15 | + return alertMessage; |
26 | 16 | }; |
27 | 17 |
|
28 | | - handleOk = () => { |
29 | | - this.pass(true); |
30 | | - this.setState({ isShow: false }); |
31 | | - }; |
| 18 | + componentDidUpdate() { |
| 19 | + const { when, alertBeforeUnload } = this.props; |
| 20 | + window.onbeforeunload = when && alertBeforeUnload |
| 21 | + ? this.handleBeforeUnload |
| 22 | + : null; |
| 23 | + } |
32 | 24 |
|
33 | | - handleCancel = () => { |
34 | | - this.pass(false); |
35 | | - this.setState({ isShow: false }); |
36 | | - }; |
| 25 | + componentWillUnmount() { |
| 26 | + window.onbeforeunload = null; |
| 27 | + } |
37 | 28 |
|
38 | 29 | render() { |
| 30 | + const { when, children } = this.props; |
39 | 31 | return ( |
40 | | - <GoodByeContext.Provider |
41 | | - value={{ |
42 | | - isShow: this.state.isShow, |
43 | | - handleOk: this.handleOk, |
44 | | - handleCancel: this.handleCancel, |
45 | | - pass: this.pass |
46 | | - }} |
47 | | - > |
48 | | - {this.props.children({ |
49 | | - handleGetUserConfirm: this.handleGetUserConfirm |
50 | | - })} |
51 | | - </GoodByeContext.Provider> |
| 32 | + <Fragment> |
| 33 | + <ReactRouterPrompt when={when} message="" /> |
| 34 | + <GoodByeContext.Consumer> |
| 35 | + {renderProps => children({ ...renderProps })} |
| 36 | + </GoodByeContext.Consumer> |
| 37 | + </Fragment> |
52 | 38 | ); |
53 | 39 | } |
54 | 40 | } |
55 | 41 |
|
56 | | -export const withGoodBye = BaseRouterComponent => { |
57 | | - const factory = createFactory(BaseRouterComponent); |
58 | | - const WithGoodBye = props => ( |
59 | | - <Provider> |
60 | | - {({ handleGetUserConfirm }) => ( |
61 | | - factory({ |
62 | | - ...props, |
63 | | - getUserConfirmation: handleGetUserConfirm |
64 | | - }) |
65 | | - )} |
66 | | - </Provider> |
67 | | - ); |
68 | | - |
69 | | - if (__DEV__) { |
70 | | - const baseRouterName = BaseRouterComponent.displayName || BaseRouterComponent.name || 'Component'; |
71 | | - WithGoodBye.displayName = `withGoodBye(${baseRouterName})`; |
72 | | - |
73 | | - return WithGoodBye; |
74 | | - } |
| 42 | +GoodBye.propTypes = { |
| 43 | + when: PropTypes.bool, |
| 44 | + alertBeforeUnload: PropTypes.bool, |
| 45 | + alertMessage: PropTypes.string, |
| 46 | + children: PropTypes.func.isRequired, |
| 47 | +}; |
75 | 48 |
|
76 | | - return WithGoodBye; |
| 49 | +GoodBye.defaultProps = { |
| 50 | + when: false, |
| 51 | + alertBeforeUnload: false, |
| 52 | + alertMessage: '' // only work for IE |
77 | 53 | }; |
78 | 54 |
|
79 | | -export default ({ when = false, children }) => { |
80 | | - return ( |
81 | | - <Fragment> |
82 | | - <ReactRouterPrompt when={when} message="" /> |
83 | | - <GoodByeContext.Consumer> |
84 | | - {renderProps => children({ ...renderProps })} |
85 | | - </GoodByeContext.Consumer> |
86 | | - </Fragment> |
87 | | - ); |
| 55 | +export { |
| 56 | + Provider, |
| 57 | + withGoodBye, |
88 | 58 | }; |
| 59 | + |
| 60 | +export default GoodBye; |
0 commit comments