forked from GeekyAnts/NativeBase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPicker.ios.js
More file actions
271 lines (251 loc) · 7.24 KB
/
Picker.ios.js
File metadata and controls
271 lines (251 loc) · 7.24 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* eslint-disable react/prefer-stateless-function */
/* eslint-disable react/prefer-es6-class */
/* eslint-disable react/sort-comp */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import createReactClass from 'create-react-class';
import { FlatList, Modal, View, ViewPropTypes } from 'react-native';
import { Picker } from '@react-native-picker/picker';
import { connectStyle } from 'native-base-shoutem-theme';
import { find, get } from 'lodash';
import computeProps from '../utils/computeProps';
import mapPropsToStyleNames from '../utils/mapPropsToStyleNames';
import { Text } from './Text';
import { Radio } from './Radio';
import { Container } from './Container';
import { Content } from './Content';
import { ListItem } from './ListItem';
import { Button } from './Button';
import { Header } from './Header';
import { Title } from './Title';
import { Left } from './Left';
import { Right } from './Right';
import { Body } from './Body';
class PickerNB extends Component {
constructor(props) {
super(props);
this.state = {
modalVisible: false,
currentLabel: this.getLabel(props),
dataSource: this.getChildren(props.children)
};
}
getInitialStyle = () => {
return {
picker: {
// alignItems: 'flex-end'
},
pickerItem: {}
};
};
getLabel(props) {
const children = this.getChildren(props.children);
const item = find(
children,
child => child.props.value === props.selectedValue
);
return get(item, 'props.label');
}
getSelectedItem() {
return find(
this.props.children,
child => child.props.value === this.props.selectedValue
);
}
getChildren = children => {
if (children && !Array.isArray(children)) {
return [].concat(children);
}
// eslint-disable-next-line prefer-spread
const appliedChildren = [].concat.apply([], children);
return appliedChildren;
};
prepareRootProps() {
const defaultProps = {
style: this.getInitialStyle().picker,
itemStyle: this.getInitialStyle().pickerItem
};
return computeProps(this.props, defaultProps);
}
_setModalVisible(visible) {
this.setState({ modalVisible: visible });
}
renderIcon() {
return React.cloneElement(this.props.iosIcon, {
style: [
{
fontSize: 22,
lineHeight: 26
},
{ ...this.props.iosIcon.props.style }
]
});
}
renderButton() {
const onPress = () => {
if (this.props.enabled !== undefined && !this.props.enabled) return;
this._setModalVisible(true);
};
const text = this.state.currentLabel
? this.state.currentLabel
: this.props.placeholder;
if (this.props.renderButton) {
return this.props.renderButton({
onPress,
text,
picker: this,
selectedItem: this.getSelectedItem()
});
}
return (
<Button
style={this.props.style}
dark
picker
transparent
onPress={onPress}
>
{this.state.currentLabel ? (
<Text
style={[this.props.textStyle]}
note={this.props.note}
numberOfLines={1}
ellipsizeMode="tail"
>
{this.state.currentLabel}
</Text>
) : (
<Text
style={[this.props.textStyle, this.props.placeholderStyle]}
note={this.props.note !== false}
numberOfLines={1}
ellipsizeMode="tail"
>
{this.props.placeholder}
</Text>
)}
{this.props.iosIcon === undefined ? null : this.renderIcon()}
</Button>
);
}
renderHeader() {
return this.props.renderHeader ? (
this.props.renderHeader(() => this._setModalVisible(false))
) : (
<Header style={this.props.headerStyle}>
<Left>
<Button
style={{
shadowOffset: null,
shadowColor: null,
shadowRadius: null,
shadowOpacity: null,
marginLeft: 3,
...this.props.headerBackButtonStyle
}}
transparent
onPress={() => {
this._setModalVisible(false);
}}
>
<Text style={this.props.headerBackButtonTextStyle}>
{this.props.headerBackButtonText || 'Back'}
</Text>
</Button>
</Left>
<Body>
<Title style={this.props.headerTitleStyle}>
{this.props.iosHeader || 'Select One'}
</Title>
</Body>
<Right />
</Header>
);
}
// eslint-disable-next-line camelcase
UNSAFE_componentWillReceiveProps(nextProps) {
const currentLabel = this.state.currentLabel;
const nextLabel = this.getLabel(nextProps);
const currentDS = this.state.dataSource;
const nextDS = this.getChildren(nextProps.children);
if (currentLabel !== nextLabel) {
this.setState({
currentLabel: nextLabel
});
}
if (currentDS !== nextDS) {
this.setState({
dataSource: nextDS
});
}
}
render() {
return (
<View ref={c => (this._root = c)}>
{this.renderButton()}
<Modal
// supportedOrientations={this.props.supportedOrientations || null}
supportedOrientations={['portrait', 'landscape']}
animationType="slide"
transparent={false}
visible={this.state.modalVisible}
onRequestClose={() => {
this._setModalVisible(false);
}}
>
<Container style={this.props.modalStyle}>
{this.renderHeader()}
<Content>
<FlatList
testID={this.props.testID}
data={this.state.dataSource}
keyExtractor={(item, index) => String(index)}
renderItem={({ item }) => (
<ListItem
selected={item.props.value === this.props.selectedValue}
button
style={this.props.itemStyle}
onPress={() => {
this._setModalVisible(false);
this.props.onValueChange(item.props.value, item.key);
this.setState({ current: item.props.label });
}}
>
<Left>
<Text style={this.props.itemTextStyle}>
{item.props.label}
</Text>
</Left>
<Right>
{item.props.value === this.props.selectedValue ? (
<Radio selected />
) : (
<Radio selected={false} />
)}
</Right>
</ListItem>
)}
/>
</Content>
</Container>
</Modal>
</View>
);
}
}
// eslint-disable-next-line react/no-multi-comp
PickerNB.Item = createReactClass({
render() {
return <Picker.Item {...this.props()} />;
}
});
PickerNB.propTypes = {
...ViewPropTypes,
renderButton: PropTypes.func
};
const StyledPickerNB = connectStyle(
'NativeBase.PickerNB',
{},
mapPropsToStyleNames
)(PickerNB);
export { StyledPickerNB as PickerNB };