feat: add international auto-detect mode to PhoneNumberTransformer#11
Merged
Merged
Conversation
Adds an `international` option where the calling code is part of the editable text and the country is detected from it as the user types (e.g. typing +44 switches formatting to the UK). The worklet matches the longest calling-code prefix against a generated COUNTRY_CALLING_CODES index and formats the national part for that country, so detection + formatting stay on the UI thread. Also exports detectCountry(value) (JS thread) to drive a flag/country indicator from the value. Ambiguous codes (e.g. +1 NANP) resolve to the primary country; area-code disambiguation is left for later. Tests added for international formatting and detectCountry.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
PhoneNumberTransformerformats for a single fixedcountry, so a field built on it can't let the user type a different country's calling code — the worklet rewrites the prefix back to the configured country, and country changes have to go through a separate picker.Solution
Add an
internationaloption. The calling code becomes part of the editable text and the country is detected from it as you type: typing+44…switches formatting to the UK, live on the UI thread. Thecountryoption is ignored in this mode.COUNTRY_CALLING_CODESindex (calling code → ISO codes, primary first); the worklet matches the longest calling-code prefix (1–3 digits) against it and formats the national part with that country's formats. (Thephone-data.tsdiff is only this appended export — existingCOUNTRY_PHONE_DATAis unchanged, so ~235 of the changed lines are generated data.)detectCountry(value)is exported (JS thread) so a consumer can drive a flag/country indicator from the value.Ambiguous codes (e.g.
+1NANP) resolve to the primary country; per-area-code disambiguation is left for later.Test plan
libphonenumber-jsAsYouType, e.g.+447911123456→+44 7911 123456.yarn test— added cases covering+1/+33/+44formatting (incl. a French trunk-0 number, and thatcountryis ignored), bare-code display, anddetectCountryUS/GB/FR/undefined.+44…switches formatting + the flag inline; the picker sets the+code; continuing to type formats correctly.