-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ios.js
108 lines (97 loc) · 2.66 KB
/
index.ios.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
var React = require('react');
var Component = React.Component;
var styles = require('./components/styles');
var {
AppRegistry,
StyleSheet,
Text,
View,
TouchableHighlight,
TextInput,
ListView
} = require('react-native');
var Firebase = require('firebase');
class Troop extends Component {
constructor(props) {
super(props);
var myFirebaseRef =
new Firebase('https://intense-inferno-962.firebaseio.com/');
this.itemsRef = myFirebaseRef.child('items');
var listParams = {
rowHasChanged: function(row1, row2) {
return row1 !== row2;
}
};
this.state = {
newTodo:'',
todoSource: new ListView.DataSource(listParams)
};
this.items = [];
}
componentDidMount() {
this.itemsRef.on('child_added', (dataSnapshot) => {
this.items.push({id: dataSnapshot.key(), text: dataSnapshot.val().todo});
this.setState({
todoSource: this.state.todoSource.cloneWithRows(this.items)
});
});
this.itemsRef.on('child_removed', (dataSnapshot) => {
this.items = this.items.filter((x) => x.id !== dataSnapshot.key());
this.setState({
todoSource: this.state.todoSource.cloneWithRows(this.items)
});
});
}
addTodo() {
if (this.state.newTodo !=='') {
this.itemsRef.push({
todo: this.state.newTodo
});
this.setState({
newTodo: ''
});
}
}
removeTodo(rowData) {
this.itemsRef.child(rowData.id).remove();
}
render() {
return (
<View style={styles.appContainer}>
<View style={styles.titleView}>
<Text style={styles.titleView}>
My Todos
</Text>
</View>
<View style={styles.inputcontainer}>
<TextInput style={styles.input} onChangeText={(text) => this.setState({newTodo: text})} value={this.state.newTodo} />
<TouchableHighlight
style={styles.button}
onPress={() => this.addTodo()}
underlayColor='#dddddd'>
<Text style={styles.btnText}>Add!</Text>
</TouchableHighlight>
</View>
<ListView
enableEmptySections={true}
dataSource={this.state.todoSource}
renderRow={this.renderRow.bind(this)} />
</View>
)
}
renderRow(rowData) {
return (
<TouchableHighlight
underlayColor='#dddddd'
onPress={() => this.removeTodo(rowData)}>
<View>
<View style={styles.row}>
<Text style={styles.todoText}>{rowData.text}</Text>
</View>
<View style={styles.separator} />
</View>
</TouchableHighlight>
)
}
}
AppRegistry.registerComponent('Troop', () => Troop);