1 |
|
2 |
|
3 |
|
4 |
|
5 | import React, {
|
6 | Component,
|
7 | PropTypes,
|
8 | } from 'react';
|
9 | import {
|
10 | StyleSheet,
|
11 | View,
|
12 | Animated,
|
13 | } from 'react-native';
|
14 |
|
15 | import Overlay from '../Overlay';
|
16 |
|
17 | const NOOP = () => {};
|
18 |
|
19 | const styles = StyleSheet.create({
|
20 | container: {
|
21 | position: 'absolute',
|
22 | bottom: 0,
|
23 | left: 0,
|
24 | right: 0,
|
25 | height: 0,
|
26 | },
|
27 | sheet: {
|
28 | position: 'absolute',
|
29 | left: 0,
|
30 | right: 0,
|
31 | },
|
32 | });
|
33 |
|
34 | class Sheet extends Component {
|
35 | constructor(props) {
|
36 | super(props);
|
37 | this.state = {
|
38 | overlayVisible: props.visible,
|
39 | animDistance: new Animated.Value(0),
|
40 | };
|
41 |
|
42 | this.height = null;
|
43 |
|
44 | this.aniShow = this.makeAnimation(this.height || 0);
|
45 | this.aniHide = this.makeAnimation(0);
|
46 |
|
47 | this.getHeight = this.getHeight.bind(this);
|
48 | }
|
49 |
|
50 | componentWillMount() {
|
51 | if (this.props.visible) {
|
52 | this.show();
|
53 | }
|
54 | }
|
55 |
|
56 | componentWillReceiveProps(props) {
|
57 | if (props.visible && !this.props.visible) {
|
58 |
|
59 | this.setState({
|
60 | overlayVisible: true,
|
61 | });
|
62 | this.show();
|
63 | } else if (!props.visible && this.props.visible) {
|
64 |
|
65 | this.hide();
|
66 | }
|
67 | }
|
68 |
|
69 | getHeight(e) {
|
70 | const { height } = e.nativeEvent.layout;
|
71 |
|
72 | if (height !== this.height) {
|
73 | this.height = height;
|
74 | this.aniShow = this.makeAnimation(-height);
|
75 | }
|
76 | }
|
77 |
|
78 | makeAnimation(toValue) {
|
79 | return Animated.timing(this.state.animDistance, {
|
80 | toValue,
|
81 | duration: this.props.duration,
|
82 | });
|
83 | }
|
84 |
|
85 |
|
86 | show() {
|
87 | if (this.height === null) {
|
88 |
|
89 | setTimeout(() => {
|
90 | this.show();
|
91 | }, 10);
|
92 | return;
|
93 | }
|
94 |
|
95 | this.aniHide.stop();
|
96 | this.aniShow.start();
|
97 | }
|
98 |
|
99 | hide() {
|
100 | this.aniShow.stop();
|
101 | this.aniHide.start((e) => {
|
102 | if (e.finished) {
|
103 | this.setState({
|
104 | overlayVisible: false,
|
105 | });
|
106 | this.props.onClose();
|
107 | }
|
108 | });
|
109 | }
|
110 |
|
111 | render() {
|
112 | return (
|
113 | <Overlay
|
114 | visible={this.state.overlayVisible}
|
115 | style={this.props.overlayStyle}
|
116 | onPress={this.props.onPressOverlay}
|
117 | >
|
118 | <View style={styles.container}>
|
119 | <Animated.View
|
120 | style={[styles.sheet, {
|
121 | top: this.state.animDistance,
|
122 | }, this.props.style]}
|
123 | onLayout={this.getHeight}
|
124 | >
|
125 | {this.props.children}
|
126 | </Animated.View>
|
127 | </View>
|
128 | </Overlay>
|
129 | );
|
130 | }
|
131 | }
|
132 |
|
133 | Sheet.propTypes = {
|
134 |
|
135 | style: View.propTypes.style,
|
136 |
|
137 | visible: PropTypes.bool.isRequired,
|
138 |
|
139 | overlayStyle: View.propTypes.style,
|
140 |
|
141 | onClose: PropTypes.func,
|
142 |
|
143 | onPressOverlay: PropTypes.func,
|
144 |
|
145 | children: PropTypes.oneOfType([PropTypes.element, PropTypes.array]),
|
146 |
|
147 | duration: PropTypes.number,
|
148 | };
|
149 | Sheet.defaultProps = {
|
150 | style: null,
|
151 | visible: false,
|
152 | overlayStyle: null,
|
153 | onClose: NOOP,
|
154 | onPressOverlay: NOOP,
|
155 | children: null,
|
156 | duration: 200,
|
157 | };
|
158 |
|
159 | export default Sheet;
|