Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New Component]: PhoneInput #6

Open
junwen-k opened this issue Nov 17, 2024 · 0 comments
Open

[New Component]: PhoneInput #6

junwen-k opened this issue Nov 17, 2024 · 0 comments
Labels
api-design API needs more thorough consideration enhancement New feature or request

Comments

@junwen-k
Copy link
Owner

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.

@junwen-k junwen-k added enhancement New feature or request api-design API needs more thorough consideration labels Nov 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-design API needs more thorough consideration enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant