app.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. const filterItems = (filter, items) => {
  35. return items.filter((item) => {
  36. if ((filter === 'ACTIVE' && item.complete) || (filter === 'COMPLETED' && !item.complete)) {
  37. return false;
  38. }
  39. return true;
  40. });
  41. };
  42. class App extends Component {
  43. constructor(props) {
  44. super(props);
  45. const ds = new ListView.DataSource({
  46. rowHasChanged: (r1, r2) => r1 !== r2
  47. });
  48. this.state = {
  49. allComplete: false,
  50. dataSource: ds.cloneWithRows([]),
  51. filter: "ALL",
  52. items: [],
  53. value: ""
  54. };
  55. this.handleAddItem = this.handleAddItem.bind(this);
  56. this.handleClearComplete = this.handleClearComplete.bind(this);
  57. this.handleFilter = this.handleFilter.bind(this);
  58. this.handleRemoveItem = this.handleRemoveItem.bind(this);
  59. this.handleToggleAllComplete = this.handleToggleAllComplete.bind(this);
  60. this.handleToggleComplete = this.handleToggleComplete.bind(this);
  61. this.setSource = this.setSource.bind(this);
  62. }
  63. handleAddItem() {
  64. if (!this.state.value) {
  65. return;
  66. }
  67. const newItems = [
  68. ...this.state.items,
  69. {
  70. key: Date.now(),
  71. text: this.state.value,
  72. complete: false
  73. }
  74. ];
  75. this.setSource(newItems, newItems, { value: "" });
  76. }
  77. handleClearComplete() {
  78. const newItems = filterItems('ACTIVE', this.state.items);
  79. this.setSource(newItems, filterItems(this.state.filter, newItems));
  80. }
  81. handleFilter(filter) {
  82. this.setSource(this.state.items, filterItems(filter, this.state.items), { filter });
  83. }
  84. handleRemoveItem(key) {
  85. const newItems = this.state.items.filter((item) => item.key !== key);
  86. this.setSource(newItems, filterItems(this.state.filter, newItems));
  87. }
  88. handleToggleAllComplete() {
  89. const complete = !this.state.allComplete;
  90. const newItems = this.state.items.map((item) => ({
  91. ...item,
  92. complete
  93. }));
  94. // console.table(newItems);
  95. this.setSource(newItems, filterItems(this.state.filter, newItems), { allComplete: complete });
  96. }
  97. handleToggleComplete(key, complete) {
  98. const newItems = this.state.items.map((item) => {
  99. if (item.key !== key) {
  100. return item;
  101. }
  102. return {
  103. ...item,
  104. complete
  105. };
  106. });
  107. this.setSource(newItems, filterItems(this.state.filter, newItems));
  108. }
  109. render() {
  110. // console.log("App state", this.state);
  111. return (
  112. <View style={styles.container}>
  113. <Header
  114. onAddItem={this.handleAddItem}
  115. onChange={(value) => this.setState({ value })}
  116. onToggleAllComplete={this.handleToggleAllComplete}
  117. value={this.state.value}
  118. />
  119. <View style={styles.content}>
  120. <ListView
  121. enableEmptySections
  122. dataSource={this.state.dataSource}
  123. onScroll={() => Keyboard.dismiss()}
  124. renderRow={({ key, ...value }) => {
  125. // console.log("renderRow", key, value);
  126. return (
  127. <Row
  128. key={key}
  129. onComplete={(complete) => this.handleToggleComplete(key, complete)}
  130. onRemove={() => this.handleRemoveItem(key)}
  131. { ...value }
  132. />
  133. );
  134. }}
  135. renderSeparator={(sectionId, rowId) => {
  136. // console.log('renderSeparator', sectionId, rowId);
  137. return <View key={rowId} style={styles.separator} />;
  138. }}
  139. style={styles.list}
  140. />
  141. </View>
  142. <Footer
  143. count={filterItems("ACTIVE", this.state.items).length}
  144. filter={this.state.filter}
  145. onClearComplete={this.handleClearComplete}
  146. onFilter={this.handleFilter}
  147. />
  148. </View>
  149. );
  150. }
  151. setSource(items, itemsDataSource, otherState = {}) {
  152. this.setState({
  153. items,
  154. dataSource: this.state.dataSource.cloneWithRows(itemsDataSource),
  155. ...otherState
  156. });
  157. }
  158. }
  159. export default App;