transform 0 to + when long pressing

This commit is contained in:
m5r 2021-08-01 12:29:41 +08:00
parent a65d6e2bcc
commit 7d7c4cb495
2 changed files with 27 additions and 4 deletions

View File

@ -1,9 +1,11 @@
import type { BlitzPage } from "blitz"; import type { BlitzPage } from "blitz";
import { Routes } from "blitz"; import { Routes } from "blitz";
import type { FunctionComponent } from "react"; import type { FunctionComponent } from "react";
import { useRef } from "react";
import { atom, useAtom } from "jotai"; import { atom, useAtom } from "jotai";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faBackspace, faPhoneAlt as faPhone } from "@fortawesome/pro-solid-svg-icons"; import { faBackspace, faPhoneAlt as faPhone } from "@fortawesome/pro-solid-svg-icons";
import { usePress } from "@react-aria/interactions";
import Layout from "../../core/layouts/layout"; import Layout from "../../core/layouts/layout";
import useRequireOnboarding from "../../core/hooks/use-require-onboarding"; import useRequireOnboarding from "../../core/hooks/use-require-onboarding";
@ -70,12 +72,25 @@ const Keypad: BlitzPage = () => {
}; };
const ZeroDigit: FunctionComponent = () => { const ZeroDigit: FunctionComponent = () => {
// TODO: long press, convert + to 0 const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const pressDigit = useAtom(pressDigitAtom)[1]; const pressDigit = useAtom(pressDigitAtom)[1];
const onClick = () => pressDigit("0"); const longPressDigit = useAtom(longPressDigitAtom)[1];
const { pressProps } = usePress({
onPressStart() {
pressDigit("0");
timeoutRef.current = setTimeout(() => {
longPressDigit("+");
}, 750);
},
onPressEnd() {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
},
});
return ( return (
<div onClick={onClick} className="text-3xl cursor-pointer select-none"> <div {...pressProps} className="text-3xl cursor-pointer select-none">
0 <DigitLetters>+</DigitLetters> 0 <DigitLetters>+</DigitLetters>
</div> </div>
); );
@ -102,13 +117,20 @@ const DigitLetters: FunctionComponent = ({ children }) => (
); );
const phoneNumberAtom = atom(""); const phoneNumberAtom = atom("");
const pressDigitAtom = atom(null, (get, set, digit) => { const pressDigitAtom = atom(null, (get, set, digit: string) => {
if (get(phoneNumberAtom).length > 17) { if (get(phoneNumberAtom).length > 17) {
return; return;
} }
set(phoneNumberAtom, (prevState) => prevState + digit); 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) => { const pressBackspaceAtom = atom(null, (get, set) => {
if (get(phoneNumberAtom).length === 0) { if (get(phoneNumberAtom).length === 0) {
return; return;

View File

@ -43,6 +43,7 @@
"@heroicons/react": "1.0.3", "@heroicons/react": "1.0.3",
"@hookform/resolvers": "2.6.1", "@hookform/resolvers": "2.6.1",
"@prisma/client": "2.27.0", "@prisma/client": "2.27.0",
"@react-aria/interactions": "3.5.0",
"@tailwindcss/forms": "0.3.3", "@tailwindcss/forms": "0.3.3",
"@tailwindcss/typography": "0.4.1", "@tailwindcss/typography": "0.4.1",
"autoprefixer": "10.3.1", "autoprefixer": "10.3.1",