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

Remove awesome phonenumber #2257

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 17 additions & 77 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.js';

/**
* @typedef {import('../types/index.js').RegionCode} RegionCode
* @typedef {import('awesome-phonenumber').PhoneNumberFormat} PhoneNumberFormat
Expand All @@ -27,7 +28,6 @@ export class LionInputTel extends LocalizeMixin(LionInput) {
formatStrategy: { type: String, attribute: 'format-strategy' },
formatCountryCodeStyle: { type: String, attribute: 'format-country-code-style' },
activeRegion: { type: String },
_phoneUtil: { type: Object, state: true },
};

static localizeNamespaces = [
Expand All @@ -51,38 +51,6 @@ export class LionInputTel extends LocalizeMixin(LionInput) {
// eslint-disable-next-line class-methods-use-this, no-empty-function
set activeRegion(v) {}

/**
* Type of phone number, derived from textbox value. Enum with values:
* -'fixed-line'
* -'fixed-line-or-mobile'
* -'mobile'
* -'pager'
* -'personal-number'
* -'premium-rate'
* -'shared-cost'
* -'toll-free'
* -'uan'
* -'voip'
* -'unknown'
* See https://www.npmjs.com/package/awesome-phonenumber
* @readonly
* @property {PhoneNumberTypes|undefined} activePhoneNumberTypes
*/
get activePhoneNumberType() {
let pn;
try {
pn =
this._phoneUtil &&
this._phoneUtil.parsePhoneNumber(this.modelValue, { regionCode: this.activeRegion });
// eslint-disable-next-line no-empty
} catch (_) {}
return pn?.type || 'unknown';
}

// @ts-ignore read only
// eslint-disable-next-line class-methods-use-this, no-empty-function
set activePhoneNumberType(v) {}

/**
* Protected setter for activeRegion, only meant for subclassers
* @protected
Expand All @@ -100,21 +68,7 @@ export class LionInputTel extends LocalizeMixin(LionInput) {
* @type {RegionCode[]}
*/
get _allowedOrAllRegions() {
return (
(this.allowedRegions?.length
? this.allowedRegions
: this._phoneUtil?.getSupportedRegionCodes()) || []
);
}

/**
* @property _phoneUtilLoadComplete
* @protected
* @type {Promise<PhoneNumber>}
*/
// eslint-disable-next-line class-methods-use-this
get _phoneUtilLoadComplete() {
return PhoneUtilManager.loadComplete;
return this.allowedRegions;
}

/**
Expand Down Expand Up @@ -161,20 +115,8 @@ export class LionInputTel extends LocalizeMixin(LionInput) {
/** @configures ValidateMixin */
this.defaultValidators.push(this.__isPhoneNumberValidatorInstance);

// Expose awesome-phonenumber lib for Subclassers
/**
* @protected
* @type {AwesomePhoneNumber|null}
*/
this._phoneUtil = PhoneUtilManager.isLoaded
? /** @type {AwesomePhoneNumber} */ (PhoneUtilManager.PhoneUtil)
: null;

if (!PhoneUtilManager.isLoaded) {
PhoneUtilManager.loadComplete.then(() => {
this._onPhoneNumberUtilReady();
});
}
this._calculateValues({ source: null });
this.__calculateActiveRegion();
}

/**
Expand Down Expand Up @@ -291,18 +233,6 @@ export class LionInputTel extends LocalizeMixin(LionInput) {
return !this.__isUpdatingRegionWhileFocused && super._reflectBackOn();
}

/**
* @protected
*/
_onPhoneNumberUtilReady() {
// This should trigger a rerender in shadow dom
this._phoneUtil = /** @type {AwesomePhoneNumber} */ (PhoneUtilManager.PhoneUtil);
// This should trigger a rerender in light dom
// Format when libPhoneNumber is loaded
this._calculateValues({ source: null });
this.__calculateActiveRegion();
}

/**
* @private
*/
Expand All @@ -318,8 +248,18 @@ 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;

const regionDerivedFromValue = countryCodes
?.sort((a, b) => {
if (a.dial_code > b.dial_code) {
return -1;
}
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
Loading