diff --git a/02/src/App.js b/02/src/App.js index 245a13d8..136d2279 100644 --- a/02/src/App.js +++ b/02/src/App.js @@ -72,6 +72,7 @@ const App = props => { // ## OOP code ## class App extends React.Component { // The state should be IMMUTABLE, do not directly change it + // If you need to change it, use the setState() method. state = { character: [ { id: 1, name: "char1", ilvl: "100" }, diff --git a/04/src/App.js b/04/src/App.js index 2f0262ac..25286dab 100644 --- a/04/src/App.js +++ b/04/src/App.js @@ -1,31 +1,57 @@ import React from "react"; import "./App.css"; -import Validation from './Validation' +import Validation from "./Validation"; +import Char from "./Char"; class App extends React.Component { constructor(props) { super(props); - this.state = { message: "Sup!" }; + this.state = { message: "" }; } - handleChange = event => { - console.log(event.target.value); + // handleChange = event => { + // let newMessage = event.target.value; + // this.setState({ message: newMessage }); + // }; + + handleChange(event) { let newMessage = event.target.value; this.setState({ message: newMessage }); }; + handleDelete(index) { + let newMessage = [...this.state.message]; + let newSplited = newMessage.toString().split(","); + newSplited.splice(index, 1); + newMessage = newSplited.join(""); + this.setState({ message: newMessage }); + } + render() { const input = ( - // // --> this works - this.handleChange(event)} /> // --> but this is better + // // --> this works + this.handleChange(event)} value={this.state.message} /> // --> but this is better ); + let splited = this.state.message.split(""); + + const charList = splited.map((item, index) => { + return ( + this.handleDelete(index)} + /> + ); + }); + return (

{this.state.message}

{input} - + + {charList}
); } diff --git a/04/src/Char.css b/04/src/Char.css new file mode 100644 index 00000000..78f6ef3e --- /dev/null +++ b/04/src/Char.css @@ -0,0 +1,7 @@ +.Char { + display: inline-block; + padding: 16px; + text-align: center; + margin: 16px; + border: 1px solid black; +} diff --git a/04/src/Char.js b/04/src/Char.js index e69de29b..4b1278d4 100644 --- a/04/src/Char.js +++ b/04/src/Char.js @@ -0,0 +1,14 @@ +import React from "react"; +import "./Char.css"; + +class Char extends React.Component { + constructor(props) { + super(props); + } + + render() { + return
{this.props.string}
; + } +} + +export default Char; diff --git a/04/src/Validation.js b/04/src/Validation.js index 22ac4fc0..df48f6ae 100644 --- a/04/src/Validation.js +++ b/04/src/Validation.js @@ -4,15 +4,19 @@ class Validation extends React.Component { constructor(props) { super(props); console.log("Validation:", props); - // console.log(this.props.message.lenght); + } + + validation() { + let validationMessage =

good enough

; + if (this.props.length >= 5) { + validationMessage =

TOO MUCH, STAHP

; + return validationMessage; + } + return validationMessage; } render() { - return( -
-

validation placeholder

-
- ) + return
{this.validation()}
; } }