-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
77 lines (68 loc) · 2.24 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
'use strict';
var React = require('react-native');
var { NativeMethodsMixin, requireNativeComponent } = React;
var QUILTVIEW = 'quiltview';
var RNQuiltView = requireNativeComponent('RNQuiltView', null);
function extend(el, map) {
for (var i in map)
if (typeof(map[i])!='object')
el[i] = map[i];
return el;
}
var QuiltView = React.createClass({
mixins: [NativeMethodsMixin],
getInitialState: function() {
return this._stateFromProps(this.props);
},
componentWillReceiveProps: function(nextProps) {
var state = this._stateFromProps(nextProps);
this.setState(state);
},
// Translate QuiltView prop and children into stuff that RNQuiltView understands.
_stateFromProps: function(props) {
var sections = [];
var children = [];
// iterate over sections
React.Children.forEach(props.children, function (section, index) {
var items=[];
var count = 0;
if (section && section.type == QuiltView.Section) {
React.Children.forEach(section.props.children, function (child, itemIndex) {
var el = {};
extend(el, child.props);
el.key = child.key;
count++;
items.push(el);
children.push(child);
});
sections.push({
items: items,
count: count
});
} else if (section){
children.push(section);
}
});
this.sections = sections;
return {sections, children};
},
render: function() {
var children = this.state.children;
var refreshControl = this.props.refreshControl;
return <RNQuiltView
ref={QUILTVIEW}
style={this.props.style}
sections={this.state.sections}
{...this.props} >
{refreshControl}
{children}
</RNQuiltView>;
},
});
QuiltView.Section = React.createClass({
render: function() {
// These items don't get rendered directly.
return null;
},
});
module.exports = QuiltView;