Browse Source

Video 3: add a native TextInput.

Frederic G. MARAND 7 years ago
parent
commit
b5a86aac58
2 changed files with 69 additions and 11 deletions
  1. 39 9
      app.js
  2. 30 2
      header.js

+ 39 - 9
app.js

@@ -1,12 +1,12 @@
-import React, { Component } from 'react';
-import { View, Text, StyleSheet, Platform } from 'react-native';
-import Header from './header';
-import Footer from './footer';
+import React, { Component } from "react";
+import { View, Text, StyleSheet, Platform } from "react-native";
+import Header from "./header";
+import Footer from "./footer";
 
 const styles = StyleSheet.create({
   container: {
     flex: 1,
-    backgroundColor: "#FF6633",
+    backgroundColor: "#F5F5F5",
     ...Platform.select({
       ios: {
         paddingTop: 30
@@ -19,13 +19,43 @@ const styles = StyleSheet.create({
 });
 
 class App extends Component {
+  constructor(props) {
+    super(props);
+    this.state = {
+      value: "",
+      items: []
+    };
+    this.handleAddItem = this.handleAddItem.bind(this);
+  }
+
+  handleAddItem() {
+    if (!this.state.value) {
+      return;
+    }
+    const newItems = [
+      ...this.state.items,
+      {
+        key: Date.now(),
+        text: this.state.value,
+        complete: false
+      }
+    ];
+    this.setState({
+      items: newItems,
+      value: ""
+    });
+  }
+
   render() {
+    console.log(this.state);
     return (
       <View style={styles.container}>
-        <Header />
-        <View style={styles.content}>
-          <Text>Some other text</Text>
-        </View>
+        <Header
+          onAddItem={this.handleAddItem}
+          onChange={(value) => this.setState({ value })}
+          value={this.state.value}
+        />
+        <View style={styles.content} />
         <Footer />
       </View>
     );

+ 30 - 2
header.js

@@ -1,10 +1,38 @@
 import React, { Component } from 'react';
-import { View, Text, StyleSheet } from 'react-native';
+import {
+  StyleSheet,
+  Text,
+  TextInput,
+  View
+} from 'react-native';
+
+const styles = StyleSheet.create({
+  header: {
+    paddingHorizontal: 16,
+    flexDirection: "row",
+    justifyContent: "space-around",
+    alignItems: "center"
+  },
+  input: {
+    flex: 1,
+    height: 50
+  }
+});
 
 class Header extends Component {
   render() {
     return (
-      <View />
+      <View style={styles.header}>
+        <TextInput
+          blurOnSubmit={false}
+          onChangeText={this.props.onChange}
+          onSubmitEditing={this.props.onAddItem}
+          placeholder="What needs to be done?"
+          returnKeyType="done"
+          style={styles.input}
+          value={this.props.value}
+        />
+      </View>
     );
   }
 }