mirror of
https://github.com/microsoft/frontend-bootcamp.git
synced 2026-01-26 14:56:42 +08:00
editing begin
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import { Action, ActionCreator } from 'redux';
|
import { Action, ActionCreator } from 'redux';
|
||||||
|
|
||||||
export type ActionTypes = 'add' | 'remove' | 'edit' | 'complete' | 'completeAll' | 'clear' | 'filter';
|
export type ActionTypes = 'add' | 'remove' | 'edit' | 'complete' | 'completeAll' | 'clear' | 'filter';
|
||||||
type TodoActionCreator = ActionCreator<Action<ActionTypes>>;
|
|
||||||
export interface TodoAction extends Action<ActionTypes> {
|
export interface TodoAction extends Action<ActionTypes> {
|
||||||
[extraProps: string]: any;
|
[extraProps: string]: any;
|
||||||
}
|
}
|
||||||
@@ -12,4 +12,4 @@ export const edit = (id: string, label: string): TodoAction => ({ type: 'edit',
|
|||||||
export const complete = (id: string): TodoAction => ({ type: 'complete', id });
|
export const complete = (id: string): TodoAction => ({ type: 'complete', id });
|
||||||
export const completeAll = (): TodoAction => ({ type: 'completeAll' });
|
export const completeAll = (): TodoAction => ({ type: 'completeAll' });
|
||||||
export const clear = (): TodoAction => ({ type: 'clear' });
|
export const clear = (): TodoAction => ({ type: 'clear' });
|
||||||
export const filter = (filterTypes: string): TodoAction => ({ type: 'filter', filterTypes });
|
export const filter = (filterTypes: string): TodoAction => ({ type: 'filter', filter: filterTypes });
|
||||||
|
|||||||
@@ -10,20 +10,22 @@ export interface TodoAppProps {
|
|||||||
filter: FilterTypes;
|
filter: FilterTypes;
|
||||||
add: (label: string) => void;
|
add: (label: string) => void;
|
||||||
remove: (id: string) => void;
|
remove: (id: string) => void;
|
||||||
|
edit: (id: string, label: string) => void;
|
||||||
complete: (id: string) => void;
|
complete: (id: string) => void;
|
||||||
|
clear: () => void;
|
||||||
setFilter: (filter: FilterTypes) => void;
|
setFilter: (filter: FilterTypes) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class TodoApp extends React.Component<TodoAppProps> {
|
export class TodoApp extends React.Component<TodoAppProps> {
|
||||||
render() {
|
render() {
|
||||||
const { todos, filter, add, remove, setFilter, complete } = this.props;
|
const { todos, filter, add, remove, setFilter, complete, clear, edit } = this.props;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack horizontalAlign="center">
|
<Stack horizontalAlign="center">
|
||||||
<Stack style={{ width: 650 }} verticalGap={25}>
|
<Stack style={{ width: 400 }} verticalGap={25}>
|
||||||
<TodoHeader {...{ add, remove, filter }} />
|
<TodoHeader {...{ add, remove, filter: setFilter }} />
|
||||||
<TodoList {...{ todos, filter, complete }} />
|
<TodoList {...{ todos, filter, complete, remove, edit }} />
|
||||||
<TodoFooter {...{ todos, setFilter }} />
|
<TodoFooter {...{ todos, setFilter, clear }} />
|
||||||
</Stack>
|
</Stack>
|
||||||
</Stack>
|
</Stack>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ export function mapDispatchToProps(dispatch: Dispatch<actions.TodoAction>) {
|
|||||||
remove: (id: string) => dispatch(actions.remove(id)),
|
remove: (id: string) => dispatch(actions.remove(id)),
|
||||||
complete: (id: string) => dispatch(actions.complete(id)),
|
complete: (id: string) => dispatch(actions.complete(id)),
|
||||||
completeAll: () => dispatch(actions.completeAll()),
|
completeAll: () => dispatch(actions.completeAll()),
|
||||||
|
edit: (id: string, label: string) => dispatch(actions.edit(id, label)),
|
||||||
clear: () => dispatch(actions.clear()),
|
clear: () => dispatch(actions.clear()),
|
||||||
setFilter: (filter: FilterTypes) => dispatch(actions.filter(filter))
|
setFilter: (filter: FilterTypes) => dispatch(actions.filter(filter))
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,12 +1,22 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { Text, Stack } from '@uifabric/experiments';
|
import { Text, Stack } from '@uifabric/experiments';
|
||||||
|
import { TodoItem } from '../store';
|
||||||
|
import { DefaultButton } from 'office-ui-fabric-react';
|
||||||
|
|
||||||
export interface TodoFooterProps {}
|
export interface TodoFooterProps {
|
||||||
|
todos: { [id: string]: TodoItem };
|
||||||
|
clear: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
export const TodoFooter = (props: TodoFooterProps) => {
|
export const TodoFooter = (props: TodoFooterProps) => {
|
||||||
|
const itemCount = Object.keys(props.todos).filter(id => !props.todos[id].completed).length;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack horizontal horizontalAlign="center">
|
<Stack horizontal horizontalAlign="space-between">
|
||||||
<Text>1 item left</Text>
|
<Text>
|
||||||
|
{itemCount} item{itemCount > 1 ? 's' : ''} left
|
||||||
|
</Text>
|
||||||
|
<DefaultButton onClick={() => props.clear()}>Clear Completed</DefaultButton>
|
||||||
</Stack>
|
</Stack>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,10 +2,12 @@ import React from 'react';
|
|||||||
import { Text, Stack } from '@uifabric/experiments';
|
import { Text, Stack } from '@uifabric/experiments';
|
||||||
import { Pivot, PivotItem, TextField } from 'office-ui-fabric-react';
|
import { Pivot, PivotItem, TextField } from 'office-ui-fabric-react';
|
||||||
import { add } from '../actions';
|
import { add } from '../actions';
|
||||||
|
import { FilterTypes } from '../store';
|
||||||
|
|
||||||
export interface TodoHeaderProps {
|
export interface TodoHeaderProps {
|
||||||
add: (label: string) => void;
|
add: (label: string) => void;
|
||||||
remove: (id: string) => void;
|
remove: (id: string) => void;
|
||||||
|
filter: (filter: FilterTypes) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface TodoHeaderState {
|
export interface TodoHeaderState {
|
||||||
@@ -29,6 +31,10 @@ export class TodoHeader extends React.Component<TodoHeaderProps, TodoHeaderState
|
|||||||
this.setState({ labelInput: newValue });
|
this.setState({ labelInput: newValue });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
onFilter = (item: PivotItem) => {
|
||||||
|
this.props.filter(item.props.headerText as FilterTypes);
|
||||||
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<Stack>
|
<Stack>
|
||||||
@@ -43,7 +49,7 @@ export class TodoHeader extends React.Component<TodoHeaderProps, TodoHeaderState
|
|||||||
onKeyPress={this.onKeyPress}
|
onKeyPress={this.onKeyPress}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Pivot>
|
<Pivot onLinkClick={this.onFilter}>
|
||||||
<PivotItem headerText="all" />
|
<PivotItem headerText="all" />
|
||||||
<PivotItem headerText="active" />
|
<PivotItem headerText="active" />
|
||||||
<PivotItem headerText="completed" />
|
<PivotItem headerText="completed" />
|
||||||
|
|||||||
@@ -6,17 +6,47 @@ import { TodoItem, FilterTypes } from '../store';
|
|||||||
export interface TodoListProps {
|
export interface TodoListProps {
|
||||||
todos: { [id: string]: TodoItem };
|
todos: { [id: string]: TodoItem };
|
||||||
filter: FilterTypes;
|
filter: FilterTypes;
|
||||||
|
edit: (id: string, label: string) => void;
|
||||||
complete: (id: string) => void;
|
complete: (id: string) => void;
|
||||||
|
remove: (id: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class TodoList extends React.Component<TodoListProps> {
|
export class TodoList extends React.Component<TodoListProps> {
|
||||||
render() {
|
render() {
|
||||||
const { filter, todos } = this.props;
|
const { filter, todos } = this.props;
|
||||||
|
let filteredTodos = todos;
|
||||||
|
|
||||||
|
switch (filter) {
|
||||||
|
case 'completed':
|
||||||
|
filteredTodos = Object.keys(todos).reduce(
|
||||||
|
(collection, id) => (todos[id].completed ? { ...collection, id: todos[id] } : collection),
|
||||||
|
{}
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'active':
|
||||||
|
filteredTodos = Object.keys(todos).reduce(
|
||||||
|
(collection, id) => (!todos[id].completed ? { ...collection, id: todos[id] } : collection),
|
||||||
|
{}
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack verticalGap={10}>
|
<Stack verticalGap={10}>
|
||||||
{Object.keys(todos).map(id => {
|
{Object.keys(filteredTodos).map(id => {
|
||||||
const todo = todos[id];
|
const todo = filteredTodos[id];
|
||||||
return <TodoListItem key={id} checked={todo.completed} label={todo.label} complete={this.props.complete} id={id} />;
|
return (
|
||||||
|
<TodoListItem
|
||||||
|
key={id}
|
||||||
|
checked={todo.completed}
|
||||||
|
label={todo.label}
|
||||||
|
complete={this.props.complete}
|
||||||
|
id={id}
|
||||||
|
edit={this.props.edit}
|
||||||
|
remove={this.props.remove}
|
||||||
|
/>
|
||||||
|
);
|
||||||
})}
|
})}
|
||||||
</Stack>
|
</Stack>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,31 +1,84 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { Stack } from '@uifabric/experiments';
|
import { Stack, Text } from '@uifabric/experiments';
|
||||||
import { Checkbox, IconButton } from 'office-ui-fabric-react';
|
import { Checkbox, IconButton, TextField } from 'office-ui-fabric-react';
|
||||||
import { mergeStyles } from '@uifabric/styling';
|
import { mergeStyles } from '@uifabric/styling';
|
||||||
|
|
||||||
export interface TodoListItemProps {
|
export interface TodoListItemProps {
|
||||||
id: string;
|
id: string;
|
||||||
checked: boolean;
|
checked: boolean;
|
||||||
label: string;
|
label: string;
|
||||||
|
edit: (id: string, label: string) => void;
|
||||||
complete: (id: string) => void;
|
complete: (id: string) => void;
|
||||||
|
remove: (id: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const TodoListItem = (props: TodoListItemProps) => {
|
export interface TodoListItemState {
|
||||||
const className = mergeStyles({
|
editing: boolean;
|
||||||
selectors: {
|
editLabel: string;
|
||||||
':global(.clearButton)': {
|
}
|
||||||
display: 'none'
|
|
||||||
},
|
|
||||||
'&:hover :global(.clearButton)': {
|
|
||||||
display: 'block'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return (
|
const className = mergeStyles({
|
||||||
<Stack horizontal className={className}>
|
selectors: {
|
||||||
<Checkbox label={props.label} checked={props.checked} onChange={() => props.complete(props.id)} />
|
'.clearButton': {
|
||||||
<IconButton iconProps={{ iconName: 'Cancel' }} className="clearButton" />
|
visibility: 'hidden'
|
||||||
</Stack>
|
},
|
||||||
);
|
'&:hover .clearButton': {
|
||||||
};
|
visibility: 'visible'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export class TodoListItem extends React.Component<TodoListItemProps, TodoListItemState> {
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
constructor(props: TodoListItemProps) {
|
||||||
|
super(props);
|
||||||
|
this.state = { editing: false, editLabel: undefined };
|
||||||
|
}
|
||||||
|
|
||||||
|
onEdit = () => {
|
||||||
|
this.setState(prevState => ({
|
||||||
|
editing: true,
|
||||||
|
editLabel: prevState.editLabel || this.props.label
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
onDoneEdit = () => {
|
||||||
|
this.props.edit(this.props.id, this.state.editLabel);
|
||||||
|
this.setState(prevState => ({
|
||||||
|
editing: false,
|
||||||
|
editLabel: undefined
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
onKeyDown = (evt: React.KeyboardEvent) => {
|
||||||
|
if (evt.which === 13) {
|
||||||
|
this.onDoneEdit();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
onChange = (evt: React.FormEvent<HTMLInputElement>, newValue: string) => {
|
||||||
|
this.setState({ editLabel: newValue });
|
||||||
|
};
|
||||||
|
|
||||||
|
render() {
|
||||||
|
const { label, checked, complete, remove, id } = this.props;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Stack horizontal className={className} verticalAlign="center" horizontalAlign="space-between">
|
||||||
|
{!this.state.editing && (
|
||||||
|
<>
|
||||||
|
<Checkbox label={label} checked={checked} 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 && <TextField value={this.state.editLabel} onChange={this.onChange} onKeyPress={this.onKeyDown} />}
|
||||||
|
</Stack>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,13 +2,23 @@ import { Reducer } from 'redux';
|
|||||||
import { ActionTypes, TodoAction } from '../actions';
|
import { ActionTypes, TodoAction } from '../actions';
|
||||||
import { Draft, produce } from 'immer';
|
import { Draft, produce } from 'immer';
|
||||||
|
|
||||||
export function createReducer<T>(
|
export type ImmerReducer<T> = (state: Draft<T>, action: TodoAction) => T;
|
||||||
initialState: T,
|
export type HandlerMap<T> = { [actionType in ActionTypes]?: ImmerReducer<T> };
|
||||||
handlers: { [actionType in ActionTypes]?: (state: Draft<T>, action: TodoAction) => T }
|
|
||||||
): Reducer<T> {
|
function isHandlerFunction<T>(handlerOrMap: HandlerMap<T> | ImmerReducer<T>): handlerOrMap is ImmerReducer<T> {
|
||||||
|
if (typeof handlerOrMap === 'function') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createReducer<T>(initialState: T, handlerOrMap: HandlerMap<T> | ImmerReducer<T>): Reducer<T> {
|
||||||
return function reducer(state = initialState, action: TodoAction): T {
|
return function reducer(state = initialState, action: TodoAction): T {
|
||||||
if (handlers.hasOwnProperty(action.type)) {
|
if (isHandlerFunction(handlerOrMap)) {
|
||||||
return produce(state, draft => handlers[action.type](draft, action));
|
return produce(state, draft => handlerOrMap(draft, action));
|
||||||
|
} else if (handlerOrMap.hasOwnProperty(action.type)) {
|
||||||
|
return produce(state, draft => handlerOrMap[action.type](draft, action));
|
||||||
} else {
|
} else {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { createReducer } from './createReducer';
|
import { createReducer } from './createReducer';
|
||||||
import { Store, FilterTypes } from '../store';
|
import { Store, FilterTypes } from '../store';
|
||||||
import { combineReducers } from 'redux';
|
import { combineReducers } from 'redux';
|
||||||
|
import produce from 'immer';
|
||||||
|
|
||||||
let counter = 0;
|
let counter = 0;
|
||||||
|
|
||||||
@@ -22,12 +23,19 @@ export const reducer = combineReducers<Store>({
|
|||||||
complete(draft, action) {
|
complete(draft, action) {
|
||||||
draft[action.id].completed = !draft[action.id].completed;
|
draft[action.id].completed = !draft[action.id].completed;
|
||||||
return draft;
|
return draft;
|
||||||
|
},
|
||||||
|
|
||||||
|
clear(draft, action) {
|
||||||
|
Object.keys(draft).forEach(id => {
|
||||||
|
if (draft[id].completed) {
|
||||||
|
delete draft[id];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return draft;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
filter: createReducer<FilterTypes>('all', {
|
filter: createReducer<Store['filter']>('all', (draft, action) => {
|
||||||
filter(draft, action) {
|
return action.filter;
|
||||||
return action.filter;
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user