From 344f34d70d6652a78ba704612715f33fae5f308c Mon Sep 17 00:00:00 2001 From: Omer Alpi Date: Tue, 12 Mar 2024 08:52:54 +0300 Subject: [PATCH] fix: simplify onChange handler (#9) --- README.md | 16 ++++- app/(home)/sections/hero.tsx | 108 ++++++++++++++++--------------- app/(home)/sections/variants.tsx | 1 - components/ui/phone-input.tsx | 45 ++++++------- content/snippets/phone-input.mdx | 45 ++++++------- 5 files changed, 115 insertions(+), 100 deletions(-) diff --git a/README.md b/README.md index 304492e..08d0336 100644 --- a/README.md +++ b/README.md @@ -63,8 +63,7 @@ export default function Hero() {
+ className="space-y-8 flex flex-col items-start"> -
-

Shadcn Phone Input

-

- An implementation of a Phone Input component built on top of Shadcn UI's input component. -

-
- - Try it out - - - Github - + <> +
+
+

Shadcn Phone Input

+

+ An implementation of a Phone Input component built on top of Shadcn UI's input component. +

+
+ + Try it out + + + Github + +
-
-
-
-
- - - ( - - Phone Number - - - - Enter a phone number - - - )} - /> -
-                  {JSON.stringify(form.watch("phone"), null, 2)}
-                
- - - +
+
+
+
+ + ( + + Phone Number + + + + Enter a phone number + + + )} + /> +
+                    {JSON.stringify(form.watch("phone"), null, 2)}
+                  
+ + + +
-
- + + ); } diff --git a/app/(home)/sections/variants.tsx b/app/(home)/sections/variants.tsx index 243323a..888e21b 100644 --- a/app/(home)/sections/variants.tsx +++ b/app/(home)/sections/variants.tsx @@ -2,7 +2,6 @@ import { PhoneInput } from "@/components/ui/phone-input"; import React, { useState } from "react"; import { Country, - Value, formatPhoneNumber, formatPhoneNumberIntl, getCountryCallingCode, diff --git a/components/ui/phone-input.tsx b/components/ui/phone-input.tsx index 4e49c3b..f99ab94 100644 --- a/components/ui/phone-input.tsx +++ b/components/ui/phone-input.tsx @@ -22,33 +22,34 @@ import { cn } from "@/lib/utils"; type PhoneInputProps = Omit, "onChange" | "value"> & Omit, "onChange"> & { - onChange: (value: RPNInput.Value) => void; - value: RPNInput.Value; + onChange?: (value: RPNInput.Value) => void; }; const PhoneInput: React.ForwardRefExoticComponent = React.forwardRef< React.ElementRef, PhoneInputProps ->(({ className, onChange, ...props }, ref) => ( - onChange(value || "")} - {...props} - /> -)); +>(({ className, onChange, ...props }, ref) => { + return ( + onChange?.(value || "")} + {...props} + /> + ); +}); PhoneInput.displayName = "PhoneInput"; const InputComponent = React.forwardRef(({ className, ...props }, ref) => ( diff --git a/content/snippets/phone-input.mdx b/content/snippets/phone-input.mdx index aab77bc..f4cd82c 100644 --- a/content/snippets/phone-input.mdx +++ b/content/snippets/phone-input.mdx @@ -35,32 +35,33 @@ type PhoneInputProps = Omit< "onChange" | "value" > & Omit, "onChange"> & { - onChange: (value: RPNInput.Value) => void; - value: RPNInput.Value; + onChange?: (value: RPNInput.Value) => void; }; const PhoneInput: React.ForwardRefExoticComponent = React.forwardRef, PhoneInputProps>( - ({ className, onChange, ...props }, ref) => ( - onChange(value || "")} - {...props} - /> - ), + ({ className, onChange, ...props }, ref) => { + return ( + onChange?.(value || "")} + {...props} + /> + ); + }, ); PhoneInput.displayName = "PhoneInput";