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

refactor(components/phone-field): use standalone components, address all linting errors #3004

Merged
merged 4 commits into from
Jan 13, 2025
Merged
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
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { SkyInputBoxModule } from '@skyux/forms';

import { SkyPhoneFieldModule } from '../phone-field.module';
import { SkyPhoneFieldCountry } from '../types/country';

@Component({
imports: [FormsModule, SkyInputBoxModule, SkyPhoneFieldModule],
selector: 'sky-test-cmp',
templateUrl: './phone-field-input-box.component.fixture.html',
standalone: false,
})
export class PhoneFieldInputBoxTestComponent {
public hintText: string | undefined;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { Component, OnInit, ViewChild } from '@angular/core';
import { UntypedFormControl, UntypedFormGroup } from '@angular/forms';
import {
ReactiveFormsModule,
UntypedFormControl,
UntypedFormGroup,
} from '@angular/forms';

import { SkyPhoneFieldInputDirective } from '../phone-field-input.directive';
import { SkyPhoneFieldComponent } from '../phone-field.component';
import { SkyPhoneFieldModule } from '../phone-field.module';
import { SkyPhoneFieldCountry } from '../types/country';
import { SkyPhoneFieldNumberReturnFormat } from '../types/number-return-format';

@Component({
imports: [ReactiveFormsModule, SkyPhoneFieldModule],
selector: 'sky-test-cmp',
templateUrl: './phone-field-reactive.component.fixture.html',
standalone: false,
})
export class PhoneFieldReactiveTestComponent implements OnInit {
public allowExtensions = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Component, ViewChild } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { SkyPhoneFieldInputDirective } from '../phone-field-input.directive';
import { SkyPhoneFieldComponent } from '../phone-field.component';
import { SkyPhoneFieldModule } from '../phone-field.module';
import { SkyPhoneFieldCountry } from '../types/country';
import { SkyPhoneFieldNumberReturnFormat } from '../types/number-return-format';

@Component({
imports: [FormsModule, SkyPhoneFieldModule],
selector: 'sky-test-cmp',
templateUrl: './phone-field.component.fixture.html',
standalone: false,
})
export class PhoneFieldTestComponent {
public allowExtensions = true;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ import {
Validator,
} from '@angular/forms';

import { PhoneNumberFormat, PhoneNumberUtil } from 'google-libphonenumber';
import {
PhoneNumber,
PhoneNumberFormat,
PhoneNumberUtil,
} from 'google-libphonenumber';
import { Subject, take, takeUntil } from 'rxjs';

import { SkyPhoneFieldAdapterService } from './phone-field-adapter.service';
import { SkyPhoneFieldComponent } from './phone-field.component';
import { SkyPhoneFieldNumberReturnFormat } from './types/number-return-format';

/**
* Creates a button, search input, and text input for entering and validating
Expand Down Expand Up @@ -50,7 +55,6 @@ import { SkyPhoneFieldComponent } from './phone-field.component';
multi: true,
},
],
standalone: false,
})
export class SkyPhoneFieldInputDirective
implements OnInit, OnDestroy, ControlValueAccessor, Validator
Expand Down Expand Up @@ -207,7 +211,7 @@ export class SkyPhoneFieldInputDirective
this.#notifyChange?.(this.#getValue());
}

#formatPhoneNumber(value: string | undefined): string | undefined {
#maybeFormatPhoneNumber(value: string | undefined): string | undefined {
Blackbaud-TrevorBurch marked this conversation as resolved.
Show resolved Hide resolved
if (!value) {
return;
}
Expand All @@ -223,39 +227,47 @@ export class SkyPhoneFieldInputDirective
);

if (this.#phoneUtils.isPossibleNumber(phoneNumber)) {
switch (returnFormat) {
case 'international':
return this.#phoneUtils.format(
phoneNumber,
PhoneNumberFormat.INTERNATIONAL,
);

case 'national':
return this.#phoneUtils.format(
phoneNumber,
PhoneNumberFormat.NATIONAL,
);

case 'default':
default:
return regionCode && regionCode !== defaultCountry
? this.#phoneUtils.format(
phoneNumber,
PhoneNumberFormat.INTERNATIONAL,
)
: this.#phoneUtils.format(
phoneNumber,
PhoneNumberFormat.NATIONAL,
);
}
return this.#formatPhoneNumber(
phoneNumber,
returnFormat,
defaultCountry,
regionCode,
);
}
} catch (err) {
} catch {
/* */
}

return;
}

