Phone
nordaun-ui/phoneFreeBlock
A simple but clean phone input with a region selection so you dont have to worry about messing up.
Live preview
live · rendered by the registry
Rendered by @nordaun/ui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://ui.nordaun.com/r/phone.jsonSource
1"use client";23import parsePhoneNumberFromString, {4 CountryCode,5 E164Number,6 getCountries,7 getCountryCallingCode,8} from "libphonenumber-js";9import * as React from "react";1011import {12 Command,13 CommandEmpty,14 CommandGroup,15 CommandInput,16 CommandItem,17 CommandList,18} from "@/components/ui/command";19import { Flag } from "@/components/ui/flag";20import {21 InputGroup,22 InputGroupAddon,23 InputGroupInput,24 InputGroupText,25} from "@/components/ui/input-group";26import {27 Popover,28 PopoverContent,29 PopoverTrigger,30} from "@/components/ui/popover";31import { countryNames } from "@/lib/geo";32import { cn } from "@/lib/utils";3334const codes: CountryCode[] = getCountries();3536type PhoneProps = React.ComponentProps<"input"> & {37 value?: string;38 searchPlaceholder: string;39 emptyPlaceholder: string;40};4142function Phone({43 value,44 onChange,45 placeholder,46 searchPlaceholder,47 emptyPlaceholder,48 ...props49}: PhoneProps) {50 const countries = React.useMemo(51 () =>52 [...codes].sort((a, b) => {53 return countryNames[a].localeCompare(countryNames[b]);54 }),55 [],56 );5758 const [country, setCountry] = React.useState<CountryCode>("US");59 const [number, setNumber] = React.useState<string>("");60 const [open, setOpen] = React.useState(false);6162 const inputRef = React.useRef<HTMLInputElement>(null);6364 const handleCountryChange = (newCountry: CountryCode) => {65 setCountry(newCountry);66 setOpen(false);67 const fullNumber = parsePhoneNumberFromString(number, newCountry);68 triggerOnChange(fullNumber?.number);69 };7071 const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {72 const filteredValue = e.target.value.replace(/[^0-9\-() ]/g, "");73 setNumber(filteredValue);74 const fullNumber = parsePhoneNumberFromString(filteredValue, country);75 triggerOnChange(fullNumber?.number);76 };7778 const triggerOnChange = (newNumber: E164Number | undefined) => {79 if (!onChange || !inputRef.current) return;80 const syntheticEvent = {81 target: {82 ...inputRef.current,83 value: newNumber?.toString(),84 },85 currentTarget: inputRef.current,86 } as React.ChangeEvent<HTMLInputElement>;87 onChange(syntheticEvent);88 };8990 const { name, className, ...groupProps } = props;9192 return (93 <>94 <InputGroup className={cn(className)}>95 <InputGroupAddon96 align="inline-start"97// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from @nordaun/ui
All 8 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Audioaudio | @nordaun/ui | Blocks | Free | 1 dep | |
| Filefile | @nordaun/ui | Blocks | Free | 1 dep · 3 files | |
| Flagflag | @nordaun/ui | Blocks | Free | no deps · 2 files | |
| Loadingloading | @nordaun/ui | Blocks | Free | no deps | |
| Mapmap | @nordaun/ui | Blocks | Free | 2 deps · 2 files | |
| Passwordpassword | @nordaun/ui | Blocks | Free | 1 dep | |
| Videovideo | @nordaun/ui | Blocks | Free | 1 dep · 2 files |
