Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '' }
]
};

Expand Down Expand Up @@ -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 (
<div>
Expand All @@ -59,6 +68,7 @@ class App extends Component {
onDecrement={this.handleDecrement}
onDelete={this.handleDelete}
onRestart={this.handleRestart}
onDescriptionChange={this.handleDescriptionChange}
/>
</main>
</div>
Expand Down
37 changes: 36 additions & 1 deletion src/components/counter.jsx
Original file line number Diff line number Diff line change
@@ -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 (
<div>
Expand All @@ -10,7 +29,7 @@ class Counter extends Component {
{this.formatCount()}
</span>
</div>
<div className="col-md-4">
<div className="col-md-6">
<button
className="btn btn-secondary"
onClick={() => this.props.onIncrement(this.props.counter)}
Expand All @@ -30,6 +49,21 @@ class Counter extends Component {
>
<i className="fa fa-trash-o" aria-hidden="true" />
</button>
<input type="text" className="form-control d-inline w-50 m-2 description"
placeholder="Description"
value={this.state.description}
onChange={(evt) => this.handleDescriptionChange(evt)}
>
</input>
<button
className={`btn d-inline
${ !this.state.disabled ? "btn-primary" : "" }
${ this.state.disabled ? "btn-light" : "" }`
}
>
Save
</button>

</div>
</div>
</div>
Expand All @@ -46,6 +80,7 @@ class Counter extends Component {
const { value } = this.props.counter;
return value === 0 ? "Zero" : value;
};

}

export default Counter;
4 changes: 3 additions & 1 deletion src/components/counters.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class Counters extends Component {
onDelete,
onDecrement,
counters,
onRestart
onRestart,
onDescriptionChange
} = this.props;
return (
<div>
Expand All @@ -34,6 +35,7 @@ class Counters extends Component {
onIncrement={onIncrement}
onDecrement={onDecrement}
onDelete={onDelete}
handleDescriptionChange={onDescriptionChange}
/>
))}
</div>
Expand Down
11 changes: 11 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@ code {
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
monospace;
}

/* Counter */
.description{
width: 40%;
background: #F1F2F3!important;
border-radius: 6px;

font-size: 16px;
line-height: 19px;
color: #787F85;
}