keypad page

This commit is contained in:
m5r
2022-05-22 12:59:53 +02:00
parent 90e3d2fb20
commit 3afc1d27cf
5 changed files with 187 additions and 151 deletions

View File

@ -0,0 +1,18 @@
import { IoCall } from "react-icons/io5";
import Keypad from "~/features/keypad/components/keypad";
export default function BlurredKeypad() {
return (
<div className="filter blur-sm select-none absolute top-0 w-full h-full z-0">
<section className="relative w-96 h-full flex flex-col justify-around mx-auto py-5 text-center">
<div className="h-16 text-3xl text-gray-700" />
<Keypad>
<button className="cursor-pointer select-none col-start-2 h-12 w-12 flex justify-center items-center mx-auto bg-green-800 rounded-full">
<IoCall className="w-6 h-6 text-white" />
</button>
</Keypad>
</section>
</div>
);
}

View File

@ -1,50 +1,45 @@
import type { FunctionComponent, PropsWithChildren } from "react";
import type { PressHookProps } from "@react-aria/interactions";
import { type FunctionComponent, type PropsWithChildren, useRef } from "react";
import { usePress } from "@react-aria/interactions";
import { useLongPressDigit, usePressDigit } from "~/features/keypad/hooks/atoms";
type Props = {
onDigitPressProps: (digit: string) => PressHookProps;
onZeroPressProps: PressHookProps;
};
const Keypad: FunctionComponent<PropsWithChildren<Props>> = ({ children, onDigitPressProps, onZeroPressProps }) => {
const Keypad: FunctionComponent<PropsWithChildren<{}>> = ({ children }) => {
return (
<section>
<Row>
<Digit onPressProps={onDigitPressProps} digit="1" />
<Digit onPressProps={onDigitPressProps} digit="2">
<Digit digit="1" />
<Digit digit="2">
<DigitLetters>ABC</DigitLetters>
</Digit>
<Digit onPressProps={onDigitPressProps} digit="3">
<Digit digit="3">
<DigitLetters>DEF</DigitLetters>
</Digit>
</Row>
<Row>
<Digit onPressProps={onDigitPressProps} digit="4">
<Digit digit="4">
<DigitLetters>GHI</DigitLetters>
</Digit>
<Digit onPressProps={onDigitPressProps} digit="5">
<Digit digit="5">
<DigitLetters>JKL</DigitLetters>
</Digit>
<Digit onPressProps={onDigitPressProps} digit="6">
<Digit digit="6">
<DigitLetters>MNO</DigitLetters>
</Digit>
</Row>
<Row>
<Digit onPressProps={onDigitPressProps} digit="7">
<Digit digit="7">
<DigitLetters>PQRS</DigitLetters>
</Digit>
<Digit onPressProps={onDigitPressProps} digit="8">
<Digit digit="8">
<DigitLetters>TUV</DigitLetters>
</Digit>
<Digit onPressProps={onDigitPressProps} digit="9">
<Digit digit="9">
<DigitLetters>WXYZ</DigitLetters>
</Digit>
</Row>
<Row>
<Digit onPressProps={onDigitPressProps} digit="*" />
<ZeroDigit onPressProps={onZeroPressProps} />
<Digit onPressProps={onDigitPressProps} digit="#" />
<Digit digit="*" />
<ZeroDigit />
<Digit digit="#" />
</Row>
{typeof children !== "undefined" ? <Row>{children}</Row> : null}
</section>
@ -57,15 +52,23 @@ const Row: FunctionComponent<PropsWithChildren<{}>> = ({ children }) => (
<div className="grid grid-cols-3 p-4 my-0 mx-auto text-black">{children}</div>
);
const DigitLetters: FunctionComponent<PropsWithChildren<{}>> = ({ children }) => <div className="text-xs text-gray-600">{children}</div>;
const DigitLetters: FunctionComponent<PropsWithChildren<{}>> = ({ children }) => (
<div className="text-xs text-gray-600">{children}</div>
);
type DigitProps = {
digit: string;
onPressProps: Props["onDigitPressProps"];
};
const Digit: FunctionComponent<PropsWithChildren<DigitProps>> = ({ children, digit, onPressProps }) => {
const { pressProps } = usePress(onPressProps(digit));
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);
return (
<div {...pressProps} className="text-3xl cursor-pointer select-none">
@ -75,11 +78,24 @@ const Digit: FunctionComponent<PropsWithChildren<DigitProps>> = ({ children, dig
);
};
type ZeroDigitProps = {
onPressProps: Props["onZeroPressProps"];
};
const ZeroDigit: FunctionComponent<PropsWithChildren<ZeroDigitProps>> = ({ onPressProps }) => {
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;
}
},
};
const { pressProps } = usePress(onPressProps);
return (

View File

@ -0,0 +1,33 @@
import { atom, useAtom } from "jotai";
const phoneNumberAtom = atom("");
const pressDigitAtom = atom(null, (get, set, digit: string) => {
if (get(phoneNumberAtom).length > 17) {
return;
}
if ("0123456789+#*".indexOf(digit) === -1) {
return;
}
set(phoneNumberAtom, (prevState) => prevState + digit);
});
const longPressDigitAtom = atom(null, (get, set, replaceWith: string) => {
if (get(phoneNumberAtom).length > 17) {
return;
}
set(phoneNumberAtom, (prevState) => prevState.slice(0, -1) + replaceWith);
});
const pressBackspaceAtom = atom(null, (get, set) => {
if (get(phoneNumberAtom).length === 0) {
return;
}
set(phoneNumberAtom, (prevState) => prevState.slice(0, -1));
});
export const usePhoneNumber = () => useAtom(phoneNumberAtom);
export const useRemoveDigit = () => useAtom(pressBackspaceAtom)[1];
export const usePressDigit = () => useAtom(pressDigitAtom)[1];
export const useLongPressDigit = () => useAtom(longPressDigitAtom)[1];

View File

@ -0,0 +1,34 @@
import { useRef } from "react";
import { usePress } from "@react-aria/interactions";
import { useRemoveDigit } from "./atoms";
export default function useOnBackspacePress() {
const removeDigit = useRemoveDigit();
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
const { pressProps: onBackspacePressProps } = usePress({
onPressStart() {
timeoutRef.current = setTimeout(() => {
removeDigit();
intervalRef.current = setInterval(removeDigit, 75);
}, 325);
},
onPressEnd() {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
timeoutRef.current = null;
}
if (intervalRef.current) {
clearInterval(intervalRef.current);
intervalRef.current = null;
return;
}
removeDigit();
},
});
return onBackspacePressProps;
}