/
ToggleButton.jsx
42 lines (39 loc) · 1.04 KB
/
ToggleButton.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import styled from "styled-components";
import { useContext } from "react";
import AppContext from "./AppContext";
const Container = styled.div`
width: 60px;
display: flex;
align-items: center;
justify-content: center;
background: white;
> button {
font-size: 24px;
font-weight: bold;
border: 0;
background: none;
color: ${(props) => (props.allChecked ? "inherit" : "#e6e6e6")};
display: ${(props) => (props.visible ? "block" : "none")};
}
`;
const ToggleButton = () => {
console.log("render <ToggleButton />");
const { todos, setTodos } = useContext(AppContext);
const allChecked = todos.findIndex((todo) => !todo.checked) == -1;
return (
<Container visible={todos.length > 0} allChecked={allChecked}>
<button
onClick={() => {
const updatedTodos = todos.map((todo) => ({
...todo,
checked: !allChecked,
}));
setTodos([...updatedTodos]);
}}
>
∨
</button>
</Container>
);
};
export default ToggleButton;