Add delete buttons for todo items in Example app

This commit is contained in:
Scott Kyle 2015-10-26 17:13:33 -07:00
parent f89c44697a
commit 8c680ffbf6
4 changed files with 42 additions and 1 deletions

View File

@ -10,7 +10,6 @@ module.exports = React.StyleSheet.create({
backgroundColor: '#ffffff', backgroundColor: '#ffffff',
}, },
listItem: { listItem: {
paddingRight: 12,
borderColor: "#c8c7cc", borderColor: "#c8c7cc",
borderBottomWidth: 0.5, borderBottomWidth: 0.5,
alignItems: 'stretch', alignItems: 'stretch',
@ -45,6 +44,12 @@ module.exports = React.StyleSheet.create({
flex: 1, flex: 1,
lineHeight: 30, lineHeight: 30,
}, },
listItemDelete: {
paddingLeft: 12,
paddingRight: 12,
flexDirection: 'column',
justifyContent: 'center',
},
instructions: { instructions: {
textAlign: 'center', textAlign: 'center',
color: '#333333', color: '#333333',

View File

@ -0,0 +1,20 @@
'use strict';
const React = require('react-native');
const styles = require('./styles');
const { Text, TouchableWithoutFeedback, View } = React;
class TodoItemDelete extends React.Component {
render() {
return (
<TouchableWithoutFeedback onPress={this.props.onPress}>
<View style={styles.listItemDelete}>
<Text>𐄂</Text>
</View>
</TouchableWithoutFeedback>
);
}
}
module.exports = TodoItemDelete;

View File

@ -2,6 +2,7 @@
const React = require('react-native'); const React = require('react-native');
const TodoItemCheckbox = require('./todo-item-checkbox'); const TodoItemCheckbox = require('./todo-item-checkbox');
const TodoItemDelete = require('./todo-item-delete');
const realm = require('./realm'); const realm = require('./realm');
const styles = require('./styles'); const styles = require('./styles');
@ -26,6 +27,7 @@ class TodoItem extends React.Component {
render() { render() {
let item = this.props.item; let item = this.props.item;
let deleteButton;
let contents; let contents;
if (this.props.editing) { if (this.props.editing) {
@ -48,12 +50,17 @@ class TodoItem extends React.Component {
{item.text} {item.text}
</Text> </Text>
); );
deleteButton = (
<TodoItemDelete onPress={this.props.onPressDelete} />
);
} }
return ( return (
<View style={styles.listItem}> <View style={styles.listItem}>
<TodoItemCheckbox checked={item.done} onPress={this._onPressCheckbox} /> <TodoItemCheckbox checked={item.done} onPress={this._onPressCheckbox} />
{contents} {contents}
{deleteButton}
</View> </View>
); );
} }

View File

@ -39,6 +39,7 @@ class TodoList extends React.Component {
item={item} item={item}
editing={this.state.editingRow == rowIndex} editing={this.state.editingRow == rowIndex}
onPress={() => this._onPressRow(rowIndex)} onPress={() => this._onPressRow(rowIndex)}
onPressDelete={() => this._onPressDeleteRow(rowIndex)}
onEndEditing={() => this._onEndEditingRow(rowIndex)} /> onEndEditing={() => this._onEndEditingRow(rowIndex)} />
); );
} }
@ -53,6 +54,14 @@ class TodoList extends React.Component {
this.setState({editingRow: rowIndex}); this.setState({editingRow: rowIndex});
} }
_onPressDeleteRow(rowIndex) {
let items = this.props.list.items;
realm.write(() => items.splice(rowIndex, 1));
this.forceUpdate();
}
_onEndEditingRow(rowIndex) { _onEndEditingRow(rowIndex) {
let items = this.props.list.items; let items = this.props.list.items;