Skip to content

[New Component]: PhoneInput #6

Closed
@junwen-k

Description

@junwen-k

Phone Input

Allows user to enter phone input using E.164 format.

Visual References

image

Description

I've built this component before using react-phone-number-input, however it does not fully support controlled behavior. This is taken from my previous code:

export const PhoneInputMobile = React.forwardRef<
  React.ElementRef<typeof ReactPhoneInput>,
  Omit<
    React.ComponentPropsWithoutRef<typeof ReactPhoneInput>,
    'country' | 'value' | 'onChange'
  >
>(({ international = true, className, ...props }, ref) => {
  const { country, value, onValueChange } = usePhoneInputContext();

  // https://github.com/catamphetamine/react-phone-number-input/blob/master/source/usePhoneDigits.js#L123
  // Underlying `usePhoneDigits` does not fully support controlled behavior, we needed this debounce hack to sync the value back to our store.
  const handleChange = debounce((v: Value) => onValueChange(v), 50);

  return (
    <ReactPhoneInput
      ref={ref}
      international={international}
      className={cn('px-3 py-2', className)}
      inputComponent={InputGroupInput}
      country={country}
      value={value}
      onChange={handleChange}
      {...props}
    />
  );
});
PhoneInputMobile.displayName = 'PhoneInputMobile';

Ideally, I was looking for a headless library that can fully support controlled behavior. I found react-phone-number-input, and unfortunately they suffer the same problem.

Would require more thoughts on this as well as the API design.

Anatomy

<PhoneInput.Root>
    <PhoneInput.Country />
    <PhoneInput.Input />
</PhoneInput.Root>

Libraries / Hooks

Credits

Visual reference taken from Shadcn Phone Input.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-designAPI needs more thorough considerationenhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions