forked from aksonov/react-native-tableview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
296 lines (268 loc) · 10.8 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
'use strict';
import React, { Component } from 'react';
import {
NativeMethodsMixin,
ReactNativeViewAttributes,
NativeModules,
StyleSheet,
View,
requireNativeComponent,
EdgeInsetsPropType,
PointPropType,
} from 'react-native';
var RNTableViewConsts = NativeModules.UIManager.RNTableView.Constants;
var resolveAssetSource = require('react-native/Libraries/Image/resolveAssetSource');
var TABLEVIEW = 'tableview';
function extend(el, map) {
for (var i in map)
if (typeof(map[i])!='object')
el[i] = map[i];
return el;
}
var TableView = React.createClass({
mixins: [NativeMethodsMixin],
propTypes: {
onPress: React.PropTypes.func,
onAccessoryPress: React.PropTypes.func,
onWillDisplayCell: React.PropTypes.func,
onEndDisplayingCell: React.PropTypes.func,
selectedValue: React.PropTypes.any, // string or integer basically
autoFocus: React.PropTypes.bool,
moveWithinSectionOnly: React.PropTypes.bool,
json: React.PropTypes.string,
textColor: React.PropTypes.string,
detailTextColor: React.PropTypes.string,
tintColor: React.PropTypes.string,
footerLabel: React.PropTypes.string,
headerFont: React.PropTypes.number,
headerTextColor: React.PropTypes.string,
footerTextColor: React.PropTypes.string,
separatorColor: React.PropTypes.string,
/**
* The amount by which the content is inset from the edges
* of the TableView. Defaults to `{0, 0, 0, 0}`.
* @platform ios
*/
contentInset: EdgeInsetsPropType,
/**
* Used to manually set the starting scroll offset.
* The default value is `{x: 0, y: 0}`.
* @platform ios
*/
contentOffset: PointPropType,
/**
* The amount by which the scroll view indicators are inset from the
* edges of the TableView. This should normally be set to the same
* value as the `contentInset`. Defaults to `contentInset` or
* `{0, 0, 0, 0}`.
* @platform ios
*/
scrollIndicatorInsets: EdgeInsetsPropType,
tableViewCellEditingStyle: React.PropTypes.number,
},
getDefaultProps() {
return {
tableViewCellEditingStyle: RNTableViewConsts.CellEditingStyle.Delete,
};
},
getInitialState: function() {
return this._stateFromProps(this.props);
},
componentWillReceiveProps: function(nextProps) {
var state = this._stateFromProps(nextProps);
this.setState(state);
},
// Translate TableView prop and children into stuff that RNTableView understands.
_stateFromProps: function(props) {
var sections = [];
var additionalItems = [];
var children = [];
var json = props.json;
// iterate over sections
React.Children.forEach(props.children, function (section, index) {
var items=[];
var count = 0;
if (section && section.type==TableView.Section) {
let customCells = false;
React.Children.forEach(section.props.children, function (child, itemIndex) {
var el = {};
extend(el, section.props);
extend(el, child.props);
if (el.children) {
el.label = el.children;
}
if (el.image && typeof el.image === 'number') {
el.image = resolveAssetSource(el.image);
}
count++;
items.push(el);
if (child.type==TableView.Cell){
customCells = true;
count++;
var element = React.cloneElement(child, {key: index+" "+itemIndex, section: index, row: itemIndex});
children.push(element);
}
});
sections.push({
customCells,
label: section.props.label,
footerLabel: section.props.footerLabel,
footerHeight: section.props.footerHeight,
headerHeight: section.props.headerHeight,
items: items,
count: count
});
} else if (section && section.type==TableView.Item){
var el = extend({},section.props);
if (!el.label){
el.label = el.children;
}
additionalItems.push(el);
} else if (section){
children.push(section);
}
});
this.sections = sections;
return {sections, additionalItems, children, json};
},
render: function() {
return (
<View style={[{flex:1},this.props.style]}>
<RNTableView
ref={TABLEVIEW}
style={this.props.style}
sections={this.state.sections}
additionalItems={this.state.additionalItems}
tableViewStyle={TableView.Consts.Style.Plain}
tableViewCellStyle={TableView.Consts.CellStyle.Subtitle}
tableViewCellEditingStyle={this.props.tableViewCellEditingStyle}
separatorStyle={TableView.Consts.SeparatorStyle.Line}
scrollIndicatorInsets={this.props.contentInset}
{...this.props}
json={this.state.json}
onPress={this._onPress}
onAccessoryPress={this._onAccessoryPress}
onChange={this._onChange}
onWillDisplayCell={this._onWillDisplayCell}
onEndDisplayingCell={this._onEndDisplayingCell}>
{this.state.children}
</RNTableView>
</View>
);
},
_onPress: function(event) {
var data = event.nativeEvent;
if (this.sections[data.selectedSection] && this.sections[data.selectedSection].items[data.selectedIndex] &&
this.sections[data.selectedSection] && this.sections[data.selectedSection].items[data.selectedIndex].onPress){
this.sections[data.selectedSection] && this.sections[data.selectedSection].items[data.selectedIndex].onPress(data);
}
if (this.props.onPress) {
this.props.onPress(data);
}
event.stopPropagation();
},
_onAccessoryPress: function(event) {
console.log('_onAccessoryPress', event);
var data = event.nativeEvent;
if (this.sections[data.selectedSection] && this.sections[data.selectedSection].items[data.accessoryIndex] &&
this.sections[data.selectedSection] && this.sections[data.selectedSection].items[data.accessoryIndex].onAccessoryPress){
this.sections[data.selectedSection] && this.sections[data.selectedSection].items[data.accessoryIndex].onAccessoryPress(data);
}
if (this.props.onAccessoryPress) {
this.props.onAccessoryPress(data);
}
event.stopPropagation();
},
_onChange: function(event) {
var data = event.nativeEvent;
if (this.sections[data.selectedSection] && this.sections[data.selectedSection].items[data.selectedIndex] &&
this.sections[data.selectedSection] && this.sections[data.selectedSection].items[data.selectedIndex].onChange){
this.sections[data.selectedSection] && this.sections[data.selectedSection].items[data.selectedIndex].onChange(data);
}
if (this.props.onChange) {
this.props.onChange(data);
}
event.stopPropagation();
},
_onWillDisplayCell: function(event) {
var data = event.nativeEvent;
if (this.sections[data.section] && this.sections[data.section].items[data.row] && this.sections[data.section].items[data.row].onWillDisplayCell) {
this.sections[data.section].items[data.row].onWillDisplayCell(data);
}
if (this.props.onWillDisplayCell) {
this.props.onWillDisplayCell(data);
}
event.stopPropagation();
},
_onEndDisplayingCell: function(event) {
var data = event.nativeEvent;
if (this.sections[data.section] && this.sections[data.section].items[data.row] && this.sections[data.section].items[data.row].onEndDisplayingCell) {
this.sections[data.section].items[data.row].onEndDisplayingCell(data);
}
if (this.props.onEndDisplayingCell) {
this.props.onEndDisplayingCell(data);
}
event.stopPropagation();
},
});
TableView.Item = React.createClass({
propTypes: {
value: React.PropTypes.any, // string or integer basically
label: React.PropTypes.string,
},
render: function() {
// These items don't get rendered directly.
return null;
},
});
TableView.Footer = React.createClass({
getInitialState(){
return {width:0, height:0}
},
render: function() {
return <RNFooterView onLayout={(event)=>{this.setState(event.nativeEvent.layout)}} {...this.props} componentWidth={this.state.width} componentHeight={this.state.height}/>
},
});
var RNFooterView = requireNativeComponent('RNTableFooterView', null);
TableView.Header = React.createClass({
getInitialState(){
return {width:0, height:0}
},
render: function() {
return <RNHeaderView onLayout={(event)=>{this.setState(event.nativeEvent.layout)}} {...this.props} componentWidth={this.state.width} componentHeight={this.state.height}/>
},
});
var RNHeaderView = requireNativeComponent('RNTableHeaderView', null);
TableView.Cell = React.createClass({
getInitialState(){
return {width:0, height:0}
},
render: function() {
return <RNCellView onLayout={(event)=>{this.setState(event.nativeEvent.layout)}} {...this.props} componentWidth={this.state.width} componentHeight={this.state.height}/>
},
});
var RNCellView = requireNativeComponent('RNCellView', null);
TableView.Section = React.createClass({
propTypes: {
label: React.PropTypes.string,
footerLabel: React.PropTypes.string,
arrow: React.PropTypes.bool,
footerHeight: React.PropTypes.number,
headerHeight: React.PropTypes.number,
},
render: function() {
// These items don't get rendered directly.
return null;
},
});
var styles = StyleSheet.create({
tableView: {
// The picker will conform to whatever width is given, but we do
// have to set the component's height explicitly on the
// surrounding view to ensure it gets rendered.
//height: RNTableViewConsts.ComponentHeight,
},
});
TableView.Consts = RNTableViewConsts;
var RNTableView = requireNativeComponent('RNTableView', null);
module.exports = TableView;