app.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import React, { Component } from "react";
  2. import {
  3. Platform,
  4. StyleSheet,
  5. Text,
  6. View
  7. } from "react-native";
  8. import Header from "./header";
  9. import Footer from "./footer";
  10. const styles = StyleSheet.create({
  11. container: {
  12. flex: 1,
  13. backgroundColor: "#F5F5F5",
  14. ...Platform.select({
  15. ios: {
  16. paddingTop: 30
  17. }
  18. })
  19. },
  20. content: {
  21. flex: 1
  22. }
  23. });
  24. class App extends Component {
  25. constructor(props) {
  26. super(props);
  27. this.state = {
  28. allComplete: false,
  29. items: [],
  30. value: ""
  31. };
  32. this.handleAddItem = this.handleAddItem.bind(this);
  33. this.handleToggleAllComplete = this.handleToggleAllComplete.bind(this);
  34. }
  35. handleAddItem() {
  36. if (!this.state.value) {
  37. return;
  38. }
  39. const newItems = [
  40. ...this.state.items,
  41. {
  42. key: Date.now(),
  43. text: this.state.value,
  44. complete: false
  45. }
  46. ];
  47. this.setState({
  48. items: newItems,
  49. value: ""
  50. });
  51. }
  52. handleToggleAllComplete() {
  53. const complete = !this.state.allComplete;
  54. const newItems = this.state.items.map((item) => ({
  55. ...item,
  56. complete
  57. }));
  58. console.table(newItems);
  59. this.setState({
  60. allComplete: complete,
  61. items: newItems
  62. });
  63. }
  64. render() {
  65. console.log(this.state);
  66. return (
  67. <View style={styles.container}>
  68. <Header
  69. onAddItem={this.handleAddItem}
  70. onChange={(value) => this.setState({ value })}
  71. onToggleAllComplete={this.handleToggleAllComplete}
  72. value={this.state.value}
  73. />
  74. <View style={styles.content} />
  75. <Footer />
  76. </View>
  77. );
  78. }
  79. }
  80. export default App;