Skip to content

Commit 4f3c8c8

Browse files
Add confirmation when erasing all
1 parent 8a371d4 commit 4f3c8c8

File tree

2 files changed

+77
-11
lines changed

2 files changed

+77
-11
lines changed

src/components/drawing/DrawingBoard.tsx

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import AutoFixNormalIcon from '@mui/icons-material/AutoFixNormal';
66
import AbcIcon from '@mui/icons-material/Abc';
77
import UndoIcon from '@mui/icons-material/Undo';
88
import RedoIcon from '@mui/icons-material/Redo';
9-
import DeleteIcon from '@mui/icons-material/Delete';
109
import CircleIcon from '@mui/icons-material/Circle';
10+
import ErasingAllConfirmationButton from './ErasingAllConfirmationButton';
1111

1212
const DrawingBoard: React.FC = () => {
1313
const canvasRef = useRef<HTMLCanvasElement>(null);
1414
const [ canvas, setCanvas ] = useState<fabric.Canvas | null>(null);
15-
const [ mode, setMode ] = useState<fabric.PencilBrush | null>(null); // eslint-disable-line
15+
const [mode, setMode] = useState<fabric.PencilBrush | null>(null); // eslint-disable-line
1616
const historyRedo: fabric.Object[] = [];
1717

1818
const paletteColors = [ 'black', 'gray', 'green', 'yellow', 'orange', 'red', 'blue', 'purple' ];
@@ -129,7 +129,7 @@ const DrawingBoard: React.FC = () => {
129129
canvas.backgroundColor = backgroundColor;
130130
}
131131
};
132-
132+
133133
return (
134134
<Grid
135135
container
@@ -176,7 +176,7 @@ const DrawingBoard: React.FC = () => {
176176
<Grid
177177
container
178178
alignItems='center'
179-
justifyContent='space-around'
179+
// justifyContent='space-around'
180180
>
181181
{/* Tools: Palette */}
182182
<Grid item>
@@ -234,13 +234,7 @@ const DrawingBoard: React.FC = () => {
234234
>
235235
<RedoIcon/>
236236
</IconButton>
237-
<IconButton
238-
aria-label="Erase All"
239-
onClick={handleEraseAll}
240-
title="Erase All"
241-
>
242-
<DeleteIcon />
243-
</IconButton>
237+
<ErasingAllConfirmationButton handleEraseAll={handleEraseAll} />
244238
</Grid>
245239
</Grid>
246240
</Grid>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import React, { useEffect, useState } from 'react';
2+
import { Button, IconButton, Popover, Typography } from '@mui/material';
3+
import DeleteIcon from '@mui/icons-material/Delete';
4+
5+
type ErasingAllConfirmationButtonProps = {
6+
handleEraseAll: () => void;
7+
};
8+
9+
const ErasingAllConfirmationButton: React.FC<ErasingAllConfirmationButtonProps> = (props) => {
10+
11+
const { handleEraseAll } = props;
12+
13+
const [ anchorEl, setAnchorEl ] = useState<HTMLButtonElement | null>(null);
14+
const [ confirmed, setConfirmed ] = useState<boolean>(false);
15+
16+
const handleConfirm = (event: React.MouseEvent<HTMLButtonElement>) => {
17+
setAnchorEl(event.currentTarget);
18+
};
19+
20+
const handleConfirmed = () => {
21+
setConfirmed(true);
22+
setAnchorEl(null);
23+
};
24+
25+
useEffect(() => {
26+
if (confirmed) {
27+
handleEraseAll();
28+
setConfirmed(false);
29+
}
30+
}, [ confirmed ]);
31+
32+
const handleClose = () => {
33+
setAnchorEl(null);
34+
};
35+
36+
const open = Boolean(anchorEl);
37+
const id = open ? 'simple-popover' : undefined;
38+
39+
return (
40+
<div style={{ display: 'inline-block' }}>
41+
<IconButton onClick={handleConfirm} aria-describedby={id}>
42+
<DeleteIcon />
43+
</IconButton>
44+
<Popover
45+
id={id}
46+
open={open}
47+
anchorEl={anchorEl}
48+
onClose={handleClose}
49+
anchorOrigin={{
50+
vertical: 'top',
51+
horizontal: 'center',
52+
}}
53+
transformOrigin={{
54+
vertical: 'bottom',
55+
horizontal: 'right',
56+
}}
57+
>
58+
<Typography sx={{ p: 1 }}>
59+
<Button
60+
variant="contained"
61+
color="warning"
62+
onClick={handleConfirmed}
63+
>
64+
Confirm
65+
</Button>
66+
</Typography>
67+
</Popover>
68+
</div>
69+
);
70+
};
71+
72+
export default ErasingAllConfirmationButton;

0 commit comments

Comments
 (0)