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

195 lines
5.1 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 {
Navigator,
Text,
TouchableOpacity,
} = React;
2015-10-07 23:20:05 +00:00
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'});
2015-10-07 23:20:05 +00:00
});
}
// This is a Results object, which will live-update.
2015-10-07 23:20:05 +00:00
this.todoLists = todoLists;
// Bind all the methods that we will be passing as props.
this.renderScene = this.renderScene.bind(this);
this._addNewTodoList = this._addNewTodoList.bind(this);
this._onPressTodoList = this._onPressTodoList.bind(this);
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: this._onPressTodoList,
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
};
let navigationBar = (
<Navigator.NavigationBar routeMapper={RouteMapper} style={styles.navBar} />
);
2015-10-07 23:20:05 +00:00
return (
<Navigator
ref="nav"
initialRoute={route}
navigationBar={navigationBar}
renderScene={this.renderScene}
sceneStyle={styles.navScene}
style={styles.navigator}
/>
2015-10-07 23:20:05 +00:00
);
}
renderScene(route) {
return <route.component {...route.passProps} />
}
_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: ''});
});
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
}
}
const RouteMapper = {
LeftButton(route, navigator, index, navState) {
if (index == 0) {
return null;
}
let prevRoute = navState.routeStack[index - 1];
return (
<TouchableOpacity
onPress={() => navigator.pop()}
style={styles.navBarLeftButton}>
<Text style={styles.navBarText}>
<Text style={styles.navBarLeftArrow}></Text>
{prevRoute.backButtonTitle || prevRoute.title || 'Back'}
</Text>
</TouchableOpacity>
);
},
RightButton(route) {
if (!route.rightButtonTitle) {
return null;
}
return (
<TouchableOpacity
onPress={route.onRightButtonPress}
style={styles.navBarRightButton}>
<Text style={styles.navBarText}>
{route.rightButtonTitle}
</Text>
</TouchableOpacity>
);
},
Title(route) {
return (
<Text style={[styles.navBarText, styles.navBarTitle]}>
{route.title}
</Text>
);
},
};
2015-10-07 23:20:05 +00:00
module.exports = TodoApp;