/
NumPad.tsx
186 lines (176 loc) · 4.87 KB
/
NumPad.tsx
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import React, { useState } from "react";
import {
Button,
Dialog,
DialogContent,
Fab,
Grid,
ButtonGroup,
DialogActions,
DialogTitle,
} from "@mui/material";
import ModeEditRoundedIcon from "@mui/icons-material/ModeEditRounded";
import BackspaceRoundedIcon from "@mui/icons-material/BackspaceRounded";
import useDeviceWidth from "#/components/lib/useDeviceWidth";
type NumPadProps = {
scanType: "reservation" | "guest";
onClose: (num: number[]) => void;
};
const NumPad: React.VFC<NumPadProps> = ({ scanType, onClose }) => {
const { largerThanSM, largerThanMD } = useDeviceWidth();
const [open, setOpen] = useState<boolean>(false);
const [id, setId] = useState<number[]>([]);
const openNumPad = () => {
setOpen(true);
};
const onNumClick = (n: number) => {
const newId = [...id, n];
setId(newId);
if (
(scanType === "reservation" && newId.length === 6) ||
(scanType === "guest" && newId.length === 9)
) {
setId([]);
setOpen(false);
onClose(newId);
}
};
const onDeleteNum = () => {
if (id.length > 0) {
setId(id.slice(0, id.length - 1));
}
};
const handleClose = (newId: number[]) => {
setOpen(false);
onClose(newId);
};
return (
<>
<Fab
variant={largerThanSM ? "extended" : "circular"}
onClick={openNumPad}
color="primary"
sx={{ whiteSpace: "nowrap" }}
>
<ModeEditRoundedIcon sx={{ mr: largerThanSM ? 1 : 0 }} />
{largerThanSM && "直接入力"}
</Fab>
<Dialog
open={open}
onClose={() => handleClose(id)}
fullScreen={!largerThanMD}
sx={{ transform: "translateZ(2px)" }}
>
<DialogTitle
sx={{ my: 0, px: 2, py: 2, textAlign: "center", overflowX: "scroll" }}
>
<ButtonGroup
variant="outlined"
sx={{
justifyContent: "center",
}}
>
<Button
disabled
sx={{
fontWeight: 800,
fontSize: "1.5rem",
"&.Mui-disabled": { color: "black" },
}}
>
{scanType === "reservation" ? "R" : "G"}
</Button>
{(scanType === "reservation"
? [0, 1, 2, 3, 4, 5]
: [0, 1, 2, 3, 4, 5, 6, 7, 8]
).map((i) => {
return (
<Button
disabled
sx={{
fontWeight: 800,
fontSize: "1.5rem",
"&.Mui-disabled": { color: "black" },
}}
key={i}
>
{i < id.length ? id[i] : "_"}
</Button>
);
})}
</ButtonGroup>
</DialogTitle>
<DialogContent
sx={{
p: 0,
textAlign: "center",
width: "100%",
display: "flex",
alignItems: "flex-end",
}}
>
<Grid
container
sx={{
width: 300,
margin: "1rem auto",
gap: ".5rem",
justifyContent: "space-between",
}}
>
{[1, 2, 3, 4, 5, 6, 7, 8, 9, 0].map((n) => {
return (
<Grid
item
key={String(n)}
sx={{ width: "min(13vh, 30%)", aspectRatio: "1 / 1" }}
>
<Button
variant="outlined"
onClick={() => onNumClick(n)}
sx={{
fontSize: "2rem",
fontWeight: 800,
width: "100%",
height: "100%",
borderRadius: "50%",
}}
>
{n}
</Button>
</Grid>
);
})}
<Grid item sx={{ width: "min(13vh, 30%)", aspectRatio: "1 / 1" }}>
<Button
variant="outlined"
onClick={onDeleteNum}
disabled={id.length <= 0}
sx={{
fontSize: "2rem",
fontWeight: 800,
width: "100%",
height: "100%",
borderRadius: "50%",
}}
>
<BackspaceRoundedIcon />
</Button>
</Grid>
</Grid>
</DialogContent>
<DialogActions sx={{ my: 0, py: 2, px: 2 }}>
<Button
onClick={() => setId([])}
color="error"
disabled={id.length === 0}
>
リセット
</Button>
<Button onClick={() => handleClose(id)}>閉じる</Button>
</DialogActions>
</Dialog>
</>
);
};
export default NumPad;