mirror of
https://github.com/microsoft/frontend-bootcamp.git
synced 2026-01-26 14:56:42 +08:00
merged stuff in
This commit is contained in:
@@ -4,29 +4,6 @@ import { TodoListItem } from './TodoListItem';
|
|||||||
export class TodoList extends React.Component<any, any> {
|
export class TodoList extends React.Component<any, any> {
|
||||||
render() {
|
render() {
|
||||||
const { filter, todos } = this.props;
|
const { filter, todos } = this.props;
|
||||||
let filteredTodos: typeof todos = {};
|
|
||||||
|
|
||||||
switch (filter) {
|
|
||||||
case 'completed':
|
|
||||||
Object.keys(todos).forEach(id => {
|
|
||||||
if (todos[id].completed) {
|
|
||||||
filteredTodos[id] = todos[id];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'active':
|
|
||||||
Object.keys(todos).forEach(id => {
|
|
||||||
if (!todos[id].completed) {
|
|
||||||
filteredTodos[id] = todos[id];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
filteredTodos = todos;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ul className="todos">
|
<ul className="todos">
|
||||||
|
|||||||
9
step07/index.html
Normal file
9
step07/index.html
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<link rel="stylesheet" href="./src/style.css" />
|
||||||
|
<body>
|
||||||
|
<div id="app"></div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
||||||
|
|
||||||
78
step07/src/App.tsx
Normal file
78
step07/src/App.tsx
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
import React from "react";
|
||||||
|
import { TodoFooter } from "./components/TodoFooter";
|
||||||
|
import { TodoHeader } from "./components/TodoHeader";
|
||||||
|
import { TodoList } from "./components/TodoList";
|
||||||
|
|
||||||
|
export class TodoApp extends React.Component<any, any> {
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.state = {
|
||||||
|
inputValue: "",
|
||||||
|
todos: [],
|
||||||
|
filter: "all"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
render() {
|
||||||
|
const { filter, todos, inputValue } = this.state;
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<TodoHeader
|
||||||
|
onInputChange={this._updateInput}
|
||||||
|
inputValue={inputValue}
|
||||||
|
onTodoSubmit={this._addTodo}
|
||||||
|
onFilterClick={this._updateFilter}
|
||||||
|
filter={filter}
|
||||||
|
/>
|
||||||
|
<TodoList onTodoToggle={this._toggleTodoComplete} todos={todos} filter={filter} />
|
||||||
|
<TodoFooter onClearClick={this._removeCompletedTodos} todos={todos} />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
_addTodo = () => {
|
||||||
|
const { todos, inputValue } = this.state;
|
||||||
|
const id = todos[0] ? todos[0].id + 1 : 0;
|
||||||
|
const newTodos = [
|
||||||
|
{
|
||||||
|
id: id,
|
||||||
|
key: id,
|
||||||
|
text: inputValue
|
||||||
|
},
|
||||||
|
...todos
|
||||||
|
];
|
||||||
|
|
||||||
|
this.setState({
|
||||||
|
todos: newTodos,
|
||||||
|
inputValue: ""
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
_toggleTodoComplete = (id) => {
|
||||||
|
this.setState({
|
||||||
|
todos: this.state.todos.map(todo => {
|
||||||
|
if (todo.id == id) {todo.completed = !todo.completed}
|
||||||
|
return todo;
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
_removeCompletedTodos = () => {
|
||||||
|
this.setState({
|
||||||
|
todos: this.state.todos.filter(todo => {
|
||||||
|
return !todo.completed;
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
_updateFilter = (filter) => {
|
||||||
|
this.setState({
|
||||||
|
filter: filter
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
_updateInput = ev => {
|
||||||
|
this.setState({
|
||||||
|
inputValue: ev.target.value
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
11
step07/src/components/TodoFooter.tsx
Normal file
11
step07/src/components/TodoFooter.tsx
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import React from "react";
|
||||||
|
|
||||||
|
export const TodoFooter = (props: any) => {
|
||||||
|
const items = props.todos.filter(todo => !todo.completed)
|
||||||
|
return (
|
||||||
|
<footer>
|
||||||
|
<span> {items.length} items left </span>
|
||||||
|
<button onClick={props.onClearClick} className="button">Clear Completed</button>
|
||||||
|
</footer>
|
||||||
|
);
|
||||||
|
};
|
||||||
21
step07/src/components/TodoHeader.tsx
Normal file
21
step07/src/components/TodoHeader.tsx
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import React from "react";
|
||||||
|
|
||||||
|
export class TodoHeader extends React.Component<any, any> {
|
||||||
|
render() {
|
||||||
|
const { filter, inputValue, onInputChange, onTodoSubmit, onFilterClick } = this.props;
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h1>todos</h1>
|
||||||
|
<input value={inputValue} onChange={onInputChange} className="textfield" />
|
||||||
|
<button onClick={onTodoSubmit} className="button add">Add</button>
|
||||||
|
<div className="filter">
|
||||||
|
<button onClick={() => onFilterClick('all')} className={filter == "all" ? "active" : ""}>all</button>
|
||||||
|
<button onClick={() => onFilterClick('active')} className={filter == "active" ? "active" : ""}>active</button>
|
||||||
|
<button onClick={() => onFilterClick('completed')} className={filter == "completed" ? "active" : ""}>
|
||||||
|
completed
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
28
step07/src/components/TodoList.tsx
Normal file
28
step07/src/components/TodoList.tsx
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { TodoListItem } from './TodoListItem';
|
||||||
|
|
||||||
|
export class TodoList extends React.Component<any, any> {
|
||||||
|
render() {
|
||||||
|
const { filter, todos, onTodoToggle } = this.props;
|
||||||
|
let filteredTodos: typeof todos = {};
|
||||||
|
|
||||||
|
filteredTodos = todos.filter(todo => {
|
||||||
|
const matchesActive = filter == 'active' && !todo.completed;
|
||||||
|
const matchesCompleted = filter == 'completed' && todo.completed;
|
||||||
|
|
||||||
|
return filter == 'all' || matchesActive || matchesCompleted;
|
||||||
|
})
|
||||||
|
|
||||||
|
const TodoListItems = filteredTodos.map((todo, i) => {
|
||||||
|
return (
|
||||||
|
<TodoListItem onTodoToggle={onTodoToggle} {...todo} />
|
||||||
|
);
|
||||||
|
})
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ul className="todos">
|
||||||
|
{TodoListItems}
|
||||||
|
</ul>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
15
step07/src/components/TodoListItem.tsx
Normal file
15
step07/src/components/TodoListItem.tsx
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import React from "react";
|
||||||
|
|
||||||
|
export class TodoListItem extends React.Component<any, any> {
|
||||||
|
|
||||||
|
render() {
|
||||||
|
const {text, completed, onTodoToggle, id} = this.props;
|
||||||
|
return (
|
||||||
|
<li className="todo">
|
||||||
|
<label>
|
||||||
|
<input onClick={() => onTodoToggle(id)} type="checkbox" checked={completed} /> {text}
|
||||||
|
</label>
|
||||||
|
</li>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
4
step07/src/index.tsx
Normal file
4
step07/src/index.tsx
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
import React from "react";
|
||||||
|
import ReactDOM from "react-dom";
|
||||||
|
import { TodoApp } from "./App";
|
||||||
|
ReactDOM.render(<TodoApp />, document.getElementById("app"));
|
||||||
50
step07/src/style.css
Normal file
50
step07/src/style.css
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
body {
|
||||||
|
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
|
||||||
|
width: 400px;
|
||||||
|
margin: 20px auto;
|
||||||
|
}
|
||||||
|
h1 {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.textfield {
|
||||||
|
width: 80%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.add {
|
||||||
|
margin-left: 5%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.button {
|
||||||
|
border: none;
|
||||||
|
padding: 5px 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.filter {
|
||||||
|
margin: 10px 0 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.filter button {
|
||||||
|
background: transparent;
|
||||||
|
border: none;
|
||||||
|
}
|
||||||
|
.filter .active {
|
||||||
|
border-bottom: 2px solid blue;
|
||||||
|
}
|
||||||
|
|
||||||
|
.todos {
|
||||||
|
list-style: none;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
footer {
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
|
||||||
|
footer span {
|
||||||
|
flex-grow: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.hidden {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ const entries = {
|
|||||||
step04: './step04/src/index',
|
step04: './step04/src/index',
|
||||||
step05: './step05/src/index',
|
step05: './step05/src/index',
|
||||||
step06: './step06/src/index',
|
step06: './step06/src/index',
|
||||||
|
step07: './step07/src/index',
|
||||||
'step2-01': './step2-01/src/index',
|
'step2-01': './step2-01/src/index',
|
||||||
playground: './playground/src/index'
|
playground: './playground/src/index'
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user