2022-05-22 10:59:53 +00:00
|
|
|
import { type FunctionComponent, type PropsWithChildren, useRef } from "react";
|
2022-05-24 21:13:08 +00:00
|
|
|
import { type PressHookProps, 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-24 21:13:08 +00:00
|
|
|
type Props = {
|
|
|
|
onDigitPressProps?: (digit: string) => PressHookProps;
|
|
|
|
onZeroPressProps?: PressHookProps;
|
|
|
|
};
|
|
|
|
|
|
|
|
const Keypad: FunctionComponent<PropsWithChildren<Props>> = ({ children, onDigitPressProps, onZeroPressProps }) => {
|
2021-08-08 11:22:34 +00:00
|
|
|
return (
|
|
|
|
<section>
|
|
|
|
<Row>
|
2022-05-24 21:13:08 +00:00
|
|
|
<Digit onPressProps={onDigitPressProps} digit="1" />
|
|
|
|
<Digit onPressProps={onDigitPressProps} digit="2">
|
2021-08-08 11:22:34 +00:00
|
|
|
<DigitLetters>ABC</DigitLetters>
|
|
|
|
</Digit>
|
2022-05-24 21:13:08 +00:00
|
|
|
<Digit onPressProps={onDigitPressProps} digit="3">
|
2021-08-08 11:22:34 +00:00
|
|
|
<DigitLetters>DEF</DigitLetters>
|
|
|
|
</Digit>
|
|
|
|
</Row>
|
|
|
|
<Row>
|
2022-05-24 21:13:08 +00:00
|
|
|
<Digit onPressProps={onDigitPressProps} digit="4">
|
2021-08-08 11:22:34 +00:00
|
|
|
<DigitLetters>GHI</DigitLetters>
|
|
|
|
</Digit>
|
2022-05-24 21:13:08 +00:00
|
|
|
<Digit onPressProps={onDigitPressProps} digit="5">
|
2021-08-08 11:22:34 +00:00
|
|
|
<DigitLetters>JKL</DigitLetters>
|
|
|
|
</Digit>
|
2022-05-24 21:13:08 +00:00
|
|
|
<Digit onPressProps={onDigitPressProps} digit="6">
|
2021-08-08 11:22:34 +00:00
|
|
|
<DigitLetters>MNO</DigitLetters>
|
|
|
|
</Digit>
|
|
|
|
</Row>
|
|
|
|
<Row>
|
2022-05-24 21:13:08 +00:00
|
|
|
<Digit onPressProps={onDigitPressProps} digit="7">
|
2021-08-08 11:22:34 +00:00
|
|
|
<DigitLetters>PQRS</DigitLetters>
|
|
|
|
</Digit>
|
2022-05-24 21:13:08 +00:00
|
|
|
<Digit onPressProps={onDigitPressProps} digit="8">
|
2021-08-08 11:22:34 +00:00
|
|
|
<DigitLetters>TUV</DigitLetters>
|
|
|
|
</Digit>
|
2022-05-24 21:13:08 +00:00
|
|
|
<Digit onPressProps={onDigitPressProps} digit="9">
|
2021-08-08 11:22:34 +00:00
|
|
|
<DigitLetters>WXYZ</DigitLetters>
|
|
|
|
</Digit>
|
|
|
|
</Row>
|
|
|
|
<Row>
|
2022-05-24 21:13:08 +00:00
|
|
|
<Digit onPressProps={onDigitPressProps} digit="*" />
|
|
|
|
<ZeroDigit onPressProps={onZeroPressProps} />
|
|
|
|
<Digit onPressProps={onDigitPressProps} 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-24 21:13:08 +00:00
|
|
|
onPressProps: Props["onDigitPressProps"];
|
2021-08-08 11:22:34 +00:00
|
|
|
};
|
|
|
|
|
2022-05-24 21:13:08 +00:00
|
|
|
const Digit: FunctionComponent<PropsWithChildren<DigitProps>> = (props) => {
|
2022-05-22 10:59:53 +00:00
|
|
|
const pressDigit = usePressDigit();
|
|
|
|
const onPressProps = {
|
|
|
|
onPress() {
|
|
|
|
// navigator.vibrate(1); // removed in webkit
|
2022-05-24 21:13:08 +00:00
|
|
|
pressDigit(props.digit);
|
2022-05-22 10:59:53 +00:00
|
|
|
},
|
|
|
|
};
|
2022-05-24 21:13:08 +00:00
|
|
|
const { pressProps } = usePress(props.onPressProps?.(props.digit) ?? onPressProps);
|
2021-08-08 11:22:34 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div {...pressProps} className="text-3xl cursor-pointer select-none">
|
2022-05-24 21:13:08 +00:00
|
|
|
{props.digit}
|
|
|
|
{props.children}
|
2021-08-08 11:22:34 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-05-24 21:13:08 +00:00
|
|
|
type ZeroDigitProps = {
|
|
|
|
onPressProps: Props["onZeroPressProps"];
|
|
|
|
};
|
|
|
|
|
|
|
|
const ZeroDigit: FunctionComponent<ZeroDigitProps> = (props) => {
|
2022-05-22 10:59:53 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
2022-05-24 21:13:08 +00:00
|
|
|
const { pressProps } = usePress(props.onPressProps ?? onPressProps);
|
2021-08-08 11:22:34 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div {...pressProps} className="text-3xl cursor-pointer select-none">
|
|
|
|
0 <DigitLetters>+</DigitLetters>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|