Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. import React from 'react';
  2. import {
  3. FlatList,
  4. TouchableOpacity,
  5. StyleSheet,
  6. Text,
  7. TextInput,
  8. View
  9. } from 'react-native';
  10.  
  11. export default class App extends React.Component {
  12. state = {
  13. shouldDisplayForm: false,
  14. text: "",
  15. index: 3,
  16. list: [
  17. {key: "aaaaaaa", value: "asdfasfd"},
  18. {key: "bbbb", value: "fsafdsa"},
  19. {key: "cc", value: "fasfs"}]
  20. }
  21.  
  22. onButtonPress = () =>
  23. this.setState({ shouldDisplayForm: true });
  24.  
  25. renderItem = (a) =>
  26. <View style={styles.item}>
  27. <Text style={styles.itemText}>{a.item.value}</Text>
  28. </View>;
  29.  
  30. keyExtractor = (item, index) => item.key + index;
  31.  
  32. onMovieAdd = () => this.setState({
  33. index: this.state.index + 1,
  34. list: [
  35. {
  36. key: this.state.index,
  37. value: this.state.text
  38. },
  39. ...this.state.list
  40. ],
  41. shouldDisplayForm: false,
  42. text: ""
  43. });
  44.  
  45. onChangeText = text => this.setState({ text });
  46.  
  47. renderList = () => (
  48. <View>
  49. <TouchableOpacity
  50. style={styles.button}
  51. onPress={this.onButtonPress}
  52. >
  53. <Text style={styles.buttonText}>Wyświetl mi forma!</Text>
  54. </TouchableOpacity>
  55. <FlatList
  56. style={styles.list}
  57. data={this.state.list}
  58. renderItem={this.renderItem}
  59. keyExtractor={this.keyExtractor}
  60. />
  61. </View>
  62. );
  63.  
  64. renderForm = () => (
  65. <View>
  66. <TextInput
  67. style={styles.input}
  68. onChangeText={this.onChangeText}
  69. value={this.state.text}
  70. />
  71. <TouchableOpacity
  72. style={styles.inputButton}
  73. onPress={this.onMovieAdd}
  74. >
  75. <Text
  76. style={styles.buttonText}
  77. >
  78. Dodaj film!
  79. </Text>
  80. </TouchableOpacity>
  81. </View>
  82. );
  83.  
  84. render() {
  85. const { shouldDisplayForm } = this.state;
  86.  
  87. return (
  88. <View style={styles.container}>
  89. {!shouldDisplayForm
  90. ? this.renderList()
  91. : this.renderForm()}
  92. </View>
  93. );
  94. }
  95. }
  96.  
  97. const styles = StyleSheet.create({
  98. input: {
  99. backgroundColor: '#eee',
  100. height: 50,
  101. borderRadius: 5,
  102. marginBottom: 10,
  103. paddingHorizontal: 10,
  104. fontSize: 18,
  105. },
  106. inputButton: {
  107. backgroundColor: '#0ff0ff',
  108. paddingHorizontal: 20,
  109. borderRadius: 5,
  110. paddingVertical: 15,
  111. },
  112. container: {
  113. flex: 1,
  114. backgroundColor: '#fff',
  115. alignItems: 'center',
  116. justifyContent: 'center',
  117. paddingVertical: 30,
  118. },
  119. list: {
  120. marginTop: 30,
  121. display: 'flex',
  122. flex: 1,
  123. width: '100%',
  124. height: '100%',
  125. },
  126. item: {
  127. width: '100%',
  128. backgroundColor: '#eee',
  129. padding: 15,
  130. },
  131. itemText: {
  132. fontSize: 20,
  133. },
  134. button: {
  135. backgroundColor: '#fff000',
  136. paddingHorizontal: 20,
  137. borderRadius: 5,
  138. paddingVertical: 15,
  139. },
  140. buttonText: {
  141. color: '#000',
  142. fontSize: 20
  143. }
  144. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement