Skip to content

Commit

Permalink
refac: improve type definitions in PhoneInput component
Browse files Browse the repository at this point in the history
  • Loading branch information
omeralpi committed Feb 26, 2024
1 parent ffc5acb commit 236c3b1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 59 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ const FormSchema = z.object({
export default function Hero() {
const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
defaultValues: {
phone: "",
},
});

function onSubmit(data: z.infer<typeof FormSchema>) {
Expand All @@ -60,7 +63,8 @@ export default function Hero() {
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className="space-y-8 flex flex-col items-start">
className="space-y-8 flex flex-col items-start"
>
<FormField
control={form.control}
name="phone"
Expand Down
2 changes: 0 additions & 2 deletions app/(home)/sections/variants.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { PhoneInput } from "@/components/ui/phone-input";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { cn } from "@/lib/utils";
import React, { useState } from "react";
import {
Country,
Expand Down
52 changes: 25 additions & 27 deletions components/ui/phone-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,34 @@ import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover
import { cn } from "@/lib/utils";

type PhoneInputProps = Omit<React.InputHTMLAttributes<HTMLInputElement>, "onChange" | "value"> &
RPNInput.Props<typeof RPNInput.default> & {
Omit<RPNInput.Props<typeof RPNInput.default>, "onChange"> & {
onChange: (value: RPNInput.Value) => void;
value: RPNInput.Value;
};

const PhoneInput: React.ForwardRefExoticComponent<
Omit<PhoneInputProps, "onChange"> & {
onChange: (value: RPNInput.Value) => void;
}
> = React.forwardRef<React.ElementRef<typeof RPNInput.default>, PhoneInputProps>(
({ className, onChange, ...props }, ref) => (
<RPNInput.default
ref={ref}
className={cn("flex", className)}
flagComponent={FlagComponent}
countrySelectComponent={CountrySelect}
inputComponent={InputComponent}
/**
* Handles the onChange event.
*
* react-phone-number-input might trigger the onChange event as undefined
* when a valid phone number is not entered. To prevent this,
* the value is coerced to an empty string.
*
* @param {E164Number | undefined} value - The entered value
*/
onChange={(value) => onChange(value || "")}
{...props}
/>
),
);
const PhoneInput: React.ForwardRefExoticComponent<PhoneInputProps> = React.forwardRef<
React.ElementRef<typeof RPNInput.default>,
PhoneInputProps
>(({ className, onChange, ...props }, ref) => (
<RPNInput.default
ref={ref}
className={cn("flex", className)}
flagComponent={FlagComponent}
countrySelectComponent={CountrySelect}
inputComponent={InputComponent}
/**
* Handles the onChange event.
*
* react-phone-number-input might trigger the onChange event as undefined
* when a valid phone number is not entered. To prevent this,
* the value is coerced to an empty string.
*
* @param {E164Number | undefined} value - The entered value
*/
onChange={(value) => onChange(value || "")}
{...props}
/>
));
PhoneInput.displayName = "PhoneInput";

const InputComponent = React.forwardRef<HTMLInputElement, InputProps>(({ className, ...props }, ref) => (
Expand Down
54 changes: 25 additions & 29 deletions content/snippets/phone-input.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,34 @@ type PhoneInputProps = Omit<
React.InputHTMLAttributes<HTMLInputElement>,
"onChange" | "value"
> &
RPNInput.Props<typeof RPNInput.default> & {
Omit<RPNInput.Props<typeof RPNInput.default>, "onChange"> & {
onChange: (value: RPNInput.Value) => void;
value: RPNInput.Value;
};

const PhoneInput: React.ForwardRefExoticComponent<
Omit<PhoneInputProps, "onChange"> & {
onChange: (value: RPNInput.Value) => void;
}
> = React.forwardRef<
React.ElementRef<typeof RPNInput.default>,
PhoneInputProps
>(({ className, onChange, ...props }, ref) => (
<RPNInput.default
ref={ref}
placeholder="Enter a phone number"
className={cn("flex", className)}
flagComponent={FlagComponent}
countrySelectComponent={CountrySelect}
inputComponent={InputComponent}
/**
* Handles the onChange event.
*
* react-phone-number-input might trigger the onChange event as undefined
* when a valid phone number is not entered. To prevent this,
* the value is coerced to an empty string.
*
* @param {E164Number | undefined} value - The entered value
*/
onChange={(value) => onChange(value || "")}
{...props}
/>
));
const PhoneInput: React.ForwardRefExoticComponent<PhoneInputProps> =
React.forwardRef<React.ElementRef<typeof RPNInput.default>, PhoneInputProps>(
({ className, onChange, ...props }, ref) => (
<RPNInput.default
ref={ref}
className={cn("flex", className)}
flagComponent={FlagComponent}
countrySelectComponent={CountrySelect}
inputComponent={InputComponent}
/**
* Handles the onChange event.
*
* react-phone-number-input might trigger the onChange event as undefined
* when a valid phone number is not entered. To prevent this,
* the value is coerced to an empty string.
*
* @param {E164Number | undefined} value - The entered value
*/
onChange={(value) => onChange(value || "")}
{...props}
/>
),
);
PhoneInput.displayName = "PhoneInput";

const InputComponent = React.forwardRef<HTMLInputElement, InputProps>(
Expand Down

0 comments on commit 236c3b1

Please sign in to comment.