Skip to content

Commit

Permalink
feat(experimental): InputPhoneInternational refactor to new textfie…
Browse files Browse the repository at this point in the history
…ld approach
  • Loading branch information
waterplea committed Dec 12, 2024
1 parent 9a2af42 commit c7c21ac
Show file tree
Hide file tree
Showing 35 changed files with 773 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {MaskitoDirective} from '@maskito/angular';
import {TUI_MASK_CARD} from '@taiga-ui/addon-commerce/constants';
import {TUI_PAYMENT_SYSTEM_ICONS} from '@taiga-ui/addon-commerce/tokens';
import {tuiControlValue} from '@taiga-ui/cdk/observables';
import {TuiTextfieldContent} from '@taiga-ui/core/components/textfield';
import {TuiTextfieldContent, TuiWithTextfield} from '@taiga-ui/core/components/textfield';
import {tuiInjectIconResolver} from '@taiga-ui/core/tokens';
import {tuiMaskito} from '@taiga-ui/kit/utils';
import {distinctUntilChanged, map, skip, startWith, switchMap, timer} from 'rxjs';
Expand All @@ -38,7 +38,7 @@ import {TUI_INPUT_CARD_OPTIONS} from './input-card.options';
styleUrls: ['./input-card.style.less'],
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
hostDirectives: [MaskitoDirective],
hostDirectives: [MaskitoDirective, TuiWithTextfield],
host: {
inputmode: 'numeric',
placeholder: '0000 0000 0000 0000',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import {Directive, inject, Input} from '@angular/core';
import {MaskitoDirective} from '@maskito/angular';
import {TUI_INPUT_CARD_OPTIONS} from '@taiga-ui/addon-commerce/components/input-card';
import {TUI_MASK_CVC} from '@taiga-ui/addon-commerce/constants';
import {TuiWithTextfield} from '@taiga-ui/core/components/textfield';
import {tuiMaskito} from '@taiga-ui/kit/utils';

@Directive({
standalone: true,
selector: 'input[tuiInputCVC]',
hostDirectives: [MaskitoDirective],
hostDirectives: [MaskitoDirective, TuiWithTextfield],
host: {
inputmode: 'numeric',
'[autocomplete]': 'autocomplete ? "cc-csc" : "off"',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import {Directive, inject, Input} from '@angular/core';
import {MaskitoDirective} from '@maskito/angular';
import {TUI_INPUT_CARD_OPTIONS} from '@taiga-ui/addon-commerce/components/input-card';
import {TUI_MASK_EXPIRE} from '@taiga-ui/addon-commerce/constants';
import {TuiWithTextfield} from '@taiga-ui/core/components/textfield';
import {tuiMaskito} from '@taiga-ui/kit/utils';

@Directive({
standalone: true,
selector: 'input[tuiInputExpire]',
hostDirectives: [MaskitoDirective],
hostDirectives: [MaskitoDirective, TuiWithTextfield],
host: {
inputmode: 'numeric',
placeholder: '00/00',
Expand Down
17 changes: 15 additions & 2 deletions projects/core/components/textfield/textfield.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class TuiTextfieldBase<T> implements OnChanges {
protected readonly s = tuiAppearanceState(null);
protected readonly m = tuiAppearanceMode(this.mode);
protected readonly f = tuiAppearanceFocus(
computed(() => this.focused() || this.textfield.focused()),
computed(() => this.focused() ?? this.textfield.focused()),
);

protected readonly el = tuiInjectElement<HTMLInputElement>();
Expand Down Expand Up @@ -99,7 +99,9 @@ export class TuiTextfieldBase<T> implements OnChanges {

@Directive({
standalone: true,
selector: 'input[tuiTextfield]',
// TODO: Remove :not in v.5
selector:
'input[tuiTextfield]:not([tuiInputCard]):not([tuiInputExpire]):not([tuiInputCVC])',
hostDirectives: [TuiNativeValidator, TuiAppearance],
host: {
'[id]': 'textfield.id',
Expand All @@ -111,3 +113,14 @@ export class TuiTextfieldBase<T> implements OnChanges {
},
})
export class TuiTextfieldDirective<T> extends TuiTextfieldBase<T> {}

@Directive({
standalone: true,
hostDirectives: [
{
directive: TuiTextfieldDirective,
inputs: ['invalid', 'focused', 'readOnly', 'state'],
},
],
})
export class TuiWithTextfield {}
18 changes: 7 additions & 11 deletions projects/core/directives/dropdown/dropdown-open.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,15 @@ export class TuiDropdownOpen implements OnChanges {

protected onKeydown({key, target, defaultPrevented}: KeyboardEvent): void {
if (
defaultPrevented ||
!tuiIsEditingKey(key) ||
!this.editable ||
!this.focused ||
!this.directive.content ||
!tuiIsHTMLElement(target) ||
(tuiIsElementEditable(target) && target !== this.host)
!defaultPrevented &&
tuiIsEditingKey(key) &&
this.editable &&
this.focused &&
tuiIsHTMLElement(target) &&
!tuiIsElementEditable(target)
) {
return;
this.host.focus({preventScroll: true});
}

this.update(true);
this.host.focus({preventScroll: true});
}

private get host(): HTMLElement {
Expand Down
1 change: 1 addition & 0 deletions projects/core/styles/components/textfield.less
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ tui-textfield {
gap: 0.25rem;
margin-inline-start: auto;
isolation: isolate;
border-radius: inherit;

> tui-icon {
pointer-events: auto;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<input
formControlName="card"
tuiInputCard
tuiTextfield
[icon]="card"
(binChange)="onBinChange($event)"
/>
Expand All @@ -15,15 +14,13 @@
<input
formControlName="expire"
tuiInputExpire
tuiTextfield
/>
</tui-textfield>
<tui-textfield>
<label tuiLabel>CVC/CVV</label>
<input
formControlName="cvc"
tuiInputCVC
tuiTextfield
/>
</tui-textfield>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<input
icon=""
tuiInputCard
tuiTextfield
[(ngModel)]="card"
/>
<tui-thumbnail-card
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,20 @@
<input
formControlName="card"
tuiInputCard
tuiTextfield
/>
</tui-textfield>
<tui-textfield>
<label tuiLabel>Expire date</label>
<input
formControlName="expire"
tuiInputExpire
tuiTextfield
/>
</tui-textfield>
<tui-textfield>
<label tuiLabel>CVC/CVV</label>
<input
formControlName="cvc"
tuiInputCVC
tuiTextfield
/>
</tui-textfield>
</form>
Expand Down
1 change: 0 additions & 1 deletion projects/demo/src/modules/components/input-card/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
<label tuiLabel>Card number</label>
<input
tuiInputCard
tuiTextfield
[autocomplete]="autocomplete"
[disabled]="control.disabled"
[icon]="icon"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<tui-input-phone-international
[countries]="countries"
[(countryIsoCode)]="countryIsoCode"
[(ngModel)]="value"
/>
<tui-textfield>
<input
tuiInputPhoneInternational
[countries]="countries"
[(countryIsoCode)]="countryIsoCode"
[(ngModel)]="value"
/>
</tui-textfield>
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import {Component} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {changeDetection} from '@demo/emulate/change-detection';
import {encapsulation} from '@demo/emulate/encapsulation';
import {TuiTextfield} from '@taiga-ui/core';
import {TuiInputPhoneInternational} from '@taiga-ui/experimental';
import type {TuiCountryIsoCode} from '@taiga-ui/i18n';
import {
TuiInputPhoneInternational,
tuiInputPhoneInternationalOptionsProvider,
} from '@taiga-ui/kit';
import {tuiInputPhoneInternationalOptionsProvider} from '@taiga-ui/kit';
import {defer} from 'rxjs';

@Component({
standalone: true,
imports: [FormsModule, TuiInputPhoneInternational],
imports: [FormsModule, TuiInputPhoneInternational, TuiTextfield],
templateUrl: './index.html',
encapsulation,
changeDetection,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<tui-input-phone-international
[countries]="(countries | tuiSortCountries | async) || []"
[countrySearch]="true"
[(countryIsoCode)]="countryIsoCode"
[(ngModel)]="value"
>
Type your number
</tui-input-phone-international>
<tui-textfield>
<label tuiLabel>Phone number</label>
<input
tuiInputPhoneInternational
[countries]="(countries | tuiSortCountries | async) || []"
[countrySearch]="true"
[(countryIsoCode)]="countryIsoCode"
[(ngModel)]="value"
/>
</tui-textfield>
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import {Component} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {changeDetection} from '@demo/emulate/change-detection';
import {encapsulation} from '@demo/emulate/encapsulation';
import {TuiTextfield} from '@taiga-ui/core';
import {TuiInputPhoneInternational} from '@taiga-ui/experimental';
import type {TuiCountryIsoCode} from '@taiga-ui/i18n';
import {
TuiInputPhoneInternational,
tuiInputPhoneInternationalOptionsProvider,
TuiSortCountriesPipe,
} from '@taiga-ui/kit';
Expand All @@ -14,7 +15,13 @@ import {defer} from 'rxjs';

@Component({
standalone: true,
imports: [AsyncPipe, FormsModule, TuiInputPhoneInternational, TuiSortCountriesPipe],
imports: [
AsyncPipe,
FormsModule,
TuiInputPhoneInternational,
TuiSortCountriesPipe,
TuiTextfield,
],
templateUrl: './index.html',
encapsulation,
changeDetection,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<tui-input-phone-international
[countries]="countries"
[(countryIsoCode)]="countryIsoCode"
[(ngModel)]="value"
>
Phone number
<tui-textfield [style.inline-size.rem]="20">
<label tuiLabel>Phone number</label>
<input
tuiInputPhoneInternational
[countries]="countries"
[(countryIsoCode)]="countryIsoCode"
[(ngModel)]="value"
/>
<tui-icon
appearance="negative"
tuiHintAppearance="error"
tuiHintDirection="top"
tuiTooltip="I am a hint"
/>
<tui-icon icon="@tui.phone" />
</tui-input-phone-international>
</tui-textfield>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ import {Component} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {changeDetection} from '@demo/emulate/change-detection';
import {encapsulation} from '@demo/emulate/encapsulation';
import {TuiIcon} from '@taiga-ui/core';
import {TuiIcon, TuiTextfield} from '@taiga-ui/core';
import {TuiInputPhoneInternational} from '@taiga-ui/experimental';
import type {TuiCountryIsoCode} from '@taiga-ui/i18n';
import {
TuiInputPhoneInternational,
tuiInputPhoneInternationalOptionsProvider,
TuiTooltip,
} from '@taiga-ui/kit';
import {tuiInputPhoneInternationalOptionsProvider, TuiTooltip} from '@taiga-ui/kit';
import {defer} from 'rxjs';

@Component({
standalone: true,
imports: [FormsModule, TuiIcon, TuiInputPhoneInternational, TuiTooltip],
imports: [FormsModule, TuiIcon, TuiInputPhoneInternational, TuiTooltip, TuiTextfield],
templateUrl: './index.html',
styleUrls: ['./index.less'],
encapsulation,
changeDetection,
providers: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<tui-input-phone-international
[countries]="countries"
[(countryIsoCode)]="countryIsoCode"
[(ngModel)]="value"
>
Type your number
</tui-input-phone-international>
<tui-textfield>
<label tuiLabel>Type your number</label>
<input
tuiInputPhoneInternational
[countries]="countries"
[(countryIsoCode)]="countryIsoCode"
[(ngModel)]="value"
/>
</tui-textfield>
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ import {Component} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {changeDetection} from '@demo/emulate/change-detection';
import {encapsulation} from '@demo/emulate/encapsulation';
import {TuiTextfield} from '@taiga-ui/core';
import {TuiInputPhoneInternational} from '@taiga-ui/experimental';
import type {TuiCountryIsoCode} from '@taiga-ui/i18n';
import {
TuiInputPhoneInternational,
tuiInputPhoneInternationalOptionsProvider,
} from '@taiga-ui/kit';
import {tuiInputPhoneInternationalOptionsProvider} from '@taiga-ui/kit';
import {getCountries} from 'libphonenumber-js';
import {defer} from 'rxjs';

@Component({
standalone: true,
imports: [FormsModule, TuiInputPhoneInternational],
imports: [FormsModule, TuiInputPhoneInternational, TuiTextfield],
templateUrl: './index.html',
encapsulation,
changeDetection,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
```ts
import {ReactiveFormsModule} from '@angular/forms';
import type {TuiCountryIsoCode} from '@taiga-ui/i18n';
import {TuiInputPhoneInternational} from '@taiga-ui/kit';

@Component({
Expand All @@ -12,11 +13,6 @@ export class Example {
testValue: new FormControl('+78005553535'),
});

readonly countries: ReadonlyArray<TuiCountryIsoCode> = [
TuiCountryIsoCode.RU,
TuiCountryIsoCode.KZ,
TuiCountryIsoCode.UA,
TuiCountryIsoCode.BY,
];
readonly countries: ReadonlyArray<TuiCountryIsoCode> = ['RU', 'KZ', 'UA', 'BY'];
}
```
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
```html
<form [formGroup]="testForm">
<tui-input-phone-international
formControlName="testValue"
[countries]="countries"
></tui-input-phone-international>
<tui-textfield>
<input
tuiInputPhoneInternational
formControlName="testValue"
[countries]="countries"
/>
</tui-textfield>
</form>
```
Loading

0 comments on commit c7c21ac

Please sign in to comment.