Skip to content

PhoneInput: controllable country + detect country from pasted numbers #31

Closed

Description

@sapatmohit

PhoneInput: controllable country + detect country from pasted numbers

Today the selected country is internal state seeded once from defaultCountry:

const [selectedCountry, setSelectedCountry] = React.useState(() => getCountryByCode(defaultCountry) ?? COUNTRIES[0]);

This breaks the library's controlled/uncontrolled convention (see ARCHITECTURE.md) — a parent can't change or reset the country, and changing defaultCountry after mount does nothing.

Proposal

  • Add country / onCountryChange and route both through useControllableState; keep defaultCountry for uncontrolled use.
  • When the user types or pastes a number starting with + (e.g. +44 20 7946 0958), match the longest dial code, switch the country and strip the prefix from the input value.
  • Shared dial codes (+1 US/CA, +7 RU/KZ): keep the currently selected country if it matches, else pick the first.

Acceptance

  • Tests: controlled country follows parent; paste +44… selects GB; +1 keeps CA if CA selected
  • Docs updated with props table

Activity

  1. ujwalbajaj commented on Oct 24, 2026

    @ujwalbajaj
    Collaborator

    Hey @sapatmohit can you assign this issue to me, I am interested in working on this!

  2. sapatmohit commented on Oct 24, 2026

    @sapatmohit
    MemberAuthor

    Done!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions