From 6caac5743bc504c4b46299b2efe312ac8e974576 Mon Sep 17 00:00:00 2001 From: AlexSserb Date: Mon, 11 Aug 2025 10:19:40 +0400 Subject: [PATCH] fix: memorizing country select --- src/components/ui/PhoneInput/PhoneInput.tsx | 43 ++++++++++++--------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/src/components/ui/PhoneInput/PhoneInput.tsx b/src/components/ui/PhoneInput/PhoneInput.tsx index 3296e10..11cf03c 100644 --- a/src/components/ui/PhoneInput/PhoneInput.tsx +++ b/src/components/ui/PhoneInput/PhoneInput.tsx @@ -1,14 +1,16 @@ "use client"; -import { useEffect, useRef, useState } from "react"; +import { useEffect, useMemo, useRef, useState } from "react"; import { IMaskInput } from "react-imask"; import { InputBase, type InputBaseProps, type PolymorphicComponentProps, } from "@mantine/core"; +import CountrySelect from "@/components/ui/PhoneInput/components/CountrySelect"; import { Country } from "@/components/ui/PhoneInput/types"; import getInitialDataFromValue from "@/components/ui/PhoneInput/utils/getInitialDataFromValue"; +import getPhoneMask from "@/components/ui/PhoneInput/utils/getPhoneMask"; type AdditionalProps = { onChange: (value: string | null) => void; @@ -60,28 +62,32 @@ const PhoneInput = ({ setDropdownWidth(inputRef.current?.offsetWidth); }, [inputRef.current?.offsetWidth]); + const countrySelect = useMemo( + () => ( + { + setCountry(country); + setPhoneMask(getPhoneMask(country.code), country); + setValue(""); + if (inputRef.current) { + inputRef.current.focus(); + } + }} + leftSectionWidth={leftSectionWidth} + inputWidth={dropdownWidth} + /> + ), + [country, leftSectionWidth, dropdownWidth, disabled, readOnly] + ); + return ( - // { - // setCountry(country); - // setPhoneMask(getPhoneMask(country.code), country); - // setValue(""); - // if (inputRef.current) { - // inputRef.current.focus(); - // } - // }} - // leftSectionWidth={leftSectionWidth} - // inputWidth={dropdownWidth} - // /> - } + leftSection={countrySelect} leftSectionWidth={leftSectionWidth} styles={{ input: { @@ -93,7 +99,6 @@ const PhoneInput = ({ "1px solid var(--mantine-color-default-border)", }, }} - inputMode={"numeric"} mask={mask} value={value} onAccept={onChange}