Skip to content

Commit b92dc2d

Browse files
author
DEVfancybear
committed
hoan thanh
1 parent ce09790 commit b92dc2d

20 files changed

+14197
-56
lines changed

example_1/.idea/.gitignore

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example_1/.idea/example_1.iml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example_1/.idea/inspectionProfiles/Project_Default.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example_1/.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example_1/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example_1/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example_1/package-lock.json

Lines changed: 13883 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example_1/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"@testing-library/jest-dom": "^4.2.4",
77
"@testing-library/react": "^9.3.2",
88
"@testing-library/user-event": "^7.1.2",
9+
"classnames": "^2.2.6",
910
"react": "^16.12.0",
1011
"react-dom": "^16.12.0",
1112
"react-scripts": "3.3.0"

example_1/src/App.css

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,23 @@
11
.App {
2-
text-align: center;
2+
width: 500px;
3+
margin-left: auto;
4+
margin-left: auto;
5+
border: 1px solid #EEE;
36
}
4-
5-
.App-logo {
6-
height: 40vmin;
7-
pointer-events: none;
8-
}
9-
10-
@media (prefers-reduced-motion: no-preference) {
11-
.App-logo {
12-
animation: App-logo-spin infinite 20s linear;
13-
}
14-
}
15-
16-
.App-header {
17-
background-color: #282c34;
18-
min-height: 100vh;
7+
.App .Header {
8+
padding: 10px 5px;
199
display: flex;
20-
flex-direction: column;
21-
align-items: center;
22-
justify-content: center;
23-
font-size: calc(10px + 2vmin);
24-
color: white;
10+
border-bottom: 1px solid #EEE;
2511
}
26-
27-
.App-link {
28-
color: #61dafb;
12+
.App .Header input {
13+
font-family: "Helvetica Neue", Arial, Helvetica, sans-serif;
14+
font-weight: 300;
15+
font-size: 24px;
16+
padding-left: 10px;
17+
width: 100%;
18+
border: none;
2919
}
3020

31-
@keyframes App-logo-spin {
32-
from {
33-
transform: rotate(0deg);
34-
}
35-
to {
36-
transform: rotate(360deg);
37-
}
38-
}
21+
.App .Header input:focus {
22+
outline: none;
23+
}

example_1/src/App.js

Lines changed: 77 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,93 @@
11
import React, { Component } from "react";
22
import ItemTodo from "./components/ItemTodo";
33
import "./App.css";
4+
import tick from "./img/tick.svg";
45

56
class App extends Component {
67
constructor() {
78
super();
8-
this.todoItems = [
9-
{ title: "Dắt gấu đi chơi", isComplete: true},
10-
{ title: "Đi shopping", isComplete: true},
11-
{ title: "Đi siêu thị"}
12-
];
9+
this.state = {
10+
newItem: '',
11+
todoItems: [
12+
{ title: "Dắt gấu đi chơi", isComplete: true },
13+
{ title: "Đi shopping", isComplete: true },
14+
{ title: "Đi siêu thị", isComplete: false },
15+
{ title: "Nấu Cơm", isComplete: false },
16+
{ title: "Rửa Bát", isComplete: true }
17+
]
18+
};
19+
this.onKeyUp = this.onKeyUp.bind(this);
20+
this.onChange=this.onChange.bind(this);
21+
}
22+
onItemClicked = item => {
23+
return e => {
24+
const isComplete = item.isComplete;
25+
const todoItems = this.state.todoItems;
26+
const index = todoItems.indexOf(item);
27+
this.setState({
28+
todoItems: [
29+
...todoItems.slice(0, index),
30+
{
31+
...item,
32+
isComplete: !isComplete
33+
},
34+
...todoItems.slice(index + 1)
35+
]
36+
});
37+
};
38+
};
39+
// bắt sự kiên lúc người dùng ấn enter
40+
onKeyUp = e => {
41+
// console.log(e.target.value);
42+
// console.log(e.keyCode);
43+
// lấy giá trị tại thời điểm gõ
44+
let text = e.target.value;
45+
if (e.keyCode === 13) {
46+
// enter key
47+
48+
if (!text) {
49+
return;
50+
}
51+
text = text.trim(); //xóa đi dấ cách ở đầu và cuối
52+
if (!text) {
53+
return;
54+
}
55+
this.setState({
56+
newItem:'',
57+
todoItems: [{ title: text, isComplete: false }, ...this.state.todoItems]
58+
});
59+
}
60+
};
61+
onChange=(e) => {
62+
this.setState({
63+
newItem: e.target.value
64+
})
1365
}
1466
render() {
15-
let a = this.todoItems.length;
67+
let a = this.state.todoItems.length;
68+
const {newItem} = this.state;
1669
return (
17-
<>
70+
<div className="App">
71+
<div className="Header">
72+
<img src={tick} width={32} height={32} />
73+
<input
74+
type="text"
75+
placeholder="Add new item"
76+
onKeyUp={this.onKeyUp}
77+
value={newItem}
78+
onChange={this.onChange}
79+
/>
80+
</div>
1881
{a > 0 &&
19-
this.todoItems.map((item, index) => (
20-
<ItemTodo key={index} item={item} />
82+
this.state.todoItems.map((item, index) => (
83+
<ItemTodo
84+
key={index}
85+
item={item}
86+
onClick={this.onItemClicked(item)}
87+
/>
2188
))}
2289
{a === 0 && <div>Không có gì</div>}
23-
{/* dùng map để biến 1 arr gồm các obj phải thêm key */}
24-
</>
90+
</div>
2591
);
2692
}
2793
}

example_1/src/components/ItemTodo.css

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@
33
font-weight: 300;
44
font-size: 24px;
55
color: #4d4d4d;
6-
text-align: center;
6+
border-bottom: 1px solid #eee;
7+
padding: 10px 5px;
8+
display: flex;
79
}
8-
.item_done {
9-
text-decoration: line-through;
10-
opacity: 0.3;
10+
.item_todo:last-child {
11+
border-bottom: none;
12+
13+
border-bottom: 1px solid #eee;
14+
}
15+
.item_done > p {
16+
text-decoration: line-through;
17+
opacity: 0.3;
18+
}
19+
.item_todo > p {
20+
margin: 0 0 0 10px;
1121
}

example_1/src/components/ItemTodo.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
import React, { Component } from "react";
2-
import './ItemTodo.css';
2+
import "./ItemTodo.css";
3+
import classNames from "classnames";
4+
import imgCheck from "../img/check.svg";
5+
import imgCheckDone from "../img/success.svg";
36
class ItemTodo extends Component {
47
render() {
5-
const items = this.props.item;
6-
let className = 'item_todo';
7-
if(items.isComplete) {
8-
//thêm class css khi click
9-
className += ' item_done';
10-
}
8+
const { item, onClick } = this.props;
9+
let url = imgCheck;
10+
if (item.isComplete) {
11+
url = imgCheckDone;
12+
}
1113
return (
12-
<div className={className}>
13-
<p>{items.title}</p>
14+
<div
15+
className={classNames("item_todo", {
16+
item_done: item.isComplete
17+
})}
18+
>
19+
<img onClick={onClick} src={url} width={32} height={32} />
20+
<p>{this.props.item.title}</p>
1421
</div>
1522
);
1623
}

example_1/src/img/check.svg

Lines changed: 42 additions & 0 deletions
Loading

example_1/src/img/success.svg

Lines changed: 38 additions & 0 deletions
Loading

example_1/src/img/tick.svg

Lines changed: 43 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)