app.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.handleToggleAllComplete = this.handleToggleAllComplete.bind(this);
  48. this.setSource = this.setSource.bind(this);
  49. }
  50. handleAddItem() {
  51. if (!this.state.value) {
  52. return;
  53. }
  54. const newItems = [
  55. ...this.state.items,
  56. {
  57. key: Date.now(),
  58. text: this.state.value,
  59. complete: false
  60. }
  61. ];
  62. this.setSource(newItems, newItems, { value: "" });
  63. }
  64. handleToggleAllComplete() {
  65. const complete = !this.state.allComplete;
  66. const newItems = this.state.items.map((item) => ({
  67. ...item,
  68. complete
  69. }));
  70. // console.table(newItems);
  71. this.setSource(newItems, newItems, { allComplete: complete });
  72. }
  73. render() {
  74. // console.log("App state", this.state);
  75. return (
  76. <View style={styles.container}>
  77. <Header
  78. onAddItem={this.handleAddItem}
  79. onChange={(value) => this.setState({ value })}
  80. onToggleAllComplete={this.handleToggleAllComplete}
  81. value={this.state.value}
  82. />
  83. <View style={styles.content}>
  84. <ListView
  85. enableEmptySections
  86. dataSource={this.state.dataSource}
  87. onScroll={() => Keyboard.dismiss()}
  88. renderRow={({ key, ...value }) => {
  89. // console.log("renderRow", key, value);
  90. return (
  91. <Row
  92. key={key}
  93. { ...value }
  94. />
  95. );
  96. }}
  97. renderSeparator={(sectionId, rowId) => {
  98. // console.log('renderSeparator', sectionId, rowId);
  99. return <View key={rowId} style={styles.separator} />;
  100. }}
  101. style={styles.list}
  102. />
  103. </View>
  104. <Footer />
  105. </View>
  106. );
  107. }
  108. setSource(items, itemsDataSource, otherState = {}) {
  109. this.setState({
  110. items,
  111. dataSource: this.state.dataSource.cloneWithRows(itemsDataSource),
  112. ...otherState
  113. });
  114. }
  115. }
  116. export default App;