realm-js/examples/ReactExample/index.ios.js

199 lines
5.1 KiB
JavaScript
Raw Normal View History

2015-08-13 16:12:48 +00:00
/**
* Sample React Native App
* https://github.com/facebook/react-native
*/
'use strict';
var Realm = require('realm');
2015-10-06 20:52:49 +00:00
var React = require('react-native');
2015-08-13 16:12:48 +00:00
var {
AppRegistry,
StyleSheet,
2015-08-24 16:22:37 +00:00
NavigatorIOS,
AlertIOS,
2015-08-13 16:12:48 +00:00
ListView,
2015-08-24 16:22:37 +00:00
TouchableHighlight,
2015-08-13 16:12:48 +00:00
Text,
2015-08-24 16:22:37 +00:00
TextInput,
2015-08-13 16:12:48 +00:00
View,
} = React;
2015-08-24 16:22:37 +00:00
var TodoItemSchema = {
name: 'Todo',
2015-08-13 16:12:48 +00:00
properties: [
{name: 'text', type: Realm.Types.STRING},
2015-08-13 16:12:48 +00:00
]
};
2015-08-24 16:22:37 +00:00
var TodoListSchmea = {
name: 'TodoList',
properties: [
{name: 'name', type: Realm.Types.STRING},
2015-09-30 17:51:04 +00:00
{name: 'items', type: Realm.Types.LIST, objectType: 'Todo'}
2015-08-24 16:22:37 +00:00
]
2015-08-13 16:12:48 +00:00
};
2015-08-24 16:22:37 +00:00
console.log(Realm.defaultPath);
var realm = new Realm({schema: [TodoItemSchema, TodoListSchmea]});
class Edit extends React.Component {
componentWillMount() {
this.setState({text: this.props.text});
2015-08-13 16:12:48 +00:00
}
2015-08-24 16:22:37 +00:00
save() {
realm.write(function () {
2015-08-28 20:37:03 +00:00
if (this.props.todoId == this.props.list.items.length) {
2015-08-28 20:34:53 +00:00
this.props.list.items.push({text: this.state.text});
2015-08-24 16:22:37 +00:00
}
else {
2015-08-28 20:34:53 +00:00
var todoItem = this.props.list.items[this.props.todoId];
todoItem.text = this.state.text;
2015-08-24 16:22:37 +00:00
}
2015-08-28 20:34:53 +00:00
}.bind(this));
2015-08-24 16:22:37 +00:00
// should not be needed once we have notifications
this.props.parent.updateDataSource();
this.props.navigator.pop();
}
2015-08-13 16:12:48 +00:00
2015-08-24 16:22:37 +00:00
render() {
return (
<View style={{flex:1, justifyContent: 'flex-start'}}>
<TextInput multiline={true} style={styles.textInput}
placeholder='Enter Todo' autoFocus={true}
onChangeText={(text) => this.setState({text})} value={this.state.text}/>
<TouchableHighlight
style={styles.button}
onPress={this.save.bind(this)}
underlayColor='#99d9f4'>
<Text style={styles.buttonText}>Save</Text>
</TouchableHighlight>
</View>
)
}
};
class TodoList extends React.Component {
componentWillMount() {
this.lists = realm.objects('TodoList');
if (this.lists.length < 1) {
realm.write(function() {
2015-08-28 20:34:53 +00:00
realm.create('TodoList', ['List', []]);
2015-08-24 16:22:37 +00:00
});
}
2015-08-28 20:34:53 +00:00
this.list = this.lists[0];
2015-08-24 16:22:37 +00:00
this.menu = this.menu.bind(this);
this.delete = this.delete.bind(this);
var dataSource = new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2
});
this.updateDataSource(dataSource);
}
updateDataSource(oldDataSource) {
if (!oldDataSource) {
oldDataSource = this.state.dataSource;
}
this.setState({dataSource: oldDataSource.cloneWithRows(this.list.items)});
}
menu(todo, todoID) {
AlertIOS.alert(
todo.text,
todoID,
[
{text: 'Complete', onPress: () => this.delete(todoID)},
{text: 'Edit', onPress: () => this.edit(todoID, todo.text)},
{text: 'Cancel'}
]
)
}
delete(todoID) {
var item = this.list.items[todoID];
realm.write(function() {
realm.delete(item);
})
this.updateDataSource();
}
edit(todoId, text) {
this.props.navigator.push({
title: text,
component: Edit,
passProps: {list: this.list, todoId: todoId, text: text, parent: this}
});
}
render() {
return (
<View style={styles.container}>
<ListView style={styles.listView} dataSource={this.state.dataSource} renderRow={(rowData, sectionID, rowID) =>
<TouchableHighlight style={styles.listItem} onPress={() => this.menu(rowData, rowID)}>
2015-08-28 22:22:56 +00:00
<Text>{rowData.text}</Text>
2015-08-24 16:22:37 +00:00
</TouchableHighlight>
}/>
<TouchableHighlight style={styles.button}
onPress={() => this.edit(this.list.items.length, "")}>
<Text style={styles.buttonText}>+</Text>
</TouchableHighlight>
<Text style={styles.instructions}>
Press Cmd+R to reload,{'\n'}
Cmd+Control+Z for dev menu
</Text>
</View>
);
}
};
class Navigator extends React.Component {
render() {
return (
<NavigatorIOS initialRoute={{component: TodoList, title: 'Todo Items'}} style={{flex:1}}/>
);
}
};
AppRegistry.registerComponent('ReactExample', () => Navigator);
2015-08-13 16:12:48 +00:00
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
2015-08-28 22:22:56 +00:00
alignItems: 'stretch',
2015-08-24 16:22:37 +00:00
backgroundColor: '#ffffff',
},
listItem: {
marginTop: 3,
2015-08-28 22:22:56 +00:00
padding: 6,
backgroundColor:'#ACACAC',
2015-08-24 16:22:37 +00:00
alignSelf: 'stretch',
flexDirection: 'row',
flex:1,
},
textInput: {
alignSelf: 'stretch',
borderWidth: 0.5,
borderColor: '#0f0f0f',
height: 200,
fontSize: 13,
margin: 6,
marginTop: 70,
padding: 4,
},
button: {
height: 36,
backgroundColor: '#48BBEC',
alignSelf: 'stretch',
justifyContent: 'center'
},
buttonText: {
alignSelf: 'center',
2015-08-13 16:12:48 +00:00
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
2015-08-24 16:22:37 +00:00
}
2015-08-13 16:12:48 +00:00
});