forked from d-a-n/react-native-modal-picker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
224 lines (198 loc) · 8.19 KB
/
index.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
215
216
217
218
219
220
221
222
223
224
'use strict';
import React from 'react';
import PropTypes from 'prop-types';
import {
View,
Modal,
Text,
ScrollView,
TouchableOpacity,
TouchableWithoutFeedback,
Platform,
ViewPropTypes as RNViewPropTypes,
} from 'react-native';
import styles from './style';
const ViewPropTypes = RNViewPropTypes || View.propTypes;
let componentIndex = 0;
const propTypes = {
data: PropTypes.array,
onChange: PropTypes.func,
initValue: PropTypes.string,
animationType: Modal.propTypes.animationType,
style: ViewPropTypes.style,
selectStyle: ViewPropTypes.style,
selectTextStyle: Text.propTypes.style,
optionStyle: ViewPropTypes.style,
optionTextStyle: Text.propTypes.style,
optionContainerStyle: ViewPropTypes.style,
sectionStyle: ViewPropTypes.style,
childrenContainerStyle: ViewPropTypes.style,
touchableStyle: ViewPropTypes.style,
sectionTextStyle: Text.propTypes.style,
cancelContainerStyle: ViewPropTypes.style,
cancelStyle: ViewPropTypes.style,
cancelTextStyle: Text.propTypes.style,
overlayStyle: ViewPropTypes.style,
cancelText: PropTypes.string,
disabled: PropTypes.bool,
supportedOrientations: Modal.propTypes.supportedOrientations,
keyboardShouldPersistTaps: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
backdropPressToClose: PropTypes.bool,
accessible: PropTypes.bool,
scrollViewAccessibilityLabel: PropTypes.string,
cancelButtonAccessibilityLabel: PropTypes.string,
passThruProps: PropTypes.object,
};
const defaultProps = {
data: [],
onChange: () => {},
initValue: 'Select me!',
animationType: 'slide',
style: {},
selectStyle: {},
selectTextStyle: {},
optionStyle: {},
optionTextStyle: {},
optionContainerStyle: {},
sectionStyle: {},
childrenContainerStyle: {},
touchableStyle: {},
sectionTextStyle: {},
cancelContainerStyle: {},
cancelStyle: {},
cancelTextStyle: {},
overlayStyle: {},
cancelText: 'cancel',
disabled: false,
supportedOrientations: ['portrait', 'landscape'],
keyboardShouldPersistTaps: 'always',
backdropPressToClose: false,
accessible: false,
scrollViewAccessibilityLabel: undefined,
cancelButtonAccessibilityLabel: undefined,
passThruProps: {},
};
export default class ModalSelector extends React.Component {
constructor(props) {
super(props);
this.state = {
modalVisible: false,
selected: props.initValue,
cancelText: props.cancelText,
changedItem: undefined,
};
}
componentWillReceiveProps(nextProps) {
if (nextProps.initValue !== this.props.initValue) {
this.setState({selected: nextProps.initValue});
}
}
onChange = (item) => {
if (Platform.OS === 'android' || !Modal.propTypes.onDismiss) {
// RN >= 0.50 on iOS comes with the onDismiss prop for Modal which solves RN issue #10471
this.props.onChange(item);
}
this.setState({selected: item.label, changedItem: item });
this.close();
}
close = () => {
this.setState({
modalVisible: false,
});
}
open = () => {
this.setState({
modalVisible: true,
changedItem: undefined,
});
}
renderSection = (section) => {
return (
<View key={section.key} style={[styles.sectionStyle,this.props.sectionStyle]}>
<Text style={[styles.sectionTextStyle,this.props.sectionTextStyle]}>{section.label}</Text>
</View>
);
}
renderOption = (option, isLastItem) => {
return (
<TouchableOpacity
key={option.key}
onPress={() => this.onChange(option)}
accessible={this.props.accessible}
accessibilityLabel={option.accessibilityLabel || undefined}
{...this.props.passThruProps}
>
<View style={[styles.optionStyle, this.props.optionStyle, isLastItem &&
{borderBottomWidth: 0}]}>
<Text style={[styles.optionTextStyle,this.props.optionTextStyle]}>{option.label}</Text>
</View>
</TouchableOpacity>);
}
renderOptionList = () => {
let options = this.props.data.map((item, index) => {
if (item.section) {
return this.renderSection(item);
}
return this.renderOption(item, index === this.props.data.length - 1);
});
const closeOverlay = this.props.backdropPressToClose;
return (
<TouchableWithoutFeedback key={'modalSelector' + (componentIndex++)} onPress={() => {
closeOverlay && this.close();
}}>
<View style={[styles.overlayStyle, this.props.overlayStyle]}>
<View style={[styles.optionContainer, this.props.optionContainerStyle]}>
<ScrollView keyboardShouldPersistTaps={this.props.keyboardShouldPersistTaps} accessible={this.props.accessible} accessibilityLabel={this.props.scrollViewAccessibilityLabel}>
<View style={{paddingHorizontal: 10}}>
{options}
</View>
</ScrollView>
</View>
<View style={[styles.cancelContainer, this.props.cancelContainerStyle]}>
<TouchableOpacity onPress={this.close} accessible={this.props.accessible} accessibilityLabel={this.props.cancelButtonAccessibilityLabel}>
<View style={[styles.cancelStyle, this.props.cancelStyle]}>
<Text style={[styles.cancelTextStyle,this.props.cancelTextStyle]}>{this.props.cancelText}</Text>
</View>
</TouchableOpacity>
</View>
</View>
</TouchableWithoutFeedback>);
}
renderChildren = () => {
if(this.props.children) {
return this.props.children;
}
return (
<View style={[styles.selectStyle, this.props.selectStyle]}>
<Text style={[styles.selectTextStyle, this.props.selectTextStyle]}>{this.state.selected}</Text>
</View>
);
}
render() {
const dp = (
<Modal
transparent={true}
ref={element => this.model = element}
supportedOrientations={this.props.supportedOrientations}
visible={this.state.modalVisible}
onRequestClose={this.close}
animationType={this.props.animationType}
onDismiss={() => this.state.changedItem && this.props.onChange(this.state.changedItem)}
>
{this.renderOptionList()}
</Modal>
);
return (
<View style={this.props.style} {...this.props.passThruProps}>
{dp}
<TouchableOpacity style={this.props.touchableStyle} onPress={this.open} disabled={this.props.disabled}>
<View style={this.props.childrenContainerStyle} pointerEvents="none">
{this.renderChildren()}
</View>
</TouchableOpacity>
</View>
);
}
}
ModalSelector.propTypes = propTypes;
ModalSelector.defaultProps = defaultProps;