Skip to content

Commit 8c2c416

Browse files
committed
Refactor components
1 parent b4fd214 commit 8c2c416

File tree

4 files changed

+102
-73
lines changed

4 files changed

+102
-73
lines changed

src/GoodByeContext.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react';
2+
3+
const GoodByeContext = React.createContext();
4+
5+
export default GoodByeContext;

src/GoodByeProvider.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import invariant from 'invariant';
4+
5+
import GoodByeContext from './GoodByeContext';
6+
7+
class Provider extends React.Component {
8+
constructor(props) {
9+
super(props);
10+
this.state = {
11+
isShow: false
12+
};
13+
this.pass = undefined;
14+
invariant(
15+
React.createContext,
16+
'react-goodbye only support React 16.3+ context api, please upgrade your react to the latest version.'
17+
)
18+
}
19+
20+
handleGetUserConfirm = (message, pass) => {
21+
this.pass = pass;
22+
this.setState({
23+
isShow: true
24+
});
25+
};
26+
27+
handleOk = () => {
28+
this.pass(true);
29+
this.setState({ isShow: false });
30+
};
31+
32+
handleCancel = () => {
33+
this.pass(false);
34+
this.setState({ isShow: false });
35+
};
36+
37+
render() {
38+
return (
39+
<GoodByeContext.Provider
40+
value={{
41+
isShow: this.state.isShow,
42+
handleOk: this.handleOk,
43+
handleCancel: this.handleCancel,
44+
pass: this.pass
45+
}}
46+
>
47+
{this.props.children({
48+
handleGetUserConfirm: this.handleGetUserConfirm
49+
})}
50+
</GoodByeContext.Provider>
51+
);
52+
}
53+
}
54+
55+
Provider.propTypes = {
56+
children: PropTypes.func
57+
}
58+
59+
export default Provider;

src/index.js

Lines changed: 9 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,12 @@
11
import React, { Fragment, createFactory } from 'react';
22
import PropTypes from 'prop-types';
33
import { Prompt as ReactRouterPrompt } from 'react-router';
4-
import invariant from 'invariant';
54

6-
const __DEV__ = process.env.NODE_ENV !== 'production';
7-
const GoodByeContext = React.createContext();
8-
9-
export class Provider extends React.Component {
10-
constructor(props) {
11-
super(props);
12-
this.state = {
13-
isShow: false
14-
};
15-
this.pass = undefined;
16-
invariant(
17-
React.createContext,
18-
'react-goodbye only support React 16.3+ context api, please upgrade your react to the latest version.'
19-
)
20-
}
21-
22-
handleGetUserConfirm = (message, pass) => {
23-
this.pass = pass;
24-
this.setState({
25-
isShow: true
26-
});
27-
};
28-
29-
handleOk = () => {
30-
this.pass(true);
31-
this.setState({ isShow: false });
32-
};
33-
34-
handleCancel = () => {
35-
this.pass(false);
36-
this.setState({ isShow: false });
37-
};
5+
import GoodByeContext from './GoodByeContext';
6+
import Provider from './GoodByeProvider';
7+
import withGoodBye from './withGoodBye';
388

39-
render() {
40-
return (
41-
<GoodByeContext.Provider
42-
value={{
43-
isShow: this.state.isShow,
44-
handleOk: this.handleOk,
45-
handleCancel: this.handleCancel,
46-
pass: this.pass
47-
}}
48-
>
49-
{this.props.children({
50-
handleGetUserConfirm: this.handleGetUserConfirm
51-
})}
52-
</GoodByeContext.Provider>
53-
);
54-
}
55-
}
56-
57-
export const withGoodBye = BaseRouterComponent => {
58-
const factory = createFactory(BaseRouterComponent);
59-
const WithGoodBye = props => (
60-
<Provider>
61-
{({ handleGetUserConfirm }) => (
62-
factory({
63-
...props,
64-
getUserConfirmation: handleGetUserConfirm
65-
})
66-
)}
67-
</Provider>
68-
);
69-
70-
if (__DEV__) {
71-
const baseRouterName = BaseRouterComponent.displayName || BaseRouterComponent.name || 'Component';
72-
WithGoodBye.displayName = `withGoodBye(${baseRouterName})`;
73-
74-
return WithGoodBye;
75-
}
76-
77-
return WithGoodBye;
78-
};
9+
const __DEV__ = process.env.NODE_ENV !== 'production';
7910

8011
class GoodBye extends React.Component {
8112
handleBeforeUnload = evt => {
@@ -121,4 +52,9 @@ GoodBye.defaultProps = {
12152
alertMessage: '' // only work for IE
12253
};
12354

55+
export {
56+
Provider,
57+
withGoodBye,
58+
};
59+
12460
export default GoodBye;

src/withGoodBye.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React from 'react';
2+
import Provider from './GoodByeProvider';
3+
4+
const __DEV__ = process.env.NODE_ENV !== 'production';
5+
6+
const withGoodBye = BaseRouterComponent => {
7+
const factory = React.createFactory(BaseRouterComponent);
8+
const WithGoodBye = props => (
9+
<Provider>
10+
{({ handleGetUserConfirm }) => (
11+
factory({
12+
...props,
13+
getUserConfirmation: handleGetUserConfirm
14+
})
15+
)}
16+
</Provider>
17+
);
18+
19+
if (__DEV__) {
20+
const baseRouterName = BaseRouterComponent.displayName || BaseRouterComponent.name || 'Component';
21+
WithGoodBye.displayName = `withGoodBye(${baseRouterName})`;
22+
23+
return WithGoodBye;
24+
}
25+
26+
return WithGoodBye;
27+
};
28+
29+
export default withGoodBye;

0 commit comments

Comments
 (0)