-
Notifications
You must be signed in to change notification settings - Fork 1
/
NavigationConfirm.tsx
148 lines (117 loc) · 4.34 KB
/
NavigationConfirm.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import * as React from 'react';
import { Action, Location, TransitionPromptHook } from 'history';
import { RouteComponentProps, withRouter } from 'react-router-dom';
import { withHistoryService } from '../hocs';
import { HistoryService } from '../services';
import { NavigationConfirmChildren, WhenPropType } from '../types';
import { isFunction, noop } from '../utils';
const BEFORE_UNLOAD_EVENT = 'beforeunload';
interface IProps extends RouteComponentProps {
unloadMsg?: string;
children: NavigationConfirmChildren;
when?: WhenPropType;
historyService: HistoryService,
}
interface IState {
isActive: boolean,
isOpen: boolean;
action: Action,
nextLocation: Location,
hasError: boolean,
}
class NavigationConfirm extends React.Component<IProps, IState> {
public static defaultProps = {
unloadMsg: 'msg',
when: true,
}
public state: IState = {
isActive: true,
isOpen: false,
action: 'PUSH',
nextLocation: { pathname: '/', search: '', state: '', hash: '' },
hasError: false,
}
public unblock: () => void = noop;
public unlisten: () => void = noop;
public componentDidMount() {
const { block, listen } = this.props.history;
this.unblock = block(this.block);
this.unlisten = listen(this.reblock);
if (this.shouldShow()) {
this.listenUnload();
}
}
public componentDidUpdate(prevProps: IProps) {
if (prevProps.when !== this.props.when) {
if (this.shouldShow()) {
this.listenUnload();
} else {
this.removeUnload();
}
}
}
public componentWillUnmount() {
this.unblock();
this.unlisten();
this.removeUnload();
}
public block: TransitionPromptHook = (nextLocation: Location, action: Action): false | void => {
if (this.shouldBlock(nextLocation)) {
this.setState({ action, nextLocation }, this.open);
return false;
}
return;
}
public reblock = () => {
this.unblock();
this.setState({ isActive: true }, () => {
this.unblock = this.props.history.block(this.block);
});
}
public navigate() {
const { history, historyService } = this.props;
const { action, nextLocation } = this.state;
const historyFunction = historyService.getHistoryFunction(nextLocation, action);
this.setState({ isActive: false, isOpen: false }, () => {
history[historyFunction](nextLocation);
});
}
public onBeforeUnload = (event: BeforeUnloadEvent) => {
const { unloadMsg } = this.props;
event.preventDefault();
event.returnValue = unloadMsg;
return unloadMsg;
}
public render() {
const { children } = this.props;
const { isActive, isOpen, hasError } = this.state;
if (!isActive || !isOpen || hasError) {
return null;
}
return children({
onCancel: this.onCancel,
onConfirm: this.onConfirm,
});
}
private listenUnload = () => window.addEventListener(BEFORE_UNLOAD_EVENT, this.onBeforeUnload);
private removeUnload = () => window.removeEventListener(BEFORE_UNLOAD_EVENT, this.onBeforeUnload);
private onConfirm = () => this.navigate();
private onCancel = () => this.setState({ isOpen: false });
private open = () => this.setState({ isOpen: true });
private isTheSameLocation = (nextLocation: Location): boolean => nextLocation.pathname === this.props.location.pathname;
private shouldBlock = (nextLocation: Location): boolean => this.state.isActive && this.shouldShow() && !this.isTheSameLocation(nextLocation);
private shouldShow = (): boolean => {
const { when, location, history, match } = this.props;
if (isFunction(when)) {
return when(location, { location, history, match });
}
return !!when;
}
}
const NavigationConfirmWithRouter = withRouter(withHistoryService(NavigationConfirm));
export {
NavigationConfirm,
NavigationConfirmWithRouter,
IProps as NavigationConfirmProps,
IState as NavigationConfirmState,
}