forked from beefe/react-native-actionsheet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionSheetCustom.js
More file actions
176 lines (144 loc) · 3.85 KB
/
ActionSheetCustom.js
File metadata and controls
176 lines (144 loc) · 3.85 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
'use strict';
import React, {
View, Text, PropTypes, Dimensions, Animated, TouchableHighlight, TouchableWithoutFeedback
} from 'react-native';
import styles, {btnStyle, sheetStyle, RADIUS} from './styles';
const {width, height} = Dimensions.get('window');
const BUTTON_H = 50;
const DURATION = 250;
const WARN_COLOR = '#ff3b30';
class ActionSheet extends React.Component {
constructor(props) {
super(props);
this.translateY = this._calculateHeight();
this.state = {
visible: false,
fadeAnim: new Animated.Value(0),
sheetAnim: new Animated.Value(this.translateY)
};
}
show() {
this.setState({visible: true});
this._showOverlay();
this._showSheet();
}
hide(index) {
this._hideOverlay(() => this.setState({visible: false}));
this._hideSheet();
this.props.onPress(index);
}
_showOverlay() {
Animated.timing(this.state.fadeAnim, {
toValue: 0.4,
duration: DURATION
}).start();
}
_hideOverlay(callback) {
Animated.timing(this.state.fadeAnim, {
toValue: 0,
duration: DURATION
}).start(callback || function() {});
}
_showSheet() {
Animated.timing(this.state.sheetAnim, {
toValue: 0,
duration: DURATION
}).start();
}
_hideSheet(callback) {
Animated.timing(this.state.sheetAnim, {
toValue: this.translateY,
duration: DURATION
}).start(callback || function() {});
}
_calculateHeight() {
let count = this.props.options.length;
if (this.props.title) count += 1;
return BUTTON_H * count + 20;
}
_renderTitle() {
if (this.props.title) {
return (
<View style={sheetStyle.title}>
<Text style={sheetStyle.titleText}>{this.props.title}</Text>
</View>
);
} else {
return null;
}
}
_renderCancelButton() {
let {options, cancelButtonIndex, tintColor} = this.props;
if (cancelButtonIndex > -1 && options[cancelButtonIndex]) {
return (
<TouchableHighlight
activeOpacity={1}
underlayColor="#f4f4f4"
style={[btnStyle.wrapper, {marginTop: 6}]}
onPress={this.hide.bind(this, cancelButtonIndex)}
>
<Text style={[btnStyle.title, {fontWeight: '700', color: tintColor}]}>{options[cancelButtonIndex]}</Text>
</TouchableHighlight>
);
} else {
return null;
}
}
_createButton(title, fontColor, index, style) {
return (
<TouchableHighlight
key={index}
activeOpacity={1}
underlayColor="#f4f4f4"
style={[btnStyle.wrapper, style || {}]}
onPress={this.hide.bind(this, index)}
>
<Text style={[btnStyle.title, {color: fontColor}]}>{title}</Text>
</TouchableHighlight>
);
}
_renderOptions() {
let {options, tintColor, cancelButtonIndex, destructiveButtonIndex} = this.props;
return options.map((title, index) => {
let fontColor = destructiveButtonIndex === index ? WARN_COLOR : tintColor;
return index === cancelButtonIndex ? null : this._createButton(title, fontColor, index);
});
}
render() {
let state = this.state;
if (state.visible === false) {
return null;
} else {
return (
<View style={styles.wrapper}>
<TouchableWithoutFeedback onPress={this.hide.bind(this)}>
<Animated.View style={[styles.overlay, {opacity: state.fadeAnim}]}></Animated.View>
</TouchableWithoutFeedback>
<Animated.View
ref={o => this.sheet = o}
style={[sheetStyle.wrapper, {transform: [{translateY: state.sheetAnim}]}]}
>
<View style={sheetStyle.options}>
{this._renderTitle()}
{this._renderOptions()}
</View>
{this._renderCancelButton()}
</Animated.View>
</View>
);
}
}
}
ActionSheet.propTypes = {
title: PropTypes.string,
options: PropTypes.array.isRequired,
tintColor: PropTypes.string,
cancelButtonIndex: PropTypes.number,
destructiveButtonIndex: PropTypes.number,
onPress: PropTypes.func
};
ActionSheet.defaultProps = {
tintColor: '#007aff',
onPress: () => {}
};
export default ActionSheet;