Created
May 14, 2017 14:06
-
-
Save drmas/952f2c6431888946c9deb314d64d291d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Sample React Native App | |
* https://github.com/facebook/react-native | |
* @flow | |
*/ | |
import React, { Component } from 'react'; | |
import { | |
AppRegistry, | |
StyleSheet, | |
Text, | |
View, | |
TextInput, | |
ListView, | |
TouchableOpacity | |
} from 'react-native'; | |
const ds = new ListView.DataSource({ | |
rowHasChanged: (r1, r2) => r1 !== r2 | |
}) | |
const data = ['Add item', 'Delete item'] | |
class Project extends Component { | |
state = { | |
dataSource: ds.cloneWithRows(data) | |
} | |
renderRow = (rowData, _, index) => { | |
return ( | |
<View style={styles.itemContainer} > | |
<Text style={styles.item} >{rowData}</Text> | |
<TouchableOpacity style={styles.btn} onPress={()=>{ | |
data = data.filter((item, i) => i != index) | |
this.setState({ | |
dataSource: ds.cloneWithRows(data) | |
}) | |
}} > | |
<Text style={styles.btnText}>Done</Text> | |
</TouchableOpacity> | |
</View> | |
) | |
} | |
onChangeText = (text) => { | |
this.setState({ | |
value: text | |
}) | |
} | |
onSubmitEditing = () => { | |
data = [this.state.value, ...data]; | |
this.setState({ | |
dataSource: ds.cloneWithRows(data), | |
value: '' | |
}) | |
} | |
render() { | |
return ( | |
<View style={styles.container}> | |
<Text style={styles.welcome}> | |
TODO | |
</Text> | |
<TextInput | |
style={styles.textInput} | |
placeholder='Add item todo' | |
onChangeText={this.onChangeText} | |
onSubmitEditing={this.onSubmitEditing} | |
value={this.state.value} | |
/> | |
<ListView | |
dataSource={this.state.dataSource} | |
renderRow={this.renderRow} | |
/> | |
</View> | |
); | |
} | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
alignItems: 'stretch', | |
backgroundColor: '#fff', | |
}, | |
welcome: { | |
fontSize: 20, | |
textAlign: 'center', | |
marginTop: 22, | |
}, | |
instructions: { | |
textAlign: 'center', | |
color: '#333333', | |
marginBottom: 5, | |
}, | |
textInput: { | |
height: 50, | |
padding: 8, | |
borderWidth: 1, | |
borderColor: '#eaeaea', | |
margin: 8 | |
}, | |
itemContainer: { | |
padding: 8, | |
borderBottomWidth: 1, | |
borderColor: '#eaeaea', | |
flexDirection: 'row' | |
}, | |
item: { | |
fontSize: 18, | |
flex: 1 | |
}, | |
btn: { | |
backgroundColor: 'red', | |
justifyContent: 'center', | |
alignItems: 'center', | |
padding: 6 | |
}, | |
btnText: { | |
color: 'white' | |
} | |
}); | |
AppRegistry.registerComponent('Project', () => Project); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment