Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. /*This is an Example of React Native Add Items to ScrollView using Loop*/
  2. import React, { Component } from 'react';
  3. //import react in our project
  4.  
  5. import {
  6. View,
  7. ScrollView,
  8. StyleSheet,
  9. Text,
  10. TouchableOpacity,
  11. CheckBox,
  12. } from 'react-native';
  13.  
  14. //import all the components we needed
  15.  
  16. export default class App extends Component {
  17. constructor() {
  18. super();
  19. //Array of Item to add
  20. this.itemsA = [
  21. {
  22. Descricao: 'PAO_',
  23. Tipo: 'COMPOSIÇÃO',
  24. Valor: 0,
  25. chkcomp: 1,
  26. id_produto: 9202,
  27. },
  28. {
  29. Descricao: 'BACON',
  30. Tipo: 'COMPOSIÇÃO',
  31. Valor: 0,
  32. chkcomp: 1,
  33. id_produto: 9199,
  34. },
  35. {
  36. Descricao: 'FATIA DE QUEIJO',
  37. Tipo: 'COMPOSIÇÃO',
  38. Valor: 0,
  39. chkcomp: 1,
  40. id_produto: 9200,
  41. },
  42. {
  43. Descricao: 'PRESUNTO FATIADO',
  44. Tipo: 'COMPOSIÇÃO',
  45. Valor: 0,
  46. chkcomp: 1,
  47. id_produto: 9203,
  48. },
  49. {
  50. Descricao: 'CARNE DE HAMBURGUER',
  51. Tipo: 'COMPOSIÇÃO',
  52. Valor: 0,
  53. chkcomp: 1,
  54. id_produto: 9201,
  55. },
  56. {
  57. Descricao: 'CARNE DE HAMBURGUER',
  58. Tipo: 'OPCIONAL',
  59. Valor: 5,
  60. chkcomp: 0,
  61. id_produto: 9201,
  62. },
  63. {
  64. Descricao: 'FATIA DE QUEIJO',
  65. Tipo: 'OPCIONAL',
  66. Valor: 1,
  67. chkcomp: 0,
  68. id_produto: 9200,
  69. },
  70. {
  71. Descricao: 'PRESUNTO FATIADO',
  72. Tipo: 'OPCIONAL',
  73. Valor: 1,
  74. chkcomp: 0,
  75. id_produto: 9203,
  76. },
  77. {
  78. Descricao: 'BACON',
  79. Tipo: 'OPCIONAL',
  80. Valor: 5,
  81. chkcomp: 0,
  82. id_produto: 9199,
  83. },
  84. ];
  85.  
  86. this.state = {
  87. items: [
  88. {
  89. Descricao: 'PAO_',
  90. Tipo: 'COMPOSIÇÃO',
  91. Valor: 0,
  92. chkcomp: 1,
  93. id_produto: 9202,
  94. },
  95. {
  96. Descricao: 'BACON',
  97. Tipo: 'COMPOSIÇÃO',
  98. Valor: 0,
  99. chkcomp: 1,
  100. id_produto: 9199,
  101. },
  102. {
  103. Descricao: 'FATIA DE QUEIJO',
  104. Tipo: 'COMPOSIÇÃO',
  105. Valor: 0,
  106. chkcomp: 1,
  107. id_produto: 9200,
  108. },
  109. {
  110. Descricao: 'PRESUNTO FATIADO',
  111. Tipo: 'COMPOSIÇÃO',
  112. Valor: 0,
  113. chkcomp: 1,
  114. id_produto: 9203,
  115. },
  116. {
  117. Descricao: 'CARNE DE HAMBURGUER',
  118. Tipo: 'COMPOSIÇÃO',
  119. Valor: 0,
  120. chkcomp: 1,
  121. id_produto: 9201,
  122. },
  123. {
  124. Descricao: 'CARNE DE HAMBURGUER',
  125. Tipo: 'OPCIONAL',
  126. Valor: 5,
  127. chkcomp: 0,
  128. id_produto: 9201,
  129. },
  130. {
  131. Descricao: 'FATIA DE QUEIJO',
  132. Tipo: 'OPCIONAL',
  133. Valor: 1,
  134. chkcomp: 0,
  135. id_produto: 9200,
  136. },
  137. {
  138. Descricao: 'PRESUNTO FATIADO',
  139. Tipo: 'OPCIONAL',
  140. Valor: 1,
  141. chkcomp: 0,
  142. id_produto: 9203,
  143. },
  144. {
  145. Descricao: 'BACON',
  146. Tipo: 'OPCIONAL',
  147. Valor: 5,
  148. chkcomp: 0,
  149. id_produto: 9199,
  150. },
  151. ],
  152. };
  153. }
  154.  
  155. /** TROCA O STATE DA COMPOSIÇÃO */
  156.  
  157. onUpdateItem(item) {
  158.  
  159. this.setState({
  160. items: this.state.items.map(i => {
  161. let checked = i.id_produto === item.id_produto ? !i.checked : i.checked;
  162.  
  163. return {
  164. ...i,
  165. checked
  166. }
  167. })
  168. });
  169. }
  170.  
  171. render() {
  172. return (
  173. <View style={styles.container}>
  174. <ScrollView>
  175. {/*Loop of JS which is like foreach loop*/}
  176. {this.state.items.map((item, key) => (
  177. //key is the index of the array
  178. //item is the single item of the array
  179. <View key={key} style={styles.item}>
  180. <TouchableOpacity onPress={() => this.onUpdateItem(item)}>
  181. <View style={styles.text}>
  182. <CheckBox value={item.checked} />
  183. <Text>{item.Descricao}</Text>
  184. </View>
  185. </TouchableOpacity>
  186. <View style={styles.separator} />
  187. </View>
  188. ))}
  189. </ScrollView>
  190. </View>
  191. );
  192. }
  193. }
  194.  
  195. const styles = StyleSheet.create({
  196. container: {
  197. flex: 1,
  198. paddingTop: 30,
  199. },
  200.  
  201. separator: {
  202. height: 1,
  203. backgroundColor: '#707080',
  204. width: '100%',
  205. },
  206. text: {
  207. fontSize: 16,
  208. color: '#606070',
  209. padding: 10,
  210. },
  211. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement