realm-js/examples/ReactExample/components/todo-app.js

126 lines
3.3 KiB
JavaScript
Raw Normal View History

2015-10-27 23:19:30 +00:00
/* Copyright 2015 Realm Inc - All Rights Reserved
* Proprietary and Confidential
*/
2015-10-07 23:20:05 +00:00
'use strict';
const React = require('react-native');
const TodoItem = require('./todo-item');
const TodoListView = require('./todo-listview');
2015-10-07 23:20:05 +00:00
const realm = require('./realm');
const styles = require('./styles');
2015-10-07 23:20:05 +00:00
const { NavigatorIOS } = React;
class TodoApp extends React.Component {
constructor(props) {
super(props);
2015-10-07 23:20:05 +00:00
let todoLists = realm.objects('TodoList');
2015-10-07 23:20:05 +00:00
if (todoLists.length < 1) {
realm.write(() => {
realm.create('TodoList', {name: 'Todo List', items: []});
});
}
// This is a Results object, which will live-update.
2015-10-07 23:20:05 +00:00
this.todoLists = todoLists;
this.state = {};
2015-10-07 23:20:05 +00:00
}
get currentListView() {
let refs = this.refs.nav.refs;
return refs.listItemView || refs.listView;
}
2015-10-07 23:20:05 +00:00
render() {
2015-10-27 10:08:18 +00:00
let extraItems = [
{name: 'Complete', items: realm.objects('Todo', 'done = true')},
{name: 'Incomplete', items: realm.objects('Todo', 'done = false')},
];
2015-10-07 23:20:05 +00:00
let route = {
title: 'My Todo Lists',
component: TodoListView,
2015-10-07 23:20:05 +00:00
passProps: {
ref: 'listView',
items: this.todoLists,
2015-10-27 10:08:18 +00:00
extraItems: extraItems,
onPressItem: (list) => this._onPressTodoList(list),
2015-10-07 23:20:05 +00:00
},
2015-10-27 10:08:18 +00:00
backButtonTitle: 'Lists',
2015-10-07 23:20:05 +00:00
rightButtonTitle: 'Add',
onRightButtonPress: () => this._addNewTodoList(),
2015-10-07 23:20:05 +00:00
};
return (
<NavigatorIOS ref="nav" initialRoute={route} style={styles.navigator} />
2015-10-07 23:20:05 +00:00
);
}
_addNewTodoItem(list) {
let items = list.items;
if (!this._shouldAddNewItem(items)) {
return;
}
2015-10-07 23:20:05 +00:00
realm.write(() => {
items.push({text: ''});
2015-10-07 23:20:05 +00:00
});
this._setEditingRow(items.length - 1);
}
_addNewTodoList() {
let items = this.todoLists;
if (!this._shouldAddNewItem(items)) {
return;
}
realm.write(() => {
realm.create('TodoList', {name: '', items: []});
});
this._setEditingRow(items.length - 1);
}
_onPressTodoList(list) {
2015-10-27 10:08:18 +00:00
let items = list.items;
let route = {
title: list.name,
component: TodoListView,
passProps: {
ref: 'listItemView',
2015-10-27 10:08:18 +00:00
items: items,
rowClass: TodoItem,
},
2015-10-27 10:08:18 +00:00
};
// Check if the items are mutable (i.e. List rather than Results).
if (items.push) {
Object.assign(route, {
rightButtonTitle: 'Add',
onRightButtonPress: () => this._addNewTodoItem(list),
});
}
this.refs.nav.push(route);
}
_shouldAddNewItem(items) {
let editingRow = this.currentListView.state.editingRow;
let editingItem = editingRow != null && items[editingRow];
// Don't allow adding a new item if the one being edited is empty.
return !editingItem || !!editingItem.text || !!editingItem.name;
}
_setEditingRow(rowIndex) {
// Update the state on the currently displayed TodoList to edit this new item.
this.currentListView.setState({editingRow: rowIndex});
2015-10-07 23:20:05 +00:00
}
}
module.exports = TodoApp;