2017-05-09 22:46:54 +02:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { connect } from 'react-redux'
|
|
|
|
import { ListView, Text, TouchableHighlight } from 'react-native';
|
2017-05-11 22:14:01 +02:00
|
|
|
import { Log } from 'src/log.js';
|
2017-05-09 22:46:54 +02:00
|
|
|
import { _ } from 'src/locale.js';
|
|
|
|
|
|
|
|
class ItemListComponent extends Component {
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
|
|
|
|
this.state = { dataSource: ds };
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillMount() {
|
|
|
|
const newDataSource = this.state.dataSource.cloneWithRows(this.props.notes);
|
|
|
|
this.state = { dataSource: newDataSource };
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(newProps) {
|
|
|
|
// https://stackoverflow.com/questions/38186114/react-native-redux-and-listview
|
2017-05-11 22:14:01 +02:00
|
|
|
this.setState({ dataSource: this.state.dataSource.cloneWithRows(newProps.notes) });
|
2017-05-09 22:46:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
let renderRow = (rowData) => {
|
|
|
|
let onPress = () => {
|
|
|
|
this.props.onItemClick(rowData.id)
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<TouchableHighlight onPress={onPress}>
|
|
|
|
<Text>{rowData.title}</Text>
|
|
|
|
</TouchableHighlight>
|
|
|
|
);
|
|
|
|
}
|
2017-05-12 21:54:06 +02:00
|
|
|
|
|
|
|
// `enableEmptySections` is to fix this warning: https://github.com/FaridSafi/react-native-gifted-listview/issues/39
|
2017-05-09 22:46:54 +02:00
|
|
|
return (
|
|
|
|
<ListView
|
|
|
|
dataSource={this.state.dataSource}
|
|
|
|
renderRow={renderRow}
|
2017-05-12 21:54:06 +02:00
|
|
|
enableEmptySections={true}
|
2017-05-09 22:46:54 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ItemList = connect(
|
|
|
|
(state) => {
|
|
|
|
return { notes: state.notes };
|
|
|
|
},
|
|
|
|
(dispatch) => {
|
|
|
|
return {
|
|
|
|
onItemClick: (noteId) => {
|
|
|
|
dispatch({
|
2017-05-10 21:21:09 +02:00
|
|
|
type: 'Navigation/NAVIGATE',
|
|
|
|
routeName: 'Note',
|
|
|
|
noteId: noteId,
|
2017-05-09 22:46:54 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)(ItemListComponent)
|
|
|
|
|
|
|
|
export { ItemList };
|