mirror of
https://github.com/microsoft/frontend-bootcamp.git
synced 2026-01-26 14:56:42 +08:00
simplify 2-2 exercise
This commit is contained in:
@@ -107,19 +107,19 @@ Stack abstracts these CSS styles and provides typings to make them more discover
|
|||||||
|
|
||||||
Check out a cookbook of sorts in our [documentation](https://developer.microsoft.com/en-us/fabric#/components/stack).
|
Check out a cookbook of sorts in our [documentation](https://developer.microsoft.com/en-us/fabric#/components/stack).
|
||||||
|
|
||||||
# Exercise
|
# Exercise 1: Getting familiar with the Fabric documentation site:
|
||||||
|
|
||||||
1. Open the [documentation for DefaultButton](https://developer.microsoft.com/en-us/fabric/#/components/button)
|
Open the [documentation for DefaultButton](https://developer.microsoft.com/en-us/fabric/#/components/button)
|
||||||
2. Open each TSX file inside `exercise/src/components`
|
|
||||||
3. Replace some of the built-in HTML tags with these Fabric components:
|
|
||||||
|
|
||||||
- Stack
|
# Exercise 2: "Fabric"ize the TodoFooter.tsx
|
||||||
- DefaultButton
|
|
||||||
- Checkbox
|
|
||||||
- TextField
|
|
||||||
- Pivot (for the filter)
|
|
||||||
|
|
||||||
> Hint: think about how you can use Stacks to make the layout look nicer.
|
1. Open TSX file inside `exercise/src/components/TodoFooter.tsx`
|
||||||
|
2. Follow the top TODO comment to import Stack, Text and DefaultButton components from Fabric
|
||||||
|
3. Follow the TODO comment to:
|
||||||
|
|
||||||
|
- replace `<footer>` with a `<Stack>`
|
||||||
|
- replace `<span>` with a `<Text>`
|
||||||
|
- replace `<button>` with a `<DefaultButton>`
|
||||||
|
|
||||||
## Bonus Exercise
|
## Bonus Exercise
|
||||||
|
|
||||||
@@ -127,4 +127,4 @@ GO WILD! There are so many components in the Fabric library! Try to put some com
|
|||||||
|
|
||||||
- Importing components from `office-ui-fabric-react`
|
- Importing components from `office-ui-fabric-react`
|
||||||
- Customizing component with props found on the documentation site
|
- Customizing component with props found on the documentation site
|
||||||
- Customize component with render props (these will be called onRender___ or similar)
|
- Customize component with render props (these will be called onRender\_\_\_ or similar)
|
||||||
|
|||||||
@@ -11,7 +11,10 @@ export class TodoApp extends React.Component<any, Store> {
|
|||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
this.state = {
|
this.state = {
|
||||||
todos: {},
|
todos: {
|
||||||
|
id0: { label: 'hello', completed: false },
|
||||||
|
id1: { label: 'world', completed: false }
|
||||||
|
},
|
||||||
filter: 'all'
|
filter: 'all'
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,16 +5,86 @@ import { TodoHeader } from './TodoHeader';
|
|||||||
import { TodoList } from './TodoList';
|
import { TodoList } from './TodoList';
|
||||||
import { Store } from '../store';
|
import { Store } from '../store';
|
||||||
|
|
||||||
|
let index = 0;
|
||||||
|
|
||||||
export class TodoApp extends React.Component<any, Store> {
|
export class TodoApp extends React.Component<any, Store> {
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.state = {
|
||||||
|
todos: {
|
||||||
|
id0: { label: 'hello', completed: false },
|
||||||
|
id1: { label: 'world', completed: false }
|
||||||
|
},
|
||||||
|
filter: 'all'
|
||||||
|
};
|
||||||
|
}
|
||||||
render() {
|
render() {
|
||||||
|
const { filter, todos } = this.state;
|
||||||
return (
|
return (
|
||||||
<Stack horizontalAlign="center">
|
<Stack horizontalAlign="center">
|
||||||
<Stack style={{ width: 400 }} gap={25}>
|
<Stack style={{ width: 400 }} gap={25}>
|
||||||
<TodoHeader />
|
<TodoHeader addTodo={this._addTodo} setFilter={this._setFilter} filter={filter} />
|
||||||
<TodoList />
|
<TodoList complete={this._complete} todos={todos} filter={filter} remove={this._remove} edit={this._edit} />
|
||||||
<TodoFooter />
|
<TodoFooter clear={this._clear} todos={todos} />
|
||||||
</Stack>
|
</Stack>
|
||||||
</Stack>
|
</Stack>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _addTodo = label => {
|
||||||
|
const { todos } = this.state;
|
||||||
|
const id = index++;
|
||||||
|
|
||||||
|
this.setState({
|
||||||
|
todos: { ...todos, [id]: { label } }
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
private _remove = id => {
|
||||||
|
const newTodos = { ...this.state.todos };
|
||||||
|
delete newTodos[id];
|
||||||
|
|
||||||
|
this.setState({
|
||||||
|
todos: newTodos
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
private _complete = id => {
|
||||||
|
const newTodos = { ...this.state.todos };
|
||||||
|
newTodos[id].completed = !newTodos[id].completed;
|
||||||
|
|
||||||
|
this.setState({
|
||||||
|
todos: newTodos
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
private _edit = (id, label) => {
|
||||||
|
const newTodos = { ...this.state.todos };
|
||||||
|
newTodos[id] = { ...newTodos[id], label };
|
||||||
|
|
||||||
|
this.setState({
|
||||||
|
todos: newTodos
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
private _clear = () => {
|
||||||
|
const { todos } = this.state;
|
||||||
|
const newTodos = {};
|
||||||
|
|
||||||
|
Object.keys(this.state.todos).forEach(id => {
|
||||||
|
if (!todos[id].completed) {
|
||||||
|
newTodos[id] = todos[id];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
this.setState({
|
||||||
|
todos: newTodos
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
private _setFilter = filter => {
|
||||||
|
this.setState({
|
||||||
|
filter: filter
|
||||||
|
});
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,20 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import { Store } from '../store';
|
||||||
|
|
||||||
export const TodoFooter = (props: any) => {
|
// TODO: import DefaultButton, Stack, and Text
|
||||||
const itemCount = 3;
|
|
||||||
|
interface TodoFooterProps {
|
||||||
|
clear: () => void;
|
||||||
|
todos: Store['todos'];
|
||||||
|
}
|
||||||
|
|
||||||
|
export const TodoFooter = (props: TodoFooterProps) => {
|
||||||
|
const itemCount = Object.keys(props.todos).filter(id => !props.todos[id].completed).length;
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
// 1. replace the <footer> with the Fabric control <Stack>
|
||||||
|
// 2. replace the <span> with the Fabric control <Text>
|
||||||
|
// 3. replace the <button> with Fabric control <DefaultButton>
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<footer>
|
<footer>
|
||||||
|
|||||||
@@ -1,19 +1,56 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import { Stack, Text, Pivot, PivotItem, TextField, PrimaryButton } from 'office-ui-fabric-react';
|
||||||
import { FilterTypes } from '../store';
|
import { FilterTypes } from '../store';
|
||||||
|
|
||||||
export class TodoHeader extends React.Component<{}, {}> {
|
interface TodoHeaderProps {
|
||||||
|
addTodo: (label: string) => void;
|
||||||
|
setFilter: (filter: FilterTypes) => void;
|
||||||
|
filter: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TodoHeaderState {
|
||||||
|
labelInput: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class TodoHeader extends React.Component<TodoHeaderProps, TodoHeaderState> {
|
||||||
|
constructor(props: TodoHeaderProps) {
|
||||||
|
super(props);
|
||||||
|
this.state = { labelInput: undefined };
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div>
|
<Stack>
|
||||||
<h1>todos - step2-02 exercise</h1>
|
<Stack horizontal horizontalAlign="center">
|
||||||
<input className="textfield" placeholder="add todo" />
|
<Text variant="xxLarge">todos - step2-02 demo</Text>
|
||||||
<button className="button add">Add</button>
|
</Stack>
|
||||||
<div className="filter">
|
|
||||||
<button>all</button>
|
<Stack horizontal>
|
||||||
<button>active</button>
|
<Stack.Item grow>
|
||||||
<button>completed</button>
|
<TextField placeholder="What needs to be done?" value={this.state.labelInput} onChange={this.onChange} />
|
||||||
</div>
|
</Stack.Item>
|
||||||
</div>
|
<PrimaryButton onClick={this.onAdd}>Add</PrimaryButton>
|
||||||
|
</Stack>
|
||||||
|
|
||||||
|
<Pivot onLinkClick={this.onFilter}>
|
||||||
|
<PivotItem headerText="all" />
|
||||||
|
<PivotItem headerText="active" />
|
||||||
|
<PivotItem headerText="completed" />
|
||||||
|
</Pivot>
|
||||||
|
</Stack>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private onAdd = () => {
|
||||||
|
this.props.addTodo(this.state.labelInput);
|
||||||
|
this.setState({ labelInput: undefined });
|
||||||
|
};
|
||||||
|
|
||||||
|
private onChange = (evt: React.FormEvent<HTMLInputElement>, newValue: string) => {
|
||||||
|
this.setState({ labelInput: newValue });
|
||||||
|
};
|
||||||
|
|
||||||
|
private onFilter = (item: PivotItem) => {
|
||||||
|
this.props.setFilter(item.props.headerText as FilterTypes);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,27 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { Stack } from 'office-ui-fabric-react';
|
import { Stack } from 'office-ui-fabric-react';
|
||||||
import { TodoListItem } from './TodoListItem';
|
import { TodoListItem } from './TodoListItem';
|
||||||
|
import { Store, FilterTypes } from '../store';
|
||||||
|
|
||||||
|
interface TodoListProps {
|
||||||
|
complete: (id: string) => void;
|
||||||
|
remove: (id: string) => void;
|
||||||
|
todos: Store['todos'];
|
||||||
|
filter: FilterTypes;
|
||||||
|
edit: (id: string, label: string) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const TodoList = (props: TodoListProps) => {
|
||||||
|
const { filter, todos, complete, remove, edit } = props;
|
||||||
|
const filteredTodos = Object.keys(todos).filter(id => {
|
||||||
|
return filter === 'all' || (filter === 'completed' && todos[id].completed) || (filter === 'active' && !todos[id].completed);
|
||||||
|
});
|
||||||
|
|
||||||
export const TodoList = (props: any) => {
|
|
||||||
return (
|
return (
|
||||||
<ul className="todos">
|
<Stack gap={10}>
|
||||||
<TodoListItem />
|
{filteredTodos.map(id => (
|
||||||
<TodoListItem />
|
<TodoListItem key={id} id={id} todos={todos} complete={complete} remove={remove} edit={edit} />
|
||||||
<TodoListItem />
|
))}
|
||||||
</ul>
|
</Stack>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,13 +1,75 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import { Stack, Checkbox, IconButton, TextField, DefaultButton } from 'office-ui-fabric-react';
|
||||||
|
import { Store } from '../store';
|
||||||
|
|
||||||
|
interface TodoListItemProps {
|
||||||
|
id: string;
|
||||||
|
todos: Store['todos'];
|
||||||
|
remove: (id: string) => void;
|
||||||
|
complete: (id: string) => void;
|
||||||
|
edit: (id: string, label: string) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TodoListItemState {
|
||||||
|
editing: boolean;
|
||||||
|
editLabel: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class TodoListItem extends React.Component<TodoListItemProps, TodoListItemState> {
|
||||||
|
constructor(props: TodoListItemProps) {
|
||||||
|
super(props);
|
||||||
|
this.state = { editing: false, editLabel: undefined };
|
||||||
|
}
|
||||||
|
|
||||||
export class TodoListItem extends React.Component<any, any> {
|
|
||||||
render() {
|
render() {
|
||||||
|
const { todos, id, complete, remove } = this.props;
|
||||||
|
const item = todos[id];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<li className="todo">
|
<Stack horizontal verticalAlign="center" horizontalAlign="space-between">
|
||||||
<label>
|
{!this.state.editing && (
|
||||||
<input type="checkbox" defaultChecked={false} /> test item
|
<>
|
||||||
</label>
|
<Checkbox label={item.label} checked={item.completed} onChange={() => complete(id)} />
|
||||||
</li>
|
<div>
|
||||||
|
<IconButton iconProps={{ iconName: 'Edit' }} className="clearButton" onClick={this.onEdit} />
|
||||||
|
<IconButton iconProps={{ iconName: 'Cancel' }} className="clearButton" onClick={() => remove(id)} />
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{this.state.editing && (
|
||||||
|
<Stack.Item grow>
|
||||||
|
<Stack horizontal>
|
||||||
|
<Stack.Item grow>
|
||||||
|
<TextField value={this.state.editLabel} onChange={this.onChange} />
|
||||||
|
</Stack.Item>
|
||||||
|
<DefaultButton onClick={this.onDoneEdit}>Save</DefaultButton>
|
||||||
|
</Stack>
|
||||||
|
</Stack.Item>
|
||||||
|
)}
|
||||||
|
</Stack>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private onEdit = () => {
|
||||||
|
const { todos, id } = this.props;
|
||||||
|
const { label } = todos[id];
|
||||||
|
|
||||||
|
this.setState({
|
||||||
|
editing: true,
|
||||||
|
editLabel: this.state.editLabel || label
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
private onDoneEdit = () => {
|
||||||
|
this.props.edit(this.props.id, this.state.editLabel);
|
||||||
|
this.setState({
|
||||||
|
editing: false,
|
||||||
|
editLabel: undefined
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
private onChange = (evt: React.FormEvent<HTMLInputElement>, newValue: string) => {
|
||||||
|
this.setState({ editLabel: newValue });
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user