-
Notifications
You must be signed in to change notification settings - Fork 20
/
Picker.js
83 lines (76 loc) · 2.26 KB
/
Picker.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
import React, { Component } from 'react'
import ReactNative, {
Platform, View, TouchableOpacity, Text, StyleSheet, ActionSheetIOS
} from 'react-native'
export default class Picker extends Component {
static Item = ReactNative.Picker.Item
constructor(props, context) {
super(props, context)
this.onPress = this.handlePress.bind(this)
}
handlePress() {
const { children, onValueChange, prompt } = this.props
const labels = React.Children.map(children, (child) => child.props.label)
const values = React.Children.map(children, (child) => child.props.value)
ActionSheetIOS.showActionSheetWithOptions(
{
title: prompt,
options: [...labels, "Cancel"],
cancelButtonIndex: labels.length,
},
index => {
if (index < labels.length) {
onValueChange(values[index])
}
}
)
}
render() {
const { children, style, textStyle } = this.props
const labels = React.Children.map(children, (child) => child.props.label)
const values = React.Children.map(children, (child) => child.props.value)
const flatStyle = (style ? StyleSheet.flatten(style) : {})
if (Platform.OS === 'ios') {
const { selectedValue } = this.props
const defaultTextStyle = {
fontSize: 12,
lineHeight: (flatStyle.height ? flatStyle.height : 12),
}
return(
<TouchableOpacity
onPress={this.onPress}
style={[{
alignSelf: 'stretch',
alignItems: 'center',
justifyContent: 'center',
flexDirection: 'row',
paddingHorizontal: 6,
}, flatStyle]}
>
<Text
style={[{ flex: 1 }, defaultTextStyle, textStyle]}
ellipsizeMode="tail"
numberOfLines={1}
>
{labels[values.indexOf(selectedValue)]}
</Text>
<Text style={[{color: 'black'}, defaultTextStyle, textStyle]}>▼</Text>
</TouchableOpacity>
)
} else {
return (
<View
style={[{
alignSelf: 'stretch',
paddingHorizontal: 6,
}, flatStyle]}
>
<ReactNative.Picker
{...this.props}
style={textStyle}
/>
</View>
)
}
}
}