Skip to content

Commit 570172b

Browse files
tutorial
1 parent d39786e commit 570172b

File tree

4 files changed

+74
-5
lines changed

4 files changed

+74
-5
lines changed

src/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import React from 'react'
22

33
import TodoList from './components/TodoList'
4+
// import { Profile } from './components/Profile'
45

56
export const App: React.FC = () => {
67
return (
78
<div>
89
<TodoList />
10+
{/* <Profile userId={'Hello!'}></Profile> */}
911
</div>
1012
)
1113
}

src/components/Profile.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React, { Component } from 'react'
2+
3+
interface Props {
4+
userId: string
5+
altText?: string
6+
}
7+
8+
interface State {
9+
username: string
10+
age: number
11+
}
12+
13+
async function getData() {
14+
return Promise.resolve({
15+
username: 'bob',
16+
age: 28,
17+
})
18+
}
19+
20+
export class Profile extends Component<Props, State> {
21+
state = {
22+
username: '',
23+
age: 0,
24+
}
25+
26+
async componentDidMount() {
27+
const userData = await getData()
28+
this.setState(userData)
29+
}
30+
31+
render() {
32+
return (
33+
<div>
34+
<h1>{this.props.userId}</h1>
35+
<p>{this.state.username}</p>
36+
<p>{this.state.age}</p>
37+
</div>
38+
)
39+
}
40+
}

src/components/TodoItem.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Todo } from '../services/todos'
33

44
interface Props extends React.PropsWithChildren<{}> {
55
todo: Todo
6+
onComplete: (event: React.ChangeEvent<HTMLInputElement>) => void
67
}
78

89
const style: React.CSSProperties = {
@@ -12,10 +13,15 @@ const style: React.CSSProperties = {
1213

1314
export const TodoItem: React.FC<Props> = ({
1415
todo,
16+
onComplete,
1517
}: Props): React.ReactElement => {
1618
return (
1719
<div style={style}>
18-
<input type="checkbox" checked={todo.completed} />
20+
<input
21+
type="checkbox"
22+
checked={todo.completed}
23+
onChange={onComplete}
24+
/>
1925
<p>{todo.content}</p>
2026
</div>
2127
)

src/components/TodoList.tsx

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ interface State {
1111
todos: Todo[]
1212
}
1313

14-
const style: React.CSSProperties = {
14+
const defaultStyle: React.CSSProperties = {
1515
display: 'flex',
1616
flexDirection: 'column',
1717
}
1818

1919
export default class TodoList extends Component<Props, State> {
20-
state = {
20+
state: State = {
2121
todos: [],
2222
}
2323

@@ -31,14 +31,35 @@ export default class TodoList extends Component<Props, State> {
3131
}
3232
}
3333

34+
onComplete = (
35+
index: number,
36+
event: React.ChangeEvent<HTMLInputElement>,
37+
) => {
38+
const newTodos = [...this.state.todos]
39+
newTodos[index].completed = !newTodos[index].completed
40+
this.setState({ todos: newTodos })
41+
}
42+
3443
render() {
3544
return (
36-
<div style={{ ...style, ...this.props.style }}>
45+
<div
46+
style={{
47+
...defaultStyle,
48+
...this.props.style,
49+
}}
50+
>
3751
<h1>Todo List</h1>
3852
{this.state.todos.map((todo, index) => (
39-
<TodoItem key={index} todo={todo} />
53+
<TodoItem
54+
key={index}
55+
todo={todo}
56+
onComplete={event => {
57+
this.onComplete(index, event)
58+
}}
59+
/>
4060
))}
4161
</div>
62+
// <h1>Hello World</h1>
4263
)
4364
}
4465
}

0 commit comments

Comments
 (0)