|
| 1 | +import React, { PropTypes as T } from 'react' |
| 2 | + |
| 3 | +import { |
| 4 | + View, |
| 5 | + Text, |
| 6 | + StyleSheet, |
| 7 | + TouchableHighlight, |
| 8 | + ListView |
| 9 | +} from 'react-native' |
| 10 | + |
| 11 | +import colors from '../../styles/colors' |
| 12 | +import appStyles from '../../styles/app'; |
| 13 | + |
| 14 | +export class List extends React.Component { |
| 15 | + static propTypes = { |
| 16 | + initialRows: T.array, |
| 17 | + onRowPress: T.func |
| 18 | + } |
| 19 | + |
| 20 | + static defaultProps = { |
| 21 | + initialRows: [] |
| 22 | + } |
| 23 | + |
| 24 | + constructor(props, context) { |
| 25 | + super(props, context); |
| 26 | + |
| 27 | + const dataSource = new ListView.DataSource({ |
| 28 | + sectionHeaderHasChanged: this._sectionHeaderHasChanged.bind(this), |
| 29 | + rowHasChanged: this._rowHasChanged.bind(this) |
| 30 | + }); |
| 31 | + |
| 32 | + const {initialRows} = this.props; |
| 33 | + |
| 34 | + this.state = { |
| 35 | + dataSource: dataSource.cloneWithRows(initialRows) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + _sectionHeaderHasChanged(oldSection, newSection) { |
| 40 | + return oldSection !== newSection; |
| 41 | + } |
| 42 | + |
| 43 | + _rowHasChanged(oldRow, newRow) { |
| 44 | + return oldRow !== newRow; |
| 45 | + } |
| 46 | + |
| 47 | + render() { |
| 48 | + const {style} = this.props; |
| 49 | + const headerStyle = [styles.header, style]; |
| 50 | + |
| 51 | + return ( |
| 52 | + <ListView |
| 53 | + ref="listView" |
| 54 | + scrollRenderAheadDistance={0} |
| 55 | + automaticallyAdjustContentInsets={true} |
| 56 | + dataSource={this.state.dataSource} |
| 57 | + renderSectionHeader={this._renderSectionHeader.bind(this)} |
| 58 | + renderRow={this._renderRow.bind(this)} |
| 59 | + renderFooter={this._renderFooter.bind(this)} |
| 60 | + renderSeparator={this._renderSeparator.bind(this)} |
| 61 | + /> |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + _renderSectionHeader(data, sectionId) { |
| 66 | + return <View></View>; // empty, for now |
| 67 | + return ( |
| 68 | + <View style={styles.sectionHeader}> |
| 69 | + <Text style={styles.sectionHeaderText}>{sectionId}</Text> |
| 70 | + </View> |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + _renderSeparator(sectionID: number, rowID: number, adjacentRowHighlighted: bool) { |
| 75 | + return ( |
| 76 | + <View |
| 77 | + key={`${sectionID}-${rowID}`} |
| 78 | + style={{ |
| 79 | + height: adjacentRowHighlighted ? 4 : 1, |
| 80 | + backgroundColor: adjacentRowHighlighted ? '#3B5998' : '#CCCCCC', |
| 81 | + }} |
| 82 | + /> |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + _defaultRenderRow(rowData, sectionID, rowID, highlightRow) { |
| 87 | + return ( |
| 88 | + <View style={appStyles.row}> |
| 89 | + <Text>{rowData.title}</Text> |
| 90 | + </View> |
| 91 | + ) |
| 92 | + } |
| 93 | + |
| 94 | + _renderRow(rowData, sectionID, rowID, highlightRow) { |
| 95 | + const {renderRow} = this.props; |
| 96 | + |
| 97 | + return ( |
| 98 | + <TouchableHighlight onPress={() => { |
| 99 | + this._pressRow(rowData, sectionID, rowID, highlightRow); |
| 100 | + }}> |
| 101 | + {renderRow ? |
| 102 | + renderRow(rowData, sectionID, rowID, highlightRow) : |
| 103 | + this._defaultRenderRow(rowData, sectionID, rowID, highlightRow)} |
| 104 | + </TouchableHighlight> |
| 105 | + ) |
| 106 | + } |
| 107 | + |
| 108 | + _pressRow(...args) { |
| 109 | + const {onRowPress} = this.props; |
| 110 | + if (onRowPress && typeof onRowPress === 'function') { |
| 111 | + onRowPress(...args); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + _renderFooter() { |
| 116 | + return ( |
| 117 | + <View style={[appStyles.container, styles.scrollSpinner]}> |
| 118 | + <Text></Text> |
| 119 | + </View> |
| 120 | + ) |
| 121 | + } |
| 122 | + |
| 123 | +} |
| 124 | + |
| 125 | +const styles = StyleSheet.create({ |
| 126 | + header: { |
| 127 | + } |
| 128 | +}) |
| 129 | + |
| 130 | +export default List; |
0 commit comments