Created
March 8, 2024 16:26
-
-
Save Dadangdut33/7494d3161d368d5466e935008c24782e to your computer and use it in GitHub Desktop.
Mantine UI Phone Number Input with react-phone-number-input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Input as MantineInput, TextInput } from "@mantine/core"; | |
import { UseFormReturnType } from "@mantine/form"; | |
import { LegacyRef, forwardRef } from "react"; | |
import PhoneInput, { DefaultInputComponentProps, Value } from "react-phone-number-input"; | |
import "react-phone-number-input/style.css"; | |
// eslint-disable-next-line react/display-name | |
const CustomInput = forwardRef((inputProps: DefaultInputComponentProps, ref: LegacyRef<HTMLInputElement>) => { | |
const { error, ...rest } = inputProps; | |
return ( | |
<TextInput | |
name="phoneNumber" | |
placeholder="+62 xxxx" | |
// @ts-ignore | |
ref={ref} | |
{...rest} | |
error={error && error.length > 0 ? true : false} | |
required | |
/> | |
); | |
}); | |
const PhoneNumber = (props: { | |
value?: string | Value; | |
onChange?: (value: string) => void; | |
mantineUseForm?: UseFormReturnType<any>; | |
mantineUseFormField?: string; | |
}) => { | |
return ( | |
<MantineInput.Wrapper mt="md" label="Nomor Telepon" required> | |
{props.mantineUseForm ? ( | |
<PhoneInput | |
defaultCountry="ID" | |
inputComponent={CustomInput} | |
{...props.mantineUseForm.getInputProps(props.mantineUseFormField!)} | |
/> | |
) : ( | |
<PhoneInput value={props.value!} onChange={props.onChange!} defaultCountry="ID" inputComponent={CustomInput} /> | |
)} | |
<MantineInput.Error mt={5}> | |
{props.mantineUseForm ? props.mantineUseForm.errors[props.mantineUseFormField!] : ""} | |
</MantineInput.Error> | |
</MantineInput.Wrapper> | |
); | |
}; | |
export default PhoneNumber; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment