Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat(ui): add phone-input

Marc Seitzbfa2c0a055b7
diff --git a/components/ui/command.tsx b/components/ui/command.tsx
new file mode 100644
index 00000000..b515722d
--- /dev/null
+++ b/components/ui/command.tsx
@@ -0,0 +1,154 @@
+import * as React from "react";
+import { type DialogProps } from "@radix-ui/react-dialog";
+import { Command as CommandPrimitive } from "cmdk";
+import { Search } from "lucide-react";
+
+import { cn } from "@/lib/utils";
+import { Dialog, DialogContent } from "@/components/ui/dialog";
+
+const Command = React.forwardRef<
+ React.ElementRef<typeof CommandPrimitive>,
+ React.ComponentPropsWithoutRef<typeof CommandPrimitive>
+>(({ className, ...props }, ref) => (
+ <CommandPrimitive
+ ref={ref}
+ className={cn(
+ "flex h-full w-full flex-col overflow-hidden rounded-md bg-popover text-popover-foreground",
+ className,
+ )}
+ {...props}
+ />
+));
+Command.displayName = CommandPrimitive.displayName;
+
+interface CommandDialogProps extends DialogProps {}
+
+const CommandDialog = ({ children, ...props }: CommandDialogProps) => {
+ return (
+ <Dialog {...props}>
+ <DialogContent className="overflow-hidden p-0 shadow-lg">
+ <Command className="[&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-muted-foreground [&_[cmdk-group]:not([hidden])_~[cmdk-group]]:pt-0 [&_[cmdk-group]]:px-2 [&_[cmdk-input-wrapper]_svg]:h-5 [&_[cmdk-input-wrapper]_svg]:w-5 [&_[cmdk-input]]:h-12 [&_[cmdk-item]]:px-2 [&_[cmdk-item]]:py-3 [&_[cmdk-item]_svg]:h-5 [&_[cmdk-item]_svg]:w-5">
+ {children}
+ </Command>
+ </DialogContent>
+ </Dialog>
+ );
+};
+
+const CommandInput = React.forwardRef<
+ React.ElementRef<typeof CommandPrimitive.Input>,
+ React.ComponentPropsWithoutRef<typeof CommandPrimitive.Input>
+>(({ className, ...props }, ref) => (
+ <div className="flex items-center border-b px-3" cmdk-input-wrapper="">
+ <Search className="mr-2 h-4 w-4 shrink-0 opacity-50" />
+ <CommandPrimitive.Input
+ ref={ref}
+ className={cn(
+ "flex h-11 w-full rounded-md bg-transparent py-3 text-sm outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed disabled:opacity-50",
+ className,
+ )}
+ {...props}
+ />
+ </div>
+));
+
+CommandInput.displayName = CommandPrimitive.Input.displayName;
+
+const CommandList = React.forwardRef<
+ React.ElementRef<typeof CommandPrimitive.List>,
+ React.ComponentPropsWithoutRef<typeof CommandPrimitive.List>
+>(({ className, ...props }, ref) => (
+ <CommandPrimitive.List
+ ref={ref}
+ className={cn("max-h-[300px] overflow-y-auto overflow-x-hidden", className)}
+ {...props}
+ />
+));
+
+CommandList.displayName = CommandPrimitive.List.displayName;
+
+const CommandEmpty = React.forwardRef<
+ React.ElementRef<typeof CommandPrimitive.Empty>,
+ React.ComponentPropsWithoutRef<typeof CommandPrimitive.Empty>
+>((props, ref) => (
+ <CommandPrimitive.Empty
+ ref={ref}
+ className="py-6 text-center text-sm"
+ {...props}
+ />
+));
+
+CommandEmpty.displayName = CommandPrimitive.Empty.displayName;
+
+const CommandGroup = React.forwardRef<
+ React.ElementRef<typeof CommandPrimitive.Group>,
+ React.ComponentPropsWithoutRef<typeof CommandPrimitive.Group>
+>(({ className, ...props }, ref) => (
+ <CommandPrimitive.Group
+ ref={ref}
+ className={cn(
+ "overflow-hidden p-1 text-foreground [&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:py-1.5 [&_[cmdk-group-heading]]:text-xs [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-muted-foreground",
+ className,
+ )}
+ {...props}
+ />
+));
+
+CommandGroup.displayName = CommandPrimitive.Group.displayName;
+
+const CommandSeparator = React.forwardRef<
+ React.ElementRef<typeof CommandPrimitive.Separator>,
+ React.ComponentPropsWithoutRef<typeof CommandPrimitive.Separator>
+>(({ className, ...props }, ref) => (
+ <CommandPrimitive.Separator
+ ref={ref}
+ className={cn("-mx-1 h-px bg-border", className)}
+ {...props}
+ />
+));
+CommandSeparator.displayName = CommandPrimitive.Separator.displayName;
+
+const CommandItem = React.forwardRef<
+ React.ElementRef<typeof CommandPrimitive.Item>,
+ React.ComponentPropsWithoutRef<typeof CommandPrimitive.Item>
+>(({ className, ...props }, ref) => (
+ <CommandPrimitive.Item
+ ref={ref}
+ className={cn(
+ // NOTE: removed these classNames because it broke the UI "data-[disabled]:pointer-events-none data-[disabled]:opacity-50"
+ "relative flex cursor-default select-none items-center rounded-sm px-2 py-1.5 text-sm outline-none aria-selected:bg-accent aria-selected:text-accent-foreground ",
+ className,
+ )}
+ {...props}
+ />
+));
+
+CommandItem.displayName = CommandPrimitive.Item.displayName;
+
+const CommandShortcut = ({
+ className,
+ ...props
+}: React.HTMLAttributes<HTMLSpanElement>) => {
+ return (
+ <span
+ className={cn(
+ "ml-auto text-xs tracking-widest text-muted-foreground",
+ className,
+ )}
+ {...props}
+ />
+ );
+};
+CommandShortcut.displayName = "CommandShortcut";
+
+export {
+ Command,
+ CommandDialog,
+ CommandInput,
+ CommandList,
+ CommandEmpty,
+ CommandGroup,
+ CommandItem,
+ CommandShortcut,
+ CommandSeparator,
+};
diff --git a/components/ui/phone-input.tsx b/components/ui/phone-input.tsx
new file mode 100644
index 00000000..6d3b7f75
--- /dev/null
+++ b/components/ui/phone-input.tsx
@@ -0,0 +1,167 @@
+import { CheckIcon, ChevronsUpDown } from "lucide-react";
+
+import * as React from "react";
+
+import * as RPNInput from "react-phone-number-input";
+
+import flags from "react-phone-number-input/flags";
+
+import { Button } from "@/components/ui/button";
+import {
+ Command,
+ CommandEmpty,
+ CommandGroup,
+ CommandInput,
+ CommandItem,
+ CommandList,
+} from "@/components/ui/command";
+import { Input, InputProps } from "@/components/ui/input";
+import {
+ Popover,
+ PopoverContent,
+ PopoverTrigger,
+} from "@/components/ui/popover";
+
+import { cn } from "@/lib/utils";
+import { ScrollArea } from "./scroll-area";
+
+type PhoneInputProps = Omit<
+ React.InputHTMLAttributes<HTMLInputElement>,
+ "onChange" | "value"
+> &
+ Omit<RPNInput.Props<typeof RPNInput.default>, "onChange"> & {
+ onChange?: (value: RPNInput.Value) => void;
+ };
+
+const PhoneInput: React.ForwardRefExoticComponent<PhoneInputProps> =
+ React.forwardRef<React.ElementRef<typeof RPNInput.default>, PhoneInputProps>(
+ ({ className, onChange, ...props }, ref) => {
+ return (
+ <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) => (
+ <Input
+ className={cn("rounded-e-lg rounded-s-none", className)}
+ {...props}
+ ref={ref}
+ />
+ ),
+);
+InputComponent.displayName = "InputComponent";
+
+type CountrySelectOption = { label: string; value: RPNInput.Country };
+
+type CountrySelectProps = {
+ disabled?: boolean;
+ value: RPNInput.Country;
+ onChange: (value: RPNInput.Country) => void;
+ options: CountrySelectOption[];
+};
+
+const CountrySelect = ({
+ disabled,
+ value,
+ onChange,
+ options,
+}: CountrySelectProps) => {
+ const handleSelect = React.useCallback(
+ (country: RPNInput.Country) => {
+ onChange(country);
+ },
+ [onChange],
+ );
+
+ return (
+ <Popover>
+ <PopoverTrigger asChild>
+ <Button
+ type="button"
+ variant={"outline"}
+ className={cn("flex gap-1 rounded-e-none rounded-s-lg px-3")}
+ disabled={disabled}
+ >
+ <FlagComponent country={value} countryName={value} />
+ <ChevronsUpDown
+ className={cn(
+ "-mr-2 h-4 w-4 opacity-50",
+ disabled ? "hidden" : "opacity-100",
+ )}
+ />
+ </Button>
+ </PopoverTrigger>
+ <PopoverContent className="w-[300px] p-0">
+ <Command>
+ <CommandList>
+ <ScrollArea className="h-72">
+ <CommandInput placeholder="Search country..." />
+ <CommandEmpty>No country found.</CommandEmpty>
+ <CommandGroup>
+ {options
+ .filter((x) => x.value)
+ .map((option) => (
+ <CommandItem
+ className="gap-2"
+ key={option.value}
+ onSelect={() => handleSelect(option.value)}
+ >
+ <FlagComponent
+ country={option.value}
+ countryName={option.label}
+ />
+ <span className="flex-1 text-sm">{option.label}</span>
+ {option.value && (
+ <span className="text-foreground/50 text-sm">
+ {`+${RPNInput.getCountryCallingCode(option.value)}`}
+ </span>
+ )}
+ <CheckIcon
+ className={cn(
+ "ml-auto h-4 w-4",
+ option.value === value ? "opacity-100" : "opacity-0",
+ )}
+ />
+ </CommandItem>
+ ))}
+ </CommandGroup>
+ </ScrollArea>
+ </CommandList>
+ </Command>
+ </PopoverContent>
+ </Popover>
+ );
+};
+
+const FlagComponent = ({ country, countryName }: RPNInput.FlagProps) => {
+ const Flag = flags[country];
+
+ return (
+ <span className="bg-foreground/20 flex h-4 w-6 overflow-hidden rounded-sm">
+ {Flag && <Flag title={countryName} />}
+ </span>
+ );
+};
+FlagComponent.displayName = "FlagComponent";
+
+export { PhoneInput };
diff --git a/package-lock.json b/package-lock.json
index 7de33466..4ab0c377 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -60,6 +60,7 @@
"bright": "^0.8.4",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
+ "cmdk": "^1.0.0",
"eslint": "^8.57.0",
"eslint-config-next": "^14.2.3",
"eventsource-parser": "^1.1.2",
@@ -92,6 +93,7 @@
"react-intersection-observer": "^9.10.1",
"react-notion-x": "^6.16.0",
"react-pdf": "^8.0.0",
+ "react-phone-number-input": "^3.4.1",
"react-textarea-autosize": "^8.5.3",
"rehype-autolink-headings": "^7.1.0",
"rehype-pretty-code": "^0.13.1",
@@ -6606,6 +6608,11 @@
"node": ">=6"
}
},
+ "node_modules/classnames": {
+ "version": "2.5.1",
+ "resolved": "https://registry.npmjs.org/classnames/-/classnames-2.5.1.tgz",
+ "integrity": "sha512-saHYOzhIQs6wy2sVxTM6bUDsQO4F50V9RQ22qBpEdCW+I+/Wmke2HOl6lS6dTpdxVhb88/I6+Hs+438c3lfUow=="
+ },
"node_modules/clean-stack": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/clean-stack/-/clean-stack-4.2.0.tgz",
@@ -6685,6 +6692,19 @@
"node": ">=6"
}
},
+ "node_modules/cmdk": {
+ "version": "1.0.0",
+ "resolved": "https://registry.npmjs.org/cmdk/-/cmdk-1.0.0.tgz",
+ "integrity": "sha512-gDzVf0a09TvoJ5jnuPvygTB77+XdOSwEmJ88L6XPFPlv7T3RxbP9jgenfylrAMD0+Le1aO0nVjQUzl2g+vjz5Q==",
+ "dependencies": {
+ "@radix-ui/react-dialog": "1.0.5",
+ "@radix-ui/react-primitive": "1.0.3"
+ },
+ "peerDependencies": {
+ "react": "^18.0.0",
+ "react-dom": "^18.0.0"
+ }
+ },
"node_modules/code-red": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/code-red/-/code-red-1.0.4.tgz",
@@ -6793,6 +6813,11 @@
"node": ">= 0.10"
}
},
+ "node_modules/country-flag-icons": {
+ "version": "1.5.11",
+ "resolved": "https://registry.npmjs.org/country-flag-icons/-/country-flag-icons-1.5.11.tgz",
+ "integrity": "sha512-B+mvFywunkRJs270k7kCBjhogvIA0uNn6GAXv6m2cPn3rrwqZzZVr2gBWcz+Cz7OGVWlcbERlYRIX0S6OGr8Bw=="
+ },
"node_modules/cronstrue": {
"version": "2.44.0",
"resolved": "https://registry.npmjs.org/cronstrue/-/cronstrue-2.44.0.tgz",
@@ -9350,6 +9375,14 @@
"fast-loops": "^1.1.3"
}
},
+ "node_modules/input-format": {
+ "version": "0.3.10",
+ "resolved": "https://registry.npmjs.org/input-format/-/input-format-0.3.10.tgz",
+ "integrity": "sha512-5cFv/kOZD7Ch0viprVkuYPDkAU7HBZYBx8QrIpQ6yXUWbAQ0+RQ8IIojDJOf/RO6FDJLL099HDSK2KoVZ2zevg==",
+ "dependencies": {
+ "prop-types": "^15.8.1"
+ }
+ },
"node_modules/internal-slot": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/internal-slot/-/internal-slot-1.0.5.tgz",
@@ -10149,6 +10182,11 @@
"node": ">= 0.8.0"
}
},
+ "node_modules/libphonenumber-js": {
+ "version": "1.10.61",
+ "resolved": "https://registry.npmjs.org/libphonenumber-js/-/libphonenumber-js-1.10.61.tgz",
+ "integrity": "sha512-TsQsyzDttDvvzWNkbp/i0fVbzTGJIG0mUu/uNalIaRQEYeJxVQ/FPg+EJgSqfSXezREjM0V3RZ8cLVsKYhhw0Q=="
+ },
"node_modules/lilconfig": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/lilconfig/-/lilconfig-2.1.0.tgz",
@@ -14917,6 +14955,22 @@
}
}
},
+ "node_modules/react-phone-number-input": {
+ "version": "3.4.1",
+ "resolved": "https://registry.npmjs.org/react-phone-number-input/-/react-phone-number-input-3.4.1.tgz",
+ "integrity": "sha512-guuenZqU/DYvDBFzFdTrppC4rs+q5ybTFHrxEo9VGvX0pPLWM4ZXlRa0llT7LRAvfxX8RjQNnQkkCiTHTzhLZA==",
+ "dependencies": {
+ "classnames": "^2.5.1",
+ "country-flag-icons": "^1.5.11",
+ "input-format": "^0.3.10",
+ "libphonenumber-js": "^1.10.61",
+ "prop-types": "^15.8.1"
+ },
+ "peerDependencies": {
+ "react": ">=16.8",
+ "react-dom": ">=16.8"
+ }
+ },
"node_modules/react-remove-scroll": {
"version": "2.5.5",
"resolved": "https://registry.npmjs.org/react-remove-scroll/-/react-remove-scroll-2.5.5.tgz",
diff --git a/package.json b/package.json
index f26541d9..bf5c31f3 100644
--- a/package.json
+++ b/package.json
@@ -68,6 +68,7 @@
"bright": "^0.8.4",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
+ "cmdk": "^1.0.0",
"eslint": "^8.57.0",
"eslint-config-next": "^14.2.3",
"eventsource-parser": "^1.1.2",
@@ -100,6 +101,7 @@
"react-intersection-observer": "^9.10.1",
"react-notion-x": "^6.16.0",
"react-pdf": "^8.0.0",
+ "react-phone-number-input": "^3.4.1",
"react-textarea-autosize": "^8.5.3",
"rehype-autolink-headings": "^7.1.0",
"rehype-pretty-code": "^0.13.1",