mirror of
https://github.com/microsoft/frontend-bootcamp.git
synced 2026-07-13 15:28:32 +08:00
checking in docs for github.io page
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
import React from 'react';
|
||||
import { Stack } from 'office-ui-fabric-react';
|
||||
import { TodoFooter } from './TodoFooter';
|
||||
import { TodoHeader } from './TodoHeader';
|
||||
import { TodoList } from './TodoList';
|
||||
|
||||
export const TodoApp = (props: {}) => (
|
||||
<Stack horizontalAlign="center">
|
||||
<Stack style={{ width: 400 }} gap={25}>
|
||||
<TodoHeader />
|
||||
<TodoList />
|
||||
<TodoFooter />
|
||||
</Stack>
|
||||
</Stack>
|
||||
);
|
||||
@@ -0,0 +1,39 @@
|
||||
import React from 'react';
|
||||
import { Text } from '@uifabric/experiments';
|
||||
import { Stack } from 'office-ui-fabric-react';
|
||||
import { Store } from '../store';
|
||||
import { DefaultButton } from 'office-ui-fabric-react';
|
||||
import { actionsWithService } from '../actions';
|
||||
import { connect } from 'react-redux';
|
||||
|
||||
// Redux Container
|
||||
export function mapStateToProps({ todos, filter }: Store) {
|
||||
return {
|
||||
todos,
|
||||
filter
|
||||
};
|
||||
}
|
||||
|
||||
export function mapDispatchToProps(dispatch: any) {
|
||||
return {
|
||||
clear: () => dispatch(actionsWithService.clear())
|
||||
};
|
||||
}
|
||||
|
||||
type TodoFooterProps = ReturnType<typeof mapStateToProps> & ReturnType<typeof mapDispatchToProps>;
|
||||
|
||||
export const TodoFooter = connect(
|
||||
mapStateToProps,
|
||||
mapDispatchToProps
|
||||
)((props: TodoFooterProps) => {
|
||||
const itemCount = Object.keys(props.todos).filter(id => !props.todos[id].completed).length;
|
||||
|
||||
return (
|
||||
<Stack horizontal horizontalAlign="space-between">
|
||||
<Text>
|
||||
{itemCount} item{itemCount > 1 ? 's' : ''} left
|
||||
</Text>
|
||||
<DefaultButton onClick={() => props.clear()}>Clear Completed</DefaultButton>
|
||||
</Stack>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,80 @@
|
||||
import React from 'react';
|
||||
import { Text } from '@uifabric/experiments';
|
||||
import { Pivot, PivotItem, TextField, Stack } from 'office-ui-fabric-react';
|
||||
import { FilterTypes, Store } from '../store';
|
||||
import { actionsWithService, actions } from '../actions';
|
||||
import { connect } from 'react-redux';
|
||||
|
||||
function mapStateToProps({ todos, filter }: Store) {
|
||||
return {
|
||||
todos,
|
||||
filter
|
||||
};
|
||||
}
|
||||
|
||||
function mapDispatchToProps(dispatch: any) {
|
||||
return {
|
||||
add: (label: string) => dispatch(actionsWithService.add(label)),
|
||||
remove: (id: string) => dispatch(actionsWithService.remove(id)),
|
||||
setFilter: (filter: FilterTypes) => dispatch(actions.setFilter(filter))
|
||||
};
|
||||
}
|
||||
|
||||
type TodoHeaderProps = ReturnType<typeof mapStateToProps> & ReturnType<typeof mapDispatchToProps>;
|
||||
|
||||
interface TodoHeaderState {
|
||||
labelInput: string;
|
||||
}
|
||||
|
||||
class TodoHeader extends React.Component<TodoHeaderProps, TodoHeaderState> {
|
||||
constructor(props: TodoHeaderProps) {
|
||||
super(props);
|
||||
this.state = { labelInput: undefined };
|
||||
}
|
||||
|
||||
onKeyPress = (evt: React.KeyboardEvent) => {
|
||||
if (evt.charCode === 13) {
|
||||
this.props.add(this.state.labelInput);
|
||||
this.setState({ labelInput: undefined });
|
||||
}
|
||||
};
|
||||
|
||||
onChange = (evt: React.FormEvent<HTMLInputElement>, newValue: string) => {
|
||||
this.setState({ labelInput: newValue });
|
||||
};
|
||||
|
||||
onFilter = (item: PivotItem) => {
|
||||
this.props.setFilter(item.props.headerText as FilterTypes);
|
||||
};
|
||||
|
||||
render() {
|
||||
return (
|
||||
<Stack>
|
||||
<Stack horizontal horizontalAlign="center">
|
||||
<Text variant="xxLarge">todos</Text>
|
||||
</Stack>
|
||||
|
||||
<TextField
|
||||
placeholder="What needs to be done?"
|
||||
value={this.state.labelInput}
|
||||
onChange={this.onChange}
|
||||
onKeyPress={this.onKeyPress}
|
||||
/>
|
||||
|
||||
<Pivot onLinkClick={this.onFilter}>
|
||||
<PivotItem headerText="all" />
|
||||
<PivotItem headerText="active" />
|
||||
<PivotItem headerText="completed" />
|
||||
</Pivot>
|
||||
</Stack>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Hook up the Redux state and dispatches
|
||||
const component = connect(
|
||||
mapStateToProps,
|
||||
mapDispatchToProps
|
||||
)(TodoHeader);
|
||||
|
||||
export { component as TodoHeader };
|
||||
@@ -0,0 +1,40 @@
|
||||
import React from 'react';
|
||||
import { Stack } from 'office-ui-fabric-react';
|
||||
import { TodoListItem } from './TodoListItem';
|
||||
import { Store } from '../store';
|
||||
import { connect } from 'react-redux';
|
||||
|
||||
function mapStateToProps({ todos, filter }: Store) {
|
||||
return {
|
||||
todos,
|
||||
filter
|
||||
};
|
||||
}
|
||||
|
||||
function mapDispatchToProps(dispatch: any) {}
|
||||
|
||||
type TodoListProps = ReturnType<typeof mapStateToProps> & ReturnType<typeof mapDispatchToProps>;
|
||||
|
||||
class TodoList extends React.Component<TodoListProps> {
|
||||
render() {
|
||||
const { filter, todos } = this.props;
|
||||
const filteredTodos = Object.keys(todos).filter(id => {
|
||||
return filter === 'all' || (filter === 'completed' && todos[id].completed) || (filter === 'active' && !todos[id].completed);
|
||||
});
|
||||
|
||||
return (
|
||||
<Stack gap={10}>
|
||||
{filteredTodos.map(id => (
|
||||
<TodoListItem key={id} id={id} />
|
||||
))}
|
||||
</Stack>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const component = connect(
|
||||
mapStateToProps,
|
||||
mapDispatchToProps
|
||||
)(TodoList);
|
||||
|
||||
export { component as TodoList };
|
||||
@@ -0,0 +1,104 @@
|
||||
import React from 'react';
|
||||
import { Stack, Checkbox, IconButton, TextField } from 'office-ui-fabric-react';
|
||||
import { mergeStyles } from '@uifabric/styling';
|
||||
import { Store } from '../store';
|
||||
import { actionsWithService } from '../actions';
|
||||
import { connect } from 'react-redux';
|
||||
|
||||
function mapStateToProps({ todos }: Store) {
|
||||
return {
|
||||
todos
|
||||
};
|
||||
}
|
||||
|
||||
function mapDispatchToProps(dispatch: any) {
|
||||
return {
|
||||
remove: (id: string) => dispatch(actionsWithService.remove(id)),
|
||||
complete: (id: string) => dispatch(actionsWithService.complete(id)),
|
||||
edit: (id: string, label: string) => dispatch(actionsWithService.edit(id, label))
|
||||
};
|
||||
}
|
||||
|
||||
type TodoListItemProps = { id: string } & ReturnType<typeof mapDispatchToProps> & ReturnType<typeof mapStateToProps>;
|
||||
|
||||
interface TodoListItemState {
|
||||
editing: boolean;
|
||||
editLabel: string;
|
||||
}
|
||||
|
||||
const className = mergeStyles({
|
||||
selectors: {
|
||||
'.clearButton': {
|
||||
visibility: 'hidden'
|
||||
},
|
||||
'&:hover .clearButton': {
|
||||
visibility: 'visible'
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
class TodoListItem extends React.Component<TodoListItemProps, TodoListItemState> {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
constructor(props: TodoListItemProps) {
|
||||
super(props);
|
||||
this.state = { editing: false, editLabel: undefined };
|
||||
}
|
||||
|
||||
onEdit = () => {
|
||||
const { todos, id } = this.props;
|
||||
const { label } = todos[id];
|
||||
|
||||
this.setState(prevState => ({
|
||||
editing: true,
|
||||
editLabel: prevState.editLabel || 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 { todos, id, complete, remove } = this.props;
|
||||
const item = todos[id];
|
||||
|
||||
return (
|
||||
<Stack horizontal className={className} 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 && <TextField value={this.state.editLabel} onChange={this.onChange} onKeyPress={this.onKeyDown} />}
|
||||
</Stack>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const component = connect(
|
||||
mapStateToProps,
|
||||
mapDispatchToProps
|
||||
)(TodoListItem);
|
||||
|
||||
export { component as TodoListItem };
|
||||
Reference in New Issue
Block a user