app.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import React, { Component } from "react";
  2. import {
  3. Keyboard,
  4. ListView,
  5. Platform,
  6. StyleSheet,
  7. Text,
  8. View
  9. } from "react-native";
  10. import Header from "./header";
  11. import Footer from "./footer";
  12. import Row from "./row";
  13. const styles = StyleSheet.create({
  14. container: {
  15. flex: 1,
  16. backgroundColor: "#F5F5F5",
  17. ...Platform.select({
  18. ios: {
  19. paddingTop: 30
  20. }
  21. })
  22. },
  23. content: {
  24. flex: 1
  25. },
  26. list: {
  27. backgroundColor: "#fff"
  28. },
  29. separator: {
  30. borderWidth: 2,
  31. borderColor: "#eee"
  32. }
  33. });
  34. class App extends Component {
  35. constructor(props) {
  36. super(props);
  37. const ds = new ListView.DataSource({
  38. rowHasChanged: (r1, r2) => r1 !== r2
  39. });
  40. this.state = {
  41. allComplete: false,
  42. dataSource: ds.cloneWithRows([]),
  43. items: [],
  44. value: ""
  45. };
  46. this.handleAddItem = this.handleAddItem.bind(this);
  47. this.handleRemoveItem = this.handleRemoveItem.bind(this);
  48. this.handleToggleAllComplete = this.handleToggleAllComplete.bind(this);
  49. this.handleToggleComplete = this.handleToggleComplete.bind(this);
  50. this.setSource = this.setSource.bind(this);
  51. }
  52. handleAddItem() {
  53. if (!this.state.value) {
  54. return;
  55. }
  56. const newItems = [
  57. ...this.state.items,
  58. {
  59. key: Date.now(),
  60. text: this.state.value,
  61. complete: false
  62. }
  63. ];
  64. this.setSource(newItems, newItems, { value: "" });
  65. }
  66. handleRemoveItem(key) {
  67. const newItems = this.state.items.filter((item) => item.key !== key);
  68. this.setSource(newItems, newItems);
  69. }
  70. handleToggleAllComplete() {
  71. const complete = !this.state.allComplete;
  72. const newItems = this.state.items.map((item) => ({
  73. ...item,
  74. complete
  75. }));
  76. // console.table(newItems);
  77. this.setSource(newItems, newItems, { allComplete: complete });
  78. }
  79. handleToggleComplete(key, complete) {
  80. const newItems = this.state.items.map((item) => {
  81. if (item.key !== key) {
  82. return item;
  83. }
  84. return {
  85. ...item,
  86. complete
  87. };
  88. });
  89. this.setSource(newItems, newItems);
  90. }
  91. render() {
  92. // console.log("App state", this.state);
  93. return (
  94. <View style={styles.container}>
  95. <Header
  96. onAddItem={this.handleAddItem}
  97. onChange={(value) => this.setState({ value })}
  98. onToggleAllComplete={this.handleToggleAllComplete}
  99. value={this.state.value}
  100. />
  101. <View style={styles.content}>
  102. <ListView
  103. enableEmptySections
  104. dataSource={this.state.dataSource}
  105. onScroll={() => Keyboard.dismiss()}
  106. renderRow={({ key, ...value }) => {
  107. // console.log("renderRow", key, value);
  108. return (
  109. <Row
  110. key={key}
  111. onComplete={(complete) => this.handleToggleComplete(key, complete)}
  112. onRemove={() => this.handleRemoveItem(key)}
  113. { ...value }
  114. />
  115. );
  116. }}
  117. renderSeparator={(sectionId, rowId) => {
  118. // console.log('renderSeparator', sectionId, rowId);
  119. return <View key={rowId} style={styles.separator} />;
  120. }}
  121. style={styles.list}
  122. />
  123. </View>
  124. <Footer />
  125. </View>
  126. );
  127. }
  128. setSource(items, itemsDataSource, otherState = {}) {
  129. this.setState({
  130. items,
  131. dataSource: this.state.dataSource.cloneWithRows(itemsDataSource),
  132. ...otherState
  133. });
  134. }
  135. }
  136. export default App;