-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui): implement list controls - items left and clear completed fu…
…nctionality
- Loading branch information
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React, { useMemo, useContext } from 'react'; | ||
import { TodosContext } from "../context/todos_context"; | ||
|
||
const TodoListControls = function (props) { | ||
const { todos, clearCompleted } = useContext(TodosContext); | ||
|
||
const itemsLeft = useMemo(() => todos.reduce((p, c) => p + (c.completed ? 0 : 1), 0), [ | ||
todos | ||
]); | ||
|
||
const itemsCompleted = useMemo(() => todos.length - itemsLeft, [ | ||
todos, itemsLeft | ||
]); | ||
|
||
return ( | ||
<footer className="footer"> | ||
{/* This should be `0 items left` by default */} | ||
<span className="todo-count"><strong>{itemsLeft}</strong> {itemsLeft != 1 ? "items" : "item"} left</span> | ||
{/* Remove this if you don't implement routing */} | ||
<ul className="filters"> | ||
<li> | ||
<a className="selected" href="#/">All</a> | ||
</li> | ||
<li> | ||
<a href="#/active">Active</a> | ||
</li> | ||
<li> | ||
<a href="#/completed">Completed</a> | ||
</li> | ||
</ul> | ||
{/* Hidden if no completed items are left ↓ */} | ||
{itemsCompleted > 0 && | ||
<button className="clear-completed" onClick={clearCompleted}>Clear completed</button> | ||
} | ||
</footer> | ||
); | ||
} | ||
|
||
export default TodoListControls; |