mirror of
https://github.com/microsoft/frontend-bootcamp.git
synced 2026-01-26 14:56:42 +08:00
fleshed out up to step 6
This commit is contained in:
9
step06/index.html
Normal file
9
step06/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>
|
||||
|
||||
|
||||
27
step06/src/App.tsx
Normal file
27
step06/src/App.tsx
Normal file
@@ -0,0 +1,27 @@
|
||||
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 {
|
||||
render() {
|
||||
|
||||
const todos = [
|
||||
{key: 1, text: 'Todo 1', completed: true},
|
||||
{key: 2, text: 'Todo 2'},
|
||||
{key: 3, text: 'Todo 3'},
|
||||
{key: 4, text: 'Todo 4'},
|
||||
];
|
||||
|
||||
const filter = 'all';
|
||||
|
||||
return (
|
||||
<div>
|
||||
<TodoHeader filter={filter} />
|
||||
<TodoList todos={todos} filter={filter} />
|
||||
<TodoFooter todos={todos} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
11
step06/src/components/TodoFooter.tsx
Normal file
11
step06/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 className="button">Clear Completed</button>
|
||||
</footer>
|
||||
);
|
||||
};
|
||||
21
step06/src/components/TodoHeader.tsx
Normal file
21
step06/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 } = this.props;
|
||||
return (
|
||||
<div>
|
||||
<h1>todos</h1>
|
||||
<input className="textfield" />
|
||||
<button className="button add">Add</button>
|
||||
<div className="filter">
|
||||
<button className={filter == "all" ? "active" : ""}>all</button>
|
||||
<button className={filter == "active" ? "active" : ""}>active</button>
|
||||
<button className={filter == "completed" ? "active" : ""}>
|
||||
completed
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
28
step06/src/components/TodoList.tsx
Normal file
28
step06/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 } = 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 => {
|
||||
return (
|
||||
<TodoListItem {...todo} />
|
||||
);
|
||||
})
|
||||
|
||||
return (
|
||||
<ul className="todos">
|
||||
{TodoListItems}
|
||||
</ul>
|
||||
);
|
||||
}
|
||||
}
|
||||
15
step06/src/components/TodoListItem.tsx
Normal file
15
step06/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} = this.props;
|
||||
return (
|
||||
<li className="todo">
|
||||
<label>
|
||||
<input type="checkbox" checked={completed} /> {text}
|
||||
</label>
|
||||
</li>
|
||||
);
|
||||
}
|
||||
}
|
||||
4
step06/src/index.tsx
Normal file
4
step06/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
step06/src/style.css
Normal file
50
step06/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;
|
||||
}
|
||||
Reference in New Issue
Block a user