-
Notifications
You must be signed in to change notification settings - Fork 55
/
MaterialDialog.js
215 lines (206 loc) · 5.8 KB
/
MaterialDialog.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
212
213
214
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {
StyleSheet,
Modal,
Text,
Platform,
TouchableHighlight,
KeyboardAvoidingView,
TouchableWithoutFeedback,
View,
Dimensions,
} from 'react-native';
import colors from './colors';
const {height, width} = Dimensions.get('window');
// TODO: Don't rely on Dimensions for the actions footer layout
// TODO: Support custom actions
// TODO: Stacked full-width buttons
class ActionButton extends Component {
render() {
return (
<TouchableHighlight
style={styles.actionContainer}
underlayColor={colors.androidPressedUnderlay}
onPress={this.props.onPress}>
<Text
style={[styles.actionText, { color: this.props.colorAccent }]}>
{this.props.label}
</Text>
</TouchableHighlight>
);
}
}
export default class MaterialDialog extends Component {
render() {
return (
<Modal
animationType={'fade'}
transparent
hardwareAccelerated
visible={this.props.visible}
onRequestClose={this.props.onCancel}>
<TouchableWithoutFeedback onPress={this.props.onCancel}>
<View style={styles.backgroundOverlay}>
<KeyboardAvoidingView behavior={Platform.OS === 'ios' ? 'padding' : null}>
<View style={[styles.modalContainer, { backgroundColor: this.props.backgroundColor }]}>
<TouchableWithoutFeedback>
<View>
{this.props.title != null
? <View
style={this.props.scrolled
? styles.titleContainerScrolled
: styles.titleContainer}>
<Text
style={[styles.titleText, { color: this.props.titleColor }]}>
{this.props.title}
</Text>
</View>
: null}
<View
style={this.props.scrolled
? styles.contentContainerScrolled
: styles.contentContainer}>
{this.props.children}
</View>
{this.props.onOk != null && this.props.onCancel != null
? <View
style={this.props.scrolled
? styles.actionsContainerScrolled
: styles.actionsContainer}>
<ActionButton
colorAccent={this.props.colorAccent}
onPress={this.props.onCancel}
label={this.props.cancelLabel} />
<ActionButton
colorAccent={this.props.colorAccent}
onPress={this.props.onOk}
label={this.props.okLabel} />
</View>
: null}
</View>
</TouchableWithoutFeedback>
</View>
</KeyboardAvoidingView>
</View>
</TouchableWithoutFeedback>
</Modal>
);
}
}
const styles = StyleSheet.create({
backgroundOverlay: {
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: colors.backgroundOverlay,
},
modalContainer: {
marginHorizontal: 16,
marginVertical: 106,
paddingTop: 24,
minWidth: 280,
borderRadius: 2,
elevation: 24,
overflow: 'hidden',
},
titleContainer: {
paddingHorizontal: 24,
paddingBottom: 20,
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
},
titleContainerScrolled: {
paddingHorizontal: 24,
paddingBottom: 20,
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
borderBottomWidth: StyleSheet.hairlineWidth,
borderColor: colors.androidBorderColor,
},
titleText: {
fontSize: 20,
...Platform.select({
android: {
fontFamily: 'sans-serif-medium',
},
ios: {
fontWeight: '600'
}
})
},
contentContainer: {
flex: -1,
paddingHorizontal: 24,
paddingBottom: 24,
},
contentContainerScrolled: {
flex: -1,
maxHeight: height - 264, // (106px vertical margin * 2) + 52px
paddingHorizontal: 24,
},
actionsContainer: {
height: 52,
flexDirection: 'row',
justifyContent: 'flex-end',
alignItems: 'center',
paddingLeft: 8,
},
actionsContainerScrolled: {
height: 52,
flexDirection: 'row',
justifyContent: 'flex-end',
alignItems: 'center',
paddingLeft: 8,
borderTopWidth: StyleSheet.hairlineWidth,
borderColor: colors.androidBorderColor,
},
actionContainer: {
marginRight: 8,
paddingHorizontal: 8,
paddingVertical: 8,
minWidth: 64,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
},
actionText: {
fontSize: 14,
...Platform.select({
android: {
fontFamily: 'sans-serif-medium',
},
ios: {
fontWeight: '600'
}
})
},
});
MaterialDialog.propTypes = {
visible: PropTypes.bool.isRequired,
children: PropTypes.element.isRequired,
onCancel: PropTypes.func.isRequired,
onOk: PropTypes.func,
cancelLabel: PropTypes.string,
okLabel: PropTypes.string,
title: PropTypes.string,
titleColor: PropTypes.string,
colorAccent: PropTypes.string,
scrolled: PropTypes.bool,
}
MaterialDialog.defaultProps = {
okLabel: 'OK',
cancelLabel: 'CANCEL',
titleColor: colors.androidPrimaryTextColor,
backgroundColor: colors.background,
colorAccent: colors.androidColorAccent,
scrolled: false,
};
ActionButton.propTypes = {
colorAccent: PropTypes.string.isRequired,
label: PropTypes.string.isRequired,
onPress: PropTypes.func.isRequired,
};