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();
|
2017-05-15 22:50:14 +02:00
|
|
|
this.previousListMode = 'view';
|
|
|
|
const ds = new ListView.DataSource({
|
|
|
|
rowHasChanged: (r1, r2) => { return r1 !== r2; }
|
|
|
|
});
|
|
|
|
this.state = {
|
|
|
|
dataSource: ds,
|
|
|
|
items: [],
|
|
|
|
selectedItemIds: [],
|
|
|
|
};
|
2017-05-09 22:46:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(newProps) {
|
2017-05-15 22:50:14 +02:00
|
|
|
// When the items have changed, we just pass this to the data source. However,
|
|
|
|
// when the list mode change, we need to clone the items to make sure the whole
|
|
|
|
// list is updated (so that the checkbox can be added or removed).
|
|
|
|
|
|
|
|
let items = newProps.items;
|
|
|
|
|
|
|
|
if (newProps.listMode != this.previousListMode) {
|
|
|
|
items = newProps.items.slice();
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
items[i] = Object.assign({}, items[i]);
|
|
|
|
}
|
|
|
|
this.previousListMode = newProps.listMode;
|
|
|
|
}
|
|
|
|
|
2017-05-09 22:46:54 +02:00
|
|
|
// https://stackoverflow.com/questions/38186114/react-native-redux-and-listview
|
2017-05-15 22:50:14 +02:00
|
|
|
this.setState({
|
|
|
|
dataSource: this.state.dataSource.cloneWithRows(items),
|
|
|
|
});
|
2017-05-09 22:46:54 +02:00
|
|
|
}
|
|
|
|
|
2017-05-15 22:50:14 +02:00
|
|
|
setListMode = (mode) => {
|
|
|
|
this.props.dispatch({
|
|
|
|
type: 'SET_LIST_MODE',
|
|
|
|
listMode: mode,
|
|
|
|
});
|
|
|
|
}
|
2017-05-15 21:46:34 +02:00
|
|
|
|
2017-05-15 22:50:14 +02:00
|
|
|
listView_itemPress = (itemId) => {}
|
|
|
|
|
|
|
|
listView_itemLongPress = (itemId) => {
|
|
|
|
this.setListMode('edit');
|
2017-05-15 21:46:34 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 22:46:54 +02:00
|
|
|
render() {
|
2017-05-15 22:50:14 +02:00
|
|
|
let renderRow = (item) => {
|
2017-05-09 22:46:54 +02:00
|
|
|
let onPress = () => {
|
2017-05-15 22:50:14 +02:00
|
|
|
this.listView_itemPress(item.id);
|
|
|
|
}
|
|
|
|
let onLongPress = () => {
|
|
|
|
this.listView_itemLongPress(item.id);
|
2017-05-09 22:46:54 +02:00
|
|
|
}
|
2017-05-15 22:50:14 +02:00
|
|
|
let editable = this.props.listMode == 'edit' ? ' [X] ' : '';
|
2017-05-09 22:46:54 +02:00
|
|
|
return (
|
2017-05-15 22:50:14 +02:00
|
|
|
<TouchableHighlight onPress={onPress} onLongPress={onLongPress}>
|
|
|
|
<Text>{item.title}<Text>{editable}</Text></Text>
|
2017-05-09 22:46:54 +02:00
|
|
|
</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
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-15 21:10:00 +02:00
|
|
|
export { ItemListComponent };
|