simplify 2-2 exercise

This commit is contained in:
Ken
2019-02-28 14:09:25 -08:00
parent 88425249ea
commit f6d185632c
7 changed files with 239 additions and 40 deletions

View File

@@ -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).
# 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)
2. Open each TSX file inside `exercise/src/components`
3. Replace some of the built-in HTML tags with these Fabric components:
Open the [documentation for DefaultButton](https://developer.microsoft.com/en-us/fabric/#/components/button)
- Stack
- DefaultButton
- Checkbox
- TextField
- Pivot (for the filter)
# Exercise 2: "Fabric"ize the TodoFooter.tsx
> 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
@@ -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`
- 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)

View File

@@ -11,7 +11,10 @@ export class TodoApp extends React.Component<any, Store> {
constructor(props) {
super(props);
this.state = {
todos: {},
todos: {
id0: { label: 'hello', completed: false },
id1: { label: 'world', completed: false }
},
filter: 'all'
};
}

View File

@@ -5,16 +5,86 @@ import { TodoHeader } from './TodoHeader';
import { TodoList } from './TodoList';
import { Store } from '../store';
let index = 0;
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() {
const { filter, todos } = this.state;
return (
<Stack horizontalAlign="center">
<Stack style={{ width: 400 }} gap={25}>
<TodoHeader />
<TodoList />
<TodoFooter />
<TodoHeader addTodo={this._addTodo} setFilter={this._setFilter} filter={filter} />
<TodoList complete={this._complete} todos={todos} filter={filter} remove={this._remove} edit={this._edit} />
<TodoFooter clear={this._clear} todos={todos} />
</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
});
};
}

View File

@@ -1,7 +1,20 @@
import React from 'react';
import { Store } from '../store';
export const TodoFooter = (props: any) => {
const itemCount = 3;
// TODO: import DefaultButton, Stack, and Text
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 (
<footer>

View File

@@ -1,19 +1,56 @@
import React from 'react';
import { Stack, Text, Pivot, PivotItem, TextField, PrimaryButton } from 'office-ui-fabric-react';
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() {
return (
<div>
<h1>todos - step2-02 exercise</h1>
<input className="textfield" placeholder="add todo" />
<button className="button add">Add</button>
<div className="filter">
<button>all</button>
<button>active</button>
<button>completed</button>
</div>
</div>
<Stack>
<Stack horizontal horizontalAlign="center">
<Text variant="xxLarge">todos - step2-02 demo</Text>
</Stack>
<Stack horizontal>
<Stack.Item grow>
<TextField placeholder="What needs to be done?" value={this.state.labelInput} onChange={this.onChange} />
</Stack.Item>
<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);
};
}

View File

@@ -1,13 +1,27 @@
import React from 'react';
import { Stack } from 'office-ui-fabric-react';
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 (
<ul className="todos">
<TodoListItem />
<TodoListItem />
<TodoListItem />
</ul>
<Stack gap={10}>
{filteredTodos.map(id => (
<TodoListItem key={id} id={id} todos={todos} complete={complete} remove={remove} edit={edit} />
))}
</Stack>
);
};

View File

@@ -1,13 +1,75 @@
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() {
const { todos, id, complete, remove } = this.props;
const item = todos[id];
return (
<li className="todo">
<label>
<input type="checkbox" defaultChecked={false} /> test item
</label>
</li>
<Stack horizontal verticalAlign="center" horizontalAlign="space-between">
{!this.state.editing && (
<>
<Checkbox label={item.label} checked={item.completed} onChange={() => complete(id)} />
<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 });
};
}