shellphone.app/app/features/keypad/components/keypad.tsx

107 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-05-22 10:59:53 +00:00
import { type FunctionComponent, type PropsWithChildren, useRef } from "react";
2021-08-08 11:22:34 +00:00
import { usePress } from "@react-aria/interactions";
2022-05-22 10:59:53 +00:00
import { useLongPressDigit, usePressDigit } from "~/features/keypad/hooks/atoms";
2021-08-08 11:22:34 +00:00
2022-05-22 10:59:53 +00:00
const Keypad: FunctionComponent<PropsWithChildren<{}>> = ({ children }) => {
2021-08-08 11:22:34 +00:00
return (
<section>
<Row>
2022-05-22 10:59:53 +00:00
<Digit digit="1" />
<Digit digit="2">
2021-08-08 11:22:34 +00:00
<DigitLetters>ABC</DigitLetters>
</Digit>
2022-05-22 10:59:53 +00:00
<Digit digit="3">
2021-08-08 11:22:34 +00:00
<DigitLetters>DEF</DigitLetters>
</Digit>
</Row>
<Row>
2022-05-22 10:59:53 +00:00
<Digit digit="4">
2021-08-08 11:22:34 +00:00
<DigitLetters>GHI</DigitLetters>
</Digit>
2022-05-22 10:59:53 +00:00
<Digit digit="5">
2021-08-08 11:22:34 +00:00
<DigitLetters>JKL</DigitLetters>
</Digit>
2022-05-22 10:59:53 +00:00
<Digit digit="6">
2021-08-08 11:22:34 +00:00
<DigitLetters>MNO</DigitLetters>
</Digit>
</Row>
<Row>
2022-05-22 10:59:53 +00:00
<Digit digit="7">
2021-08-08 11:22:34 +00:00
<DigitLetters>PQRS</DigitLetters>
</Digit>
2022-05-22 10:59:53 +00:00
<Digit digit="8">
2021-08-08 11:22:34 +00:00
<DigitLetters>TUV</DigitLetters>
</Digit>
2022-05-22 10:59:53 +00:00
<Digit digit="9">
2021-08-08 11:22:34 +00:00
<DigitLetters>WXYZ</DigitLetters>
</Digit>
</Row>
<Row>
2022-05-22 10:59:53 +00:00
<Digit digit="*" />
<ZeroDigit />
<Digit digit="#" />
2021-08-08 11:22:34 +00:00
</Row>
{typeof children !== "undefined" ? <Row>{children}</Row> : null}
</section>
);
};
export default Keypad;
2022-05-14 10:22:06 +00:00
const Row: FunctionComponent<PropsWithChildren<{}>> = ({ children }) => (
2021-08-08 11:22:34 +00:00
<div className="grid grid-cols-3 p-4 my-0 mx-auto text-black">{children}</div>
);
2022-05-22 10:59:53 +00:00
const DigitLetters: FunctionComponent<PropsWithChildren<{}>> = ({ children }) => (
<div className="text-xs text-gray-600">{children}</div>
);
2021-08-08 11:22:34 +00:00
type DigitProps = {
digit: string;
};
2022-05-22 10:59:53 +00:00
const Digit: FunctionComponent<PropsWithChildren<DigitProps>> = ({ children, digit }) => {
const pressDigit = usePressDigit();
const onPressProps = {
onPress() {
// navigator.vibrate(1); // removed in webkit
pressDigit(digit);
},
};
const { pressProps } = usePress(onPressProps);
2021-08-08 11:22:34 +00:00
return (
<div {...pressProps} className="text-3xl cursor-pointer select-none">
{digit}
{children}
</div>
);
};
2022-05-22 10:59:53 +00:00
const ZeroDigit: FunctionComponent = () => {
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const pressDigit = usePressDigit();
const longPressDigit = useLongPressDigit();
const onPressProps = {
onPressStart() {
pressDigit("0");
timeoutRef.current = setTimeout(() => {
longPressDigit("+");
}, 750);
},
onPressEnd() {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
timeoutRef.current = null;
}
},
};
2021-08-08 11:22:34 +00:00
const { pressProps } = usePress(onPressProps);
return (
<div {...pressProps} className="text-3xl cursor-pointer select-none">
0 <DigitLetters>+</DigitLetters>
</div>
);
};