diff --git a/src/App.js b/src/App.js index 00c7ea9d..c2695b2b 100644 --- a/src/App.js +++ b/src/App.js @@ -5,10 +5,10 @@ import Counters from "./components/counters"; class App extends Component { state = { counters: [ - { id: 1, value: 0 }, - { id: 2, value: 0 }, - { id: 3, value: 0 }, - { id: 4, value: 0 } + { id: 1, value: 0, description: '' }, + { id: 2, value: 0, description: '' }, + { id: 3, value: 0, description: '' }, + { id: 4, value: 0, description: '' } ] }; @@ -45,6 +45,15 @@ class App extends Component { window.location.reload(); }; + handleDescriptionChange = (counter, desc) => { + const counters = [...this.state.counters]; + const index = counters.indexOf(counter); + counters[index] = { ...counters[index] }; + counters[index].description = desc; + this.setState({ counters }); + }; + + render() { return (
@@ -59,6 +68,7 @@ class App extends Component { onDecrement={this.handleDecrement} onDelete={this.handleDelete} onRestart={this.handleRestart} + onDescriptionChange={this.handleDescriptionChange} />
diff --git a/src/components/counter.jsx b/src/components/counter.jsx index 862ab509..ead8a271 100644 --- a/src/components/counter.jsx +++ b/src/components/counter.jsx @@ -1,6 +1,25 @@ import React, { Component } from "react"; class Counter extends Component { + constructor(props) { + super(props); + this.state = {disabled: true, description: props.description}; + + this.handleDescriptionChange = this.handleDescriptionChange.bind(this) + } + + // Handle Description change + handleDescriptionChange(event) { + let prevDescription = this.props.description, + currentDescription = event.target.value; + + if( prevDescription !== currentDescription ){ + this.setState({...this.state, disabled: false}); + } + + this.props.handleDescriptionChange(this.props.counter, currentDescription) + } + render() { return (
@@ -10,7 +29,7 @@ class Counter extends Component { {this.formatCount()}
-
+