-
Notifications
You must be signed in to change notification settings - Fork 6
/
Dialog.js
211 lines (201 loc) · 5.75 KB
/
Dialog.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* react-native-md-dialog
* @author ahmed<[email protected]>
*/
import React, {
Component,
PropTypes
} from 'react';
import {
View,
TouchableWithoutFeedback,
Dimensions,
StyleSheet,
TouchableNativeFeedback,
TouchableOpacity,
Animated,
Easing,
Text,
Platform
} from 'react-native';
const isAndroid = Platform.OS === 'android';
let {
width: deviceWidth
} = Dimensions.get('window');
export default class Dialog extends Component {
constructor(props) {
super(props);
this.state = {
visible: false
}
let animationDuration = props.animationDuration;
this.animatedVal = new Animated.Value(0);
this.animationTimingStart = Animated.timing(
this.animatedVal,
{
toValue: 1,
duration: animationDuration,
easing: Easing.easing
}
);
this.animationTimingEnd = Animated.timing(
this.animatedVal,
{
toValue: 0,
duration: animationDuration,
easing: Easing.easing
}
);
}
open() {
this.setState({ visible: true }, () => this.animationTimingStart.start());
}
close() {
this.animationTimingEnd.start(() => this.setState({ visible: false }));
}
render() {
let {
actions,
children,
style,
title,
titleColor,
dismissable,
backgroundColor,
maxHeight,
width,
} = this.props;
let leftActions, rightActions;
if (actions) {
leftActions = actions.filter(({ props }) => props.position === 'left');
rightActions = actions.filter(({ props }) => props.position !== 'left');
}
return (
this.state.visible && <Animated.View style={[styles.wrapper, { opacity: this.animatedVal }]}>
<TouchableWithoutFeedback onPress={() => dismissable && this.close() }>
<View style={styles.dismissWrapper}/>
</TouchableWithoutFeedback>
<Animated.View style={[styles.container, style, { opacity: this.animatedVal, transform: [{ scale: 1 }], width, maxHeight, backgroundColor }]}>
<Text style={[styles.title, { color: titleColor }]}>{title}</Text>
<View style={{ flex: 1 }}>
{children}
</View>
{actions && <View style={styles.footer}>
<View style={{ flex: 1, flexDirection: 'row' }}>
{leftActions}
</View>
<View style={{ flex: 1, justifyContent: 'flex-end', flexDirection: 'row' }}>
{rightActions}
</View>
</View>}
</Animated.View>
</Animated.View>
);
}
}
const Touchable = (props, state) => (
isAndroid ? <TouchableNativeFeedback {...props} {...state}/> : <TouchableOpacity {...props} {...state}/>
)
export const DialogButton = ({ text, onPress, color, disabled }) => (
<Touchable disabled={disabled} onPress={() => onPress() }>
<View style={styles.dialogBtnContainer}>
<Text type='medium' style={[styles.dialogBtn, { color: color? color: disabled? '#bdbdbd':'#009688' }]}>{text}</Text>
</View>
</Touchable>
);
DialogButton.propTypes = {
text: PropTypes.string,
onPress: PropTypes.func,
color: PropTypes.string,
disabled: PropTypes.bool,
position: PropTypes.string,
}
DialogButton.defaultProps = {
disabled: false,
position: 'right'
}
const styles = StyleSheet.create({
wrapper: {
position: 'absolute',
left: 0,
right: 0,
top: 0,
bottom: 0,
backgroundColor: 'rgba(0,0,0,0.6)',
justifyContent: 'center',
alignItems: 'center',
opacity: 0,
zIndex: 9999,
elevation: 9999,
},
dismissWrapper: {
position: 'absolute',
left: 0,
right: 0,
top: 0,
bottom: 0
},
container: {
elevation: 5,
minHeight: 96,
borderRadius: 2,
opacity: 0,
shadowColor: 'black',
shadowOpacity: 0.2,
shadowRadius: 5,
shadowOffset: {
width: 1,
height: 2
},
},
title: {
fontSize: 20,
margin: 16,
fontWeight: isAndroid ? '400' : '500',
fontFamily: isAndroid ? 'sans-serif-medium' : 'System'
},
footer: {
justifyContent: 'center',
flexDirection: 'row',
marginTop: 8,
marginBottom: 8,
marginHorizontal: 16
},
dialogBtnContainer: {
minWidth: 48,
paddingHorizontal: 8,
paddingVertical: 8,
borderRadius: 8,
justifyContent: 'center',
alignItems: 'center'
},
dialogBtn: {
fontWeight: isAndroid ? '400' : '500',
fontFamily: isAndroid ? 'sans-serif-medium' : 'System',
fontSize: isAndroid ? 14 : 13,
}
})
Dialog.propTypes = {
actions: PropTypes.array,
children: PropTypes.object,
style: PropTypes.oneOfType([
PropTypes.array,
PropTypes.object,
]),
title: PropTypes.string,
titleColor: PropTypes.string,
backgroundColor: PropTypes.string,
dismissable: PropTypes.bool,
animationDuration: PropTypes.number,
width: PropTypes.number,
maxHeight: PropTypes.number,
}
Dialog.defaultProps = {
title: 'Dialog',
backgroundColor: 'white',
titleColor: 'rgba(0,0,0,0.8)',
dismissable: true,
animationDuration: 200,
width: deviceWidth - 48,
maxHeight: 420,
}