-
Notifications
You must be signed in to change notification settings - Fork 457
/
Copy pathselect.ios.js
185 lines (166 loc) · 5.1 KB
/
select.ios.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
import React from "react";
import PropTypes from "prop-types";
import { Animated, View, TouchableOpacity, Text, Picker } from "react-native";
const UIPICKER_HEIGHT = 216;
class CollapsiblePickerIOS extends React.Component {
constructor(props) {
super(props);
const isCollapsed =
typeof this.props.locals.isCollapsed === typeof true
? this.props.locals.isCollapsed
: true;
this.state = {
isCollapsed,
height: new Animated.Value(isCollapsed ? 0 : UIPICKER_HEIGHT)
};
this.animatePicker = this._animatePicker.bind(this);
this.togglePicker = this._togglePicker.bind(this);
this.onValueChange = this._onValueChange.bind(this);
}
_animatePicker(isCollapsed) {
const locals = this.props.locals;
let animation = Animated.timing;
let animationConfig = {
duration: 200
};
if (locals.config) {
if (locals.config.animation) {
animation = locals.config.animation;
}
if (locals.config.animationConfig) {
animationConfig = locals.config.animationConfig;
}
}
animation(
this.state.height,
Object.assign(
{
toValue: isCollapsed ? 0 : UIPICKER_HEIGHT
},
animationConfig
)
).start();
}
_togglePicker() {
this.setState({ isCollapsed: !this.state.isCollapsed }, () => {
this.animatePicker(this.state.isCollapsed);
if (typeof this.props.locals.onCollapseChange === "function") {
this.props.locals.onCollapseChange(this.state.isCollapsed);
}
});
}
_onValueChange(val) {
const { onChange, value } = this.props.locals;
if (val !== value) {
this.togglePicker();
onChange(val);
}
}
render() {
const locals = this.props.locals;
const { stylesheet } = locals;
let pickerContainer = stylesheet.pickerContainer.normal;
let pickerContainerOpen = stylesheet.pickerContainer.open;
let selectStyle = stylesheet.select.normal;
let touchableStyle = stylesheet.pickerTouchable.normal;
let touchableStyleActive = stylesheet.pickerTouchable.active;
let pickerValue = stylesheet.pickerValue.normal;
if (locals.hasError) {
pickerContainer = stylesheet.pickerContainer.error;
selectStyle = stylesheet.select.error;
touchableStyle = stylesheet.pickerTouchable.error;
pickerValue = stylesheet.pickerValue.error;
}
if (locals.disabled) {
touchableStyle = stylesheet.pickerTouchable.notEditable;
}
const options = locals.options.map(({ value, text }) => (
<Picker.Item key={value} value={value} label={text} />
));
const selectedOption = locals.options.find(
option => option.value === locals.value
);
const height = this.state.isCollapsed ? 0 : UIPICKER_HEIGHT;
return (
<View
style={[
pickerContainer,
!this.state.isCollapsed ? pickerContainerOpen : {}
]}
>
<TouchableOpacity
disabled={locals.disabled}
style={[
touchableStyle,
this.state.isCollapsed ? {} : touchableStyleActive
]}
onPress={this.togglePicker}
>
<Text style={pickerValue}>{selectedOption.text}</Text>
</TouchableOpacity>
<Animated.View
style={{ height: this.state.height, overflow: "hidden" }}
>
<Picker
accessibilityLabel={locals.label}
ref="input"
style={selectStyle}
selectedValue={locals.value}
onValueChange={this.onValueChange}
help={locals.help}
enabled={!locals.disabled}
mode={locals.mode}
prompt={locals.prompt}
itemStyle={locals.itemStyle}
>
{options}
</Picker>
</Animated.View>
</View>
);
}
}
CollapsiblePickerIOS.propTypes = {
locals: PropTypes.object.isRequired
};
function select(locals) {
if (locals.hidden) {
return null;
}
const stylesheet = locals.stylesheet;
let formGroupStyle = stylesheet.formGroup.normal;
let controlLabelStyle = stylesheet.controlLabel.normal;
let selectStyle = stylesheet.select.normal;
let helpBlockStyle = stylesheet.helpBlock.normal;
let errorBlockStyle = stylesheet.errorBlock;
if (locals.hasError) {
formGroupStyle = stylesheet.formGroup.error;
controlLabelStyle = stylesheet.controlLabel.error;
selectStyle = stylesheet.select.error;
helpBlockStyle = stylesheet.helpBlock.error;
}
const label = locals.label ? (
<Text style={controlLabelStyle}>{locals.label}</Text>
) : null;
const help = locals.help ? (
<Text style={helpBlockStyle}>{locals.help}</Text>
) : null;
const error =
locals.hasError && locals.error ? (
<Text accessibilityLiveRegion="polite" style={errorBlockStyle}>
{locals.error}
</Text>
) : null;
var options = locals.options.map(({ value, text }) => (
<Picker.Item key={value} value={value} label={text} />
));
return (
<View style={formGroupStyle}>
{label}
<CollapsiblePickerIOS locals={locals} />
{help}
{error}
</View>
);
}
module.exports = select;