From 684292b454033d76d38127bdb5c98362d0301750 Mon Sep 17 00:00:00 2001 From: Yutsuo Date: Wed, 15 Jan 2020 12:28:18 -0300 Subject: [PATCH] 04-ongoing --- 02/src/App.js | 1 + 04/src/App.js | 33 ++++++++++++++++++++++++--------- 04/src/Validation.js | 19 ++++++++++--------- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/02/src/App.js b/02/src/App.js index 96cfda19..8ccf5d9f 100644 --- a/02/src/App.js +++ b/02/src/App.js @@ -74,6 +74,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 0a96d484..25286dab 100644 --- a/04/src/App.js +++ b/04/src/App.js @@ -10,33 +10,48 @@ class App extends React.Component { 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 newSplited = [...this.state.splited] + 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(""); - console.log("splited", splited); + + const charList = splited.map((item, index) => { + return ( + this.handleDelete(index)} + /> + ); + }); return (

{this.state.message}

{input} - {splited.map((item, index) => { - return - })} + {charList}
); } diff --git a/04/src/Validation.js b/04/src/Validation.js index 88f1668d..df48f6ae 100644 --- a/04/src/Validation.js +++ b/04/src/Validation.js @@ -6,16 +6,17 @@ class Validation extends React.Component { console.log("Validation:", props); } + validation() { + let validationMessage =

good enough

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

TOO MUCH, STAHP

; + return validationMessage; + } + return validationMessage; + } + render() { - return( -
- {this.props.length <= 5 ? -

good enough (length: {this.props.length})

- : -

TOO MUCH, STAHP (length: {this.props.length})

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