fix: memorizing country select
This commit is contained in:
@ -1,14 +1,16 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useEffect, useRef, useState } from "react";
|
import { useEffect, useMemo, useRef, useState } from "react";
|
||||||
import { IMaskInput } from "react-imask";
|
import { IMaskInput } from "react-imask";
|
||||||
import {
|
import {
|
||||||
InputBase,
|
InputBase,
|
||||||
type InputBaseProps,
|
type InputBaseProps,
|
||||||
type PolymorphicComponentProps,
|
type PolymorphicComponentProps,
|
||||||
} from "@mantine/core";
|
} from "@mantine/core";
|
||||||
|
import CountrySelect from "@/components/ui/PhoneInput/components/CountrySelect";
|
||||||
import { Country } from "@/components/ui/PhoneInput/types";
|
import { Country } from "@/components/ui/PhoneInput/types";
|
||||||
import getInitialDataFromValue from "@/components/ui/PhoneInput/utils/getInitialDataFromValue";
|
import getInitialDataFromValue from "@/components/ui/PhoneInput/utils/getInitialDataFromValue";
|
||||||
|
import getPhoneMask from "@/components/ui/PhoneInput/utils/getPhoneMask";
|
||||||
|
|
||||||
type AdditionalProps = {
|
type AdditionalProps = {
|
||||||
onChange: (value: string | null) => void;
|
onChange: (value: string | null) => void;
|
||||||
@ -60,28 +62,32 @@ const PhoneInput = ({
|
|||||||
setDropdownWidth(inputRef.current?.offsetWidth);
|
setDropdownWidth(inputRef.current?.offsetWidth);
|
||||||
}, [inputRef.current?.offsetWidth]);
|
}, [inputRef.current?.offsetWidth]);
|
||||||
|
|
||||||
|
const countrySelect = useMemo(
|
||||||
|
() => (
|
||||||
|
<CountrySelect
|
||||||
|
disabled={disabled || readOnly}
|
||||||
|
country={country}
|
||||||
|
setCountry={country => {
|
||||||
|
setCountry(country);
|
||||||
|
setPhoneMask(getPhoneMask(country.code), country);
|
||||||
|
setValue("");
|
||||||
|
if (inputRef.current) {
|
||||||
|
inputRef.current.focus();
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
leftSectionWidth={leftSectionWidth}
|
||||||
|
inputWidth={dropdownWidth}
|
||||||
|
/>
|
||||||
|
),
|
||||||
|
[country, leftSectionWidth, dropdownWidth, disabled, readOnly]
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<InputBase
|
<InputBase
|
||||||
{...props}
|
{...props}
|
||||||
component={IMaskInput}
|
component={IMaskInput}
|
||||||
inputRef={inputRef}
|
inputRef={inputRef}
|
||||||
leftSection={
|
leftSection={countrySelect}
|
||||||
<></>
|
|
||||||
// <CountrySelect
|
|
||||||
// disabled={disabled || readOnly}
|
|
||||||
// country={country}
|
|
||||||
// setCountry={country => {
|
|
||||||
// setCountry(country);
|
|
||||||
// setPhoneMask(getPhoneMask(country.code), country);
|
|
||||||
// setValue("");
|
|
||||||
// if (inputRef.current) {
|
|
||||||
// inputRef.current.focus();
|
|
||||||
// }
|
|
||||||
// }}
|
|
||||||
// leftSectionWidth={leftSectionWidth}
|
|
||||||
// inputWidth={dropdownWidth}
|
|
||||||
// />
|
|
||||||
}
|
|
||||||
leftSectionWidth={leftSectionWidth}
|
leftSectionWidth={leftSectionWidth}
|
||||||
styles={{
|
styles={{
|
||||||
input: {
|
input: {
|
||||||
@ -93,7 +99,6 @@ const PhoneInput = ({
|
|||||||
"1px solid var(--mantine-color-default-border)",
|
"1px solid var(--mantine-color-default-border)",
|
||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
inputMode={"numeric"}
|
|
||||||
mask={mask}
|
mask={mask}
|
||||||
value={value}
|
value={value}
|
||||||
onAccept={onChange}
|
onAccept={onChange}
|
||||||
|
|||||||
Reference in New Issue
Block a user