-
Notifications
You must be signed in to change notification settings - Fork 302
/
Modal.js
87 lines (73 loc) · 1.82 KB
/
Modal.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
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
// @flow
import React, { Component } from 'react';
import Sibling from 'react-native-root-siblings';
import BaseModal from './components/BaseModal';
import BottomModal from './components/BottomModal';
import type { ModalProps } from './type';
type State = {
visible: boolean
}
export default class Modal extends Component<ModalProps, State> {
static BottomModal = BottomModal
constructor(props: ModalProps) {
super(props);
this.state = {
visible: props.visible,
};
}
componentDidMount() {
const { visible } = this.state;
if (visible) {
this.createModal();
}
}
componentDidUpdate(prevProps: ModalProps, prevState: State) {
// update visible state and create dialog if visible is true
if (prevState.visible !== this.props.visible) {
// will use getDerivedStateFromProps in future, then don't need to setState
// on componentDidUpdate
// eslint-disable-next-line
this.setState({ visible: this.props.visible });
if (this.props.visible) {
this.createModal();
}
}
// always re-render if sibling is not null
if (this.sibling) {
this.updateModal();
}
}
handleDismiss = () => {
const { onDismiss } = this.props;
if (onDismiss) {
onDismiss();
}
this.destroyModal();
}
sibling: Sibling = null
createModal() {
// Protect against setState happening asynchronously
if (!this.sibling) {
this.sibling = new Sibling(this.renderModal());
}
}
updateModal() {
this.sibling.update(this.renderModal());
}
destroyModal() {
this.sibling.destroy();
this.sibling = null;
}
renderModal() {
return (
<BaseModal
{...this.props}
onDismiss={this.handleDismiss}
visible={this.state.visible}
/>
);
}
render() {
return null;
}
}