import { type FunctionComponent, type PropsWithChildren, useRef } from "react"; import { type PressHookProps, usePress } from "@react-aria/interactions"; import { useLongPressDigit, usePressDigit } from "~/features/keypad/hooks/atoms"; type Props = { onDigitPressProps?: (digit: string) => PressHookProps; onZeroPressProps?: PressHookProps; }; const Keypad: FunctionComponent> = ({ children, onDigitPressProps, onZeroPressProps }) => { return (
ABC DEF GHI JKL MNO PQRS TUV WXYZ {typeof children !== "undefined" ? {children} : null}
); }; export default Keypad; const Row: FunctionComponent> = ({ children }) => (
{children}
); const DigitLetters: FunctionComponent> = ({ children }) => (
{children}
); type DigitProps = { digit: string; onPressProps: Props["onDigitPressProps"]; }; const Digit: FunctionComponent> = (props) => { const pressDigit = usePressDigit(); const onPressProps = { onPress() { // navigator.vibrate(1); // removed in webkit pressDigit(props.digit); }, }; const { pressProps } = usePress(props.onPressProps?.(props.digit) ?? onPressProps); return (
{props.digit} {props.children}
); }; type ZeroDigitProps = { onPressProps: Props["onZeroPressProps"]; }; const ZeroDigit: FunctionComponent = (props) => { const timeoutRef = useRef | 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; } }, }; const { pressProps } = usePress(props.onPressProps ?? onPressProps); return (
0 +
); };