-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathPrompt.js
54 lines (43 loc) · 1.01 KB
/
Prompt.js
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
import React from "react"
import PropTypes from "prop-types"
import { history as historyType } from "./PropTypes"
class Prompt extends React.Component {
static propTypes = {
when: PropTypes.bool,
message: PropTypes.oneOfType([PropTypes.func, PropTypes.string]).isRequired
}
static contextTypes = {
history: historyType.isRequired
}
static defaultProps = {
when: true
}
enable(message) {
if (this.unblock) this.unblock()
this.unblock = this.context.history.block(message)
}
disable() {
if (this.unblock) {
this.unblock()
this.unblock = null
}
}
componentWillMount() {
if (this.props.when) this.enable(this.props.message)
}
componentWillReceiveProps(nextProps) {
if (nextProps.when) {
if (!this.props.when || this.props.message !== nextProps.message)
this.enable(nextProps.message)
} else {
this.disable()
}
}
componentWillUnmount() {
this.disable()
}
render() {
return null
}
}
export default Prompt