Skip to content
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
[attr.aria-labelledby]="id() + '-aria-label ' + id() + '-value'"
[attr.aria-expanded]="open"
[options]="countryList()"
[value]="selectedCountry"
[value]="selectedCountry()"
[tabindex]="disabled() ? '-1' : '0'"
[attr.aria-controls]="id() + '-listbox'"
(valueChange)="countryInput($event)"
Expand All @@ -30,12 +30,12 @@
}}</span>
<span
aria-hidden="true"
[class]="`fi fi-${(selectedCountry?.isoCode | lowercase) ?? 'xx'}`"
[class]="`fi fi-${(selectedCountry()?.isoCode | lowercase) ?? 'xx'}`"
></span>
@if (selectedCountry) {
@if (selectedCountry()) {
<span class="si-body ms-4" [id]="id() + '-value'">
<span class="visually-hidden">{{ selectedCountry.name }}</span>
+{{ selectedCountry.countryCode }}
<span class="visually-hidden">{{ selectedCountry()!.name }}</span>
+{{ selectedCountry()!.countryCode }}
</span>
}
Comment on lines +35 to 40

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using the local variable binding syntax in Angular's native @if control flow (@if (selectedCountry(); as selected)) allows us to avoid multiple signal reads and eliminates the need for non-null assertions (!) in the template.

Suggested change
@if (selectedCountry()) {
<span class="si-body ms-4" [id]="id() + '-value'">
<span class="visually-hidden">{{ selectedCountry.name }}</span>
+{{ selectedCountry.countryCode }}
<span class="visually-hidden">{{ selectedCountry()!.name }}</span>
+{{ selectedCountry()!.countryCode }}
</span>
}
@if (selectedCountry(); as selected) {
<span class="si-body ms-4" [id]="id() + '-value'">
<span class="visually-hidden">{{ selected.name }}</span>
+{{ selected.countryCode }}
</span>
}
References
  1. Use native control flow (@if, @for, @switch) instead of *ngIf, *ngFor, *ngSwitch (link)

<si-icon class="icon dropdown-caret" [icon]="icons.elementDown2" />
Expand Down Expand Up @@ -65,6 +65,11 @@
{{ option.value.name }} +{{ option.value.countryCode }}
</ng-template>
</button>
@if (nationalPrefix()) {
<span class="national-prefix d-flex align-items-center" aria-hidden="true"
>({{ nationalPrefix() }})</span
>
}
<input
#phoneInput
type="tel"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@
}
}

.national-prefix {
color: all-variables.$element-text-secondary;
white-space: nowrap;
cursor: default;
user-select: none;
}

.disabled .national-prefix {
color: all-variables.$element-text-disabled;
}

.phone-number {
background-color: transparent;
inline-size: inherit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,55 @@ describe('SiPhoneNumberInputComponent', () => {
expect(component.form.controls.workPhone.errors).toBeFalsy();
});

it('should keep the mobile token visible for numbers that only expose it in international format', async () => {
// Argentine mobile numbers carry a `9` after the country code in international
// format (`+54 9 11 ...`). The national format hides it as a `15` infix, so the
// input must display the international significant number to keep the `9` visible.
component.supportedCountries.set(null);
await fixture.whenStable();
component.form.controls.workPhone.setValue('+5491123456789');
await fixture.whenStable();

expect(component.country()).toBe('AR');
expect(inputElement.value).toEqual('9 11 2345-6789');
});

describe('national (trunk) prefix indicator', () => {
const getNationalPrefix = (): HTMLElement | null =>
element.querySelector<HTMLElement>('.national-prefix');

beforeEach(async () => {
component.supportedCountries.set(null);
await fixture.whenStable();
});

it('should show the indicator for numbers with a droppable `0` trunk prefix', async () => {
component.form.controls.workPhone.setValue('+4917612345678');
await fixture.whenStable();
expect(getNationalPrefix()).toHaveTextContent('(0)');
});

it('should not show the indicator for countries without a droppable `0` trunk prefix', async () => {
component.form.controls.workPhone.setValue('+12025550173');
await fixture.whenStable();
expect(getNationalPrefix()).toBeNull();
});

it('should show the indicator for an Argentine landline', async () => {
component.form.controls.workPhone.setValue('+541143211234');
await fixture.whenStable();
expect(getNationalPrefix()).toHaveTextContent('(0)');
});

it('should not show the indicator for an Argentine mobile', async () => {
// The international `9` mobile token and the domestic `0 … 15` marker are different
// encodings, so `+54 (0) 9 …` would be misleading.
component.form.controls.workPhone.setValue('+5491123456789');
await fixture.whenStable();
expect(getNationalPrefix()).toBeNull();
});
});

describe('with control disabled', () => {
let phoneInput: HTMLElement;

Expand Down
Loading
Loading