forked from nlively/react-native-multipicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti-picker.ios.js
172 lines (143 loc) · 4.35 KB
/
multi-picker.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
/**
* Copyright (c) 2015-present Dave Vedder
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule MultiPickerIOS
*
*/
"use strict";
import React from "react";
import {
StyleSheet,
View,
requireNativeComponent,
UIManager
} from "react-native";
import PropTypes from "prop-types";
const RNMultiPicker = requireNativeComponent("RNMultiPicker", MultiPickerIOS);
const RNMultiPickerConsts = UIManager.getViewManagerConfig("RNMultiPicker")
.Constants;
const PICKER_REF = "picker";
const styles = StyleSheet.create({
multipicker: {
height: RNMultiPickerConsts.ComponentHeight
}
});
export default class MultiPickerIOS extends React.Component {
static propTypes = {
componentData: PropTypes.any,
selectedIndexes: PropTypes.array,
onChange: PropTypes.func
};
constructor(props) {
super(props);
const componentData = [];
const selectedIndexes = [];
React.Children.forEach(props.children, (child, index) => {
const items = [];
let selectedIndex;
if (child.props.selectedIndex) {
selectedIndex = child.props.selectedIndex;
} else if (child.props.initialSelectedIndex && !this.state) {
selectedIndex = child.props.initialSelectedIndex;
} else {
selectedIndex = 0;
}
React.Children.forEach(child.props.children, function(child, idx) {
items.push({ label: child.props.label, value: child.props.value });
});
componentData.push(items);
selectedIndexes.push(selectedIndex);
});
this.state = { componentData, selectedIndexes };
this.onChange = this.onChange.bind(this);
}
static getDerivedStateFromProps(props, state) {
if (props.children) {
const componentData = [];
const selectedIndexes = [];
React.Children.forEach(props.children, child => {
const items = [];
let selectedIndex;
if (child.props.selectedIndex) {
selectedIndex = child.props.selectedIndex;
} else if (child.props.initialSelectedIndex && !state) {
selectedIndex = child.props.initialSelectedIndex;
} else {
selectedIndex = 0;
}
React.Children.forEach(child.props.children, innerChild => {
items.push({
label: innerChild.props.label,
value: innerChild.props.value
});
});
componentData.push(items);
selectedIndexes.push(selectedIndex);
});
state = { componentData, selectedIndexes };
return state;
} else {
return state;
}
}
onChange({ nativeEvent }) {
// Call any change handlers on the component itself
if (this.props.onChange) {
this.props.onChange(nativeEvent);
}
if (this.props.valueChange) {
this.props.valueChange(nativeEvent);
}
// Call any change handlers on the child component picker that changed
// if it has one. Doing it this way rather than storing references
// to child nodes and their onChage props in _stateFromProps because
// React docs imply that may not be a good idea.
React.Children.forEach(this.props.children, function(child, idx) {
if (idx === nativeEvent.component && child.props.onChange) {
child.props.onChange(nativeEvent);
}
});
const nativeProps = {
componentData: this.state.componentData
};
nativeProps.selectedIndexes = this.state.selectedIndexes;
this.refs[PICKER_REF].setNativeProps(nativeProps);
}
render() {
return (
<View style={this.props.style}>
<RNMultiPicker
ref={PICKER_REF}
style={styles.multipicker}
selectedIndexes={this.state.selectedIndexes}
componentData={this.state.componentData}
onChange={this.onChange}
/>
</View>
);
}
}
export class Group extends React.Component {
static propTypes = {
items: PropTypes.array,
selectedIndex: PropTypes.number,
onChange: PropTypes.func
};
render() {
return null;
}
}
// Represents an item in a picker section: the `value` is used for setting /
// getting selection
export class Item extends React.Component {
static propTypes = {
value: PropTypes.any.isRequired, // string or integer basically
label: PropTypes.string.isRequired // for display
};
render() {
return null;
}
}