#formatPhoneNumber(
phoneNumber: PhoneNumber,
returnFormat?: SkyPhoneFieldNumberReturnFormat,
defaultCountry?: string,
regionCode?: string,
): string {
switch (returnFormat) {
case 'international':
return this.#phoneUtils.format(
phoneNumber,
PhoneNumberFormat.INTERNATIONAL,
);

case 'national':
return this.#phoneUtils.format(phoneNumber, PhoneNumberFormat.NATIONAL);

case 'default':
default:
return regionCode && regionCode !== defaultCountry
? this.#phoneUtils.format(
phoneNumber,
PhoneNumberFormat.INTERNATIONAL,
)
: this.#phoneUtils.format(phoneNumber, PhoneNumberFormat.NATIONAL);
}
}

#getDefaultCountry(): string | undefined {
return this.#phoneFieldComponent?.defaultCountry;
}
Expand Down Expand Up @@ -284,15 +296,15 @@ export class SkyPhoneFieldInputDirective
}

return this.#phoneUtils.isValidNumberForRegion(phoneNumber, regionCode);
} catch (e) {
} catch {
return false;
}
}

#setValue(value: string | undefined): void {
/* istanbul ignore else */
if (value !== undefined) {
const formatted = this.#formatPhoneNumber(value);
const formatted = this.#maybeFormatPhoneNumber(value);
this.#_value = formatted ?? value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@ import {
fakeAsync,
tick,
} from '@angular/core/testing';
import {
FormsModule,
NgModel,
ReactiveFormsModule,
UntypedFormControl,
} from '@angular/forms';
import { NgModel, UntypedFormControl } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { SkyAppTestUtility, expect, expectAsync } from '@skyux-sdk/testing';
import { SkyInputBoxModule } from '@skyux/forms';
import {
SkyTheme,
SkyThemeMode,
Expand All @@ -27,7 +21,6 @@ import { BehaviorSubject } from 'rxjs';
import { PhoneFieldInputBoxTestComponent } from './fixtures/phone-field-input-box.component.fixture';
import { PhoneFieldReactiveTestComponent } from './fixtures/phone-field-reactive.component.fixture';
import { PhoneFieldTestComponent } from './fixtures/phone-field.component.fixture';
import { SkyPhoneFieldModule } from './phone-field.module';

describe('Phone Field Component', () => {
let mockThemeSvc: Partial<SkyThemeService>;
Expand Down Expand Up @@ -287,8 +280,7 @@ describe('Phone Field Component', () => {
};

TestBed.configureTestingModule({
declarations: [PhoneFieldTestComponent],
imports: [SkyPhoneFieldModule, NoopAnimationsModule, FormsModule],
imports: [PhoneFieldTestComponent, NoopAnimationsModule],
providers: [
{
provide: SkyThemeService,
Expand Down Expand Up @@ -1383,13 +1375,7 @@ describe('Phone Field Component', () => {
};

TestBed.configureTestingModule({
declarations: [PhoneFieldReactiveTestComponent],
imports: [
SkyPhoneFieldModule,
NoopAnimationsModule,
FormsModule,
ReactiveFormsModule,
],
imports: [PhoneFieldReactiveTestComponent, NoopAnimationsModule],
providers: [
{
provide: SkyThemeService,
Expand Down Expand Up @@ -2292,13 +2278,7 @@ describe('Phone Field Component', () => {
};

TestBed.configureTestingModule({
declarations: [PhoneFieldInputBoxTestComponent],
imports: [
SkyInputBoxModule,
SkyPhoneFieldModule,
NoopAnimationsModule,
FormsModule,
],
imports: [PhoneFieldInputBoxTestComponent, NoopAnimationsModule],
providers: [
{
provide: SkyThemeService,
Expand Down
Loading
Loading