Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
koddsson committed Apr 15, 2024
1 parent a393a98 commit 0fa1d95
Show file tree
Hide file tree
Showing 2 changed files with 1,224 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/ui/components/input-tel/src/LionInputTel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { Unparseable } from '@lion/ui/form-core.js';
import { LocalizeMixin } from '@lion/ui/localize-no-side-effects.js';
import { LionInput } from '@lion/ui/input.js';

import { PhoneUtilManager } from './PhoneUtilManager.js';
import { liveFormatPhoneNumber } from './preprocessors.js';
import { formatPhoneNumber } from './formatters.js';
import { parsePhoneNumber } from './parsers.js';
import { PhoneNumber } from './validators.js';
import { localizeNamespaceLoader } from './localizeNamespaceLoader.js';

import countryCodes from './country-codes.json' with {type: 'json'}

/**
* @typedef {import('../types/index.js').RegionCode} RegionCode
* @typedef {import('awesome-phonenumber').PhoneNumberFormat} PhoneNumberFormat
Expand Down Expand Up @@ -318,8 +319,16 @@ export class LionInputTel extends LocalizeMixin(LionInput) {
const value = !(this.modelValue instanceof Unparseable)
? this.modelValue
: this.value.match(regex)?.join('');
const regionDerivedFromValue =
value && this._phoneUtil && this._phoneUtil.parsePhoneNumber(value).regionCode;

regionDerivedFromValue = countryCodes.sort((a,b) => {
if (a.dial_code > b.dial_code) {
return -1;
} else if (a.dial_code < b.dial_code) {
return 1;
}
return 0;
})
.find(countryCode => value.startsWith(countryCode.dial_code)).code;

if (regionDerivedFromValue && this._allowedOrAllRegions.includes(regionDerivedFromValue)) {
this._setActiveRegion(regionDerivedFromValue);
Expand Down
Loading

0 comments on commit 0fa1d95

Please sign in to comment.