Skip to content

Commit

Permalink
refactor: use injectContext (#8870)
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode authored Sep 6, 2024
1 parent ab62212 commit 6f28012
Show file tree
Hide file tree
Showing 27 changed files with 55 additions and 68 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
"plugin:@taiga-ui/experience/entry-points"
],
"rules": {
"no-restricted-imports": "off",
"@typescript-eslint/no-import-type-side-effects": "off",
"@typescript-eslint/prefer-regexp-exec": "off",
"@typescript-eslint/use-unknown-in-catch-callback-variable": "off",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {TUI_ANIMATIONS_SPEED} from '@taiga-ui/core/tokens';
import {tuiGetDuration} from '@taiga-ui/core/utils/miscellaneous';
import {TUI_DAY_CAPS_MAPPER} from '@taiga-ui/kit/components/calendar-range';
import {TUI_MOBILE_CALENDAR} from '@taiga-ui/kit/tokens';
import {POLYMORPHEUS_CONTEXT} from '@taiga-ui/polymorpheus';
import {injectContext} from '@taiga-ui/polymorpheus';
import type {Observer} from 'rxjs';

export interface TuiMobileCalendarData {
Expand Down Expand Up @@ -43,7 +43,7 @@ export class TuiMobileCalendarDropdown {
// TODO: Rework to use TuiDropdownOpenDirective so the focus returns to the field on closing
private readonly dropdown = inject(TuiDropdownDirective, {optional: true});
private readonly keyboard = inject(TuiKeyboardService);
private readonly context = inject(POLYMORPHEUS_CONTEXT, {optional: true});
private readonly context = injectContext<Record<string, any>>({optional: true});
private readonly observer?: Observer<any> = this.context?.$implicit;
private readonly data: TuiMobileCalendarData = this.context?.data || {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {TuiPopover} from '@taiga-ui/cdk/services';
import {TUI_IS_IOS} from '@taiga-ui/cdk/tokens';
import {TuiButton} from '@taiga-ui/core/components/button';
import {
POLYMORPHEUS_CONTEXT,
injectContext,
PolymorpheusOutlet,
PolymorpheusTemplate,
} from '@taiga-ui/polymorpheus';
Expand Down Expand Up @@ -33,7 +33,7 @@ import type {TuiMobileDialogOptions} from './mobile-dialog.options';
export class TuiMobileDialog<I> {
protected readonly isIOS = inject(TUI_IS_IOS);
protected readonly context =
inject<TuiPopover<TuiMobileDialogOptions<I>, number>>(POLYMORPHEUS_CONTEXT);
injectContext<TuiPopover<TuiMobileDialogOptions<I>, number>>();

protected onAction(index: number): void {
this.context.completeWith(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {NgIf} from '@angular/common';
import {ChangeDetectionStrategy, Component, inject} from '@angular/core';
import type {TuiContext} from '@taiga-ui/cdk/types';
import {TuiLoader} from '@taiga-ui/core/components/loader';
import {POLYMORPHEUS_CONTEXT, PolymorpheusComponent} from '@taiga-ui/polymorpheus';
import {injectContext, PolymorpheusComponent} from '@taiga-ui/polymorpheus';

import {TUI_PULL_TO_REFRESH_THRESHOLD} from '../pull-to-refresh.providers';
import {MICRO_OFFSET} from '../pull-to-refresh.service';
Expand All @@ -25,7 +25,7 @@ const ROTATE_X_MULTIPLIER = 2.3;
},
})
export class TuiMobileLoaderAndroid {
private readonly context = inject<TuiContext<number>>(POLYMORPHEUS_CONTEXT);
private readonly context = injectContext<TuiContext<number>>();
private readonly threshold = inject(TUI_PULL_TO_REFRESH_THRESHOLD);

protected get percent(): number {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {NgIf} from '@angular/common';
import {ChangeDetectionStrategy, Component, inject} from '@angular/core';
import {TuiRepeatTimes} from '@taiga-ui/cdk/directives/repeat-times';
import type {TuiContext} from '@taiga-ui/cdk/types';
import {POLYMORPHEUS_CONTEXT, PolymorpheusComponent} from '@taiga-ui/polymorpheus';
import {injectContext, PolymorpheusComponent} from '@taiga-ui/polymorpheus';

import {TUI_PULL_TO_REFRESH_THRESHOLD} from '../pull-to-refresh.providers';

Expand All @@ -18,7 +18,7 @@ const ROTATE_X_STEP = 30;
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TuiMobileLoaderIOS {
private readonly context = inject<TuiContext<number>>(POLYMORPHEUS_CONTEXT);
private readonly context = injectContext<TuiContext<number>>();
private readonly threshold = inject(TUI_PULL_TO_REFRESH_THRESHOLD);

protected readonly steps = 12;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
import {tuiGetDuration} from '@taiga-ui/core/utils/miscellaneous';
import {shouldCall} from '@taiga-ui/event-plugins';
import {
POLYMORPHEUS_CONTEXT,
injectContext,
PolymorpheusOutlet,
PolymorpheusTemplate,
} from '@taiga-ui/polymorpheus';
Expand Down Expand Up @@ -93,7 +93,7 @@ export class TuiSheetDialogComponent<I> implements AfterViewInit {
protected readonly icons = inject(TUI_COMMON_ICONS);
protected readonly closeWord$ = inject(TUI_CLOSE_WORD);
protected readonly context =
inject<TuiPopover<TuiSheetDialogOptions<I>, any>>(POLYMORPHEUS_CONTEXT);
injectContext<TuiPopover<TuiSheetDialogOptions<I>, any>>();

public ngAfterViewInit(): void {
this.el.scrollTop =
Expand Down
5 changes: 2 additions & 3 deletions projects/core/components/alert/alert.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
TUI_COMMON_ICONS,
} from '@taiga-ui/core/tokens';
import {tuiToAnimationOptions} from '@taiga-ui/core/utils';
import {POLYMORPHEUS_CONTEXT, PolymorpheusOutlet} from '@taiga-ui/polymorpheus';
import {injectContext, PolymorpheusOutlet} from '@taiga-ui/polymorpheus';
import {EMPTY, fromEvent, of, repeat, switchMap, takeUntil, timer} from 'rxjs';

import type {TuiAlertOptions} from './alert.interfaces';
Expand Down Expand Up @@ -42,8 +42,7 @@ export class TuiAlertComponent<O, I> {
protected readonly options = tuiToAnimationOptions(inject(TUI_ANIMATIONS_SPEED));
protected readonly close = toSignal(inject(TUI_CLOSE_WORD));
protected readonly position = inject(TUI_ALERT_POSITION);
protected readonly item =
inject<TuiPopover<TuiAlertOptions<I>, O>>(POLYMORPHEUS_CONTEXT);
protected readonly item = injectContext<TuiPopover<TuiAlertOptions<I>, O>>();

protected readonly animation = this.position.endsWith('auto')
? {...this.options, value: 'right'}
Expand Down
1 change: 1 addition & 0 deletions projects/core/components/alert/alerts.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import {TuiMapperPipe} from '@taiga-ui/cdk/pipes/mapper';
import type {TuiMapper} from '@taiga-ui/cdk/types';
import {tuiParentAnimation} from '@taiga-ui/core/animations';
// eslint-disable-next-line no-restricted-imports
import {POLYMORPHEUS_CONTEXT} from '@taiga-ui/polymorpheus';
import {identity} from 'rxjs';

Expand Down
7 changes: 3 additions & 4 deletions projects/core/components/dialog/dialog.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
TUI_COMMON_ICONS,
} from '@taiga-ui/core/tokens';
import {tuiGetDuration} from '@taiga-ui/core/utils';
import type {PolymorpheusContent} from '@taiga-ui/polymorpheus';
import {POLYMORPHEUS_CONTEXT, PolymorpheusOutlet} from '@taiga-ui/polymorpheus';
import {injectContext, type PolymorpheusContent} from '@taiga-ui/polymorpheus';
import {PolymorpheusOutlet} from '@taiga-ui/polymorpheus';
import type {Observable} from 'rxjs';
import {filter, isObservable, map, merge, of, Subject, switchMap} from 'rxjs';

Expand Down Expand Up @@ -70,8 +70,7 @@ export class TuiDialogComponent<O, I> {

protected readonly close$ = new Subject<void>();

protected readonly context =
inject<TuiPopover<TuiDialogOptions<I>, O>>(POLYMORPHEUS_CONTEXT);
protected readonly context = injectContext<TuiPopover<TuiDialogOptions<I>, O>>();

protected readonly closeWord$ = inject(TUI_CLOSE_WORD);
protected readonly icons = inject(TUI_COMMON_ICONS);
Expand Down
4 changes: 2 additions & 2 deletions projects/core/directives/hint/hint-unstyled.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {TuiContext} from '@taiga-ui/cdk/types';
import {tuiParentAnimation} from '@taiga-ui/core/animations';
import type {TuiPortalItem} from '@taiga-ui/core/types';
import {
POLYMORPHEUS_CONTEXT,
injectContext,
PolymorpheusComponent,
PolymorpheusOutlet,
} from '@taiga-ui/polymorpheus';
Expand All @@ -25,7 +25,7 @@ import {TuiHintDirective} from './hint.directive';
host: {'[@tuiParentAnimation]': ''},
})
export class TuiHintUnstyledComponent {
protected readonly context = inject<TuiContext<TuiPortalItem>>(POLYMORPHEUS_CONTEXT);
protected readonly context = injectContext<TuiContext<TuiPortalItem>>();
}

@Directive({
Expand Down
7 changes: 3 additions & 4 deletions projects/core/directives/hint/hint.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
import {TuiPositionService, TuiVisualViewportService} from '@taiga-ui/core/services';
import {TUI_ANIMATIONS_SPEED, TUI_VIEWPORT} from '@taiga-ui/core/tokens';
import {tuiIsObscured, tuiToAnimationOptions} from '@taiga-ui/core/utils';
import type {PolymorpheusContent} from '@taiga-ui/polymorpheus';
import {POLYMORPHEUS_CONTEXT, PolymorpheusOutlet} from '@taiga-ui/polymorpheus';
import {injectContext, type PolymorpheusContent} from '@taiga-ui/polymorpheus';
import {PolymorpheusOutlet} from '@taiga-ui/polymorpheus';
import {map, takeWhile} from 'rxjs';

import {TuiHintDirective} from './hint.directive';
Expand Down Expand Up @@ -67,8 +67,7 @@ export class TuiHintComponent<C = any> {
protected readonly pointer = inject(TuiHintPointer, {optional: true});
protected readonly accessor = inject(TuiRectAccessor);

protected readonly hint =
inject<TuiContext<TuiHintDirective<C>>>(POLYMORPHEUS_CONTEXT).$implicit;
protected readonly hint = injectContext<TuiContext<TuiHintDirective<C>>>().$implicit;

protected readonly appearance =
this.hint.appearance ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {encapsulation} from '@demo/emulate/encapsulation';
import type {TuiPopover} from '@taiga-ui/cdk';
import type {TuiAlertOptions} from '@taiga-ui/core';
import {TuiAlertService, TuiButton} from '@taiga-ui/core';
import {POLYMORPHEUS_CONTEXT, PolymorpheusComponent} from '@taiga-ui/polymorpheus';
import {injectContext, PolymorpheusComponent} from '@taiga-ui/polymorpheus';
import {switchMap, takeUntil} from 'rxjs';

@Component({
Expand All @@ -30,7 +30,7 @@ import {switchMap, takeUntil} from 'rxjs';
})
export class AlertExample {
protected readonly context =
inject<TuiPopover<TuiAlertOptions<void>, boolean>>(POLYMORPHEUS_CONTEXT);
injectContext<TuiPopover<TuiAlertOptions<void>, boolean>>();
}

@Component({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {TuiAmountPipe} from '@taiga-ui/addon-commerce';
import type {TuiPopover} from '@taiga-ui/cdk';
import type {TuiAlertOptions} from '@taiga-ui/core';
import {TuiAlertService, TuiButton, TuiLink} from '@taiga-ui/core';
import {POLYMORPHEUS_CONTEXT, PolymorpheusComponent} from '@taiga-ui/polymorpheus';
import {injectContext, PolymorpheusComponent} from '@taiga-ui/polymorpheus';
import {switchMap, takeUntil} from 'rxjs';

@Component({
Expand Down Expand Up @@ -39,7 +39,7 @@ import {switchMap, takeUntil} from 'rxjs';
})
export class AlertExampleWithData {
protected readonly context =
inject<TuiPopover<TuiAlertOptions<number>, number>>(POLYMORPHEUS_CONTEXT);
injectContext<TuiPopover<TuiAlertOptions<number>, number>>();

protected value = this.context.data;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {TuiPopover} from '@taiga-ui/cdk';
import type {TuiAlertOptions} from '@taiga-ui/core';
import {TuiAlertService, TuiButton, TuiIcon} from '@taiga-ui/core';
import {
POLYMORPHEUS_CONTEXT,
injectContext,
PolymorpheusComponent,
PolymorpheusOutlet,
PolymorpheusTemplate,
Expand Down Expand Up @@ -40,7 +40,7 @@ class CustomLabel {}
})
class AlertExampleWithCustomLabel {
protected readonly context =
inject<TuiPopover<TuiAlertOptions<unknown>, boolean>>(POLYMORPHEUS_CONTEXT);
injectContext<TuiPopover<TuiAlertOptions<unknown>, boolean>>();
}

@Component({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
TuiSelectModule,
TuiTextfieldControllerModule,
} from '@taiga-ui/legacy';
import {POLYMORPHEUS_CONTEXT} from '@taiga-ui/polymorpheus';
import {injectContext} from '@taiga-ui/polymorpheus';

@Component({
standalone: true,
Expand All @@ -36,8 +36,7 @@ import {POLYMORPHEUS_CONTEXT} from '@taiga-ui/polymorpheus';
})
export class DialogExample {
private readonly dialogs = inject(TuiDialogService);
private readonly context =
inject<TuiDialogContext<number, number>>(POLYMORPHEUS_CONTEXT);
private readonly context = injectContext<TuiDialogContext<number, number>>();

protected value: number | null = null;
protected name = '';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {Component, inject} from '@angular/core';
import {Component} from '@angular/core';
import {changeDetection} from '@demo/emulate/change-detection';
import {TuiAutoFocus} from '@taiga-ui/cdk';
import type {TuiDialogContext} from '@taiga-ui/core';
import {TuiTextfield} from '@taiga-ui/core';
import {POLYMORPHEUS_CONTEXT} from '@taiga-ui/polymorpheus';
import {injectContext} from '@taiga-ui/polymorpheus';

@Component({
standalone: true,
Expand All @@ -13,7 +13,7 @@ import {POLYMORPHEUS_CONTEXT} from '@taiga-ui/polymorpheus';
changeDetection,
})
export class SearchDialogExample {
private readonly context = inject<TuiDialogContext<boolean>>(POLYMORPHEUS_CONTEXT);
private readonly context = injectContext<TuiDialogContext<boolean>>();

protected close(): void {
this.context.completeWith(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
} from '@taiga-ui/core';
import {TuiButtonLoading, TuiCheckbox} from '@taiga-ui/kit';
import {TuiAsideItemDirective} from '@taiga-ui/layout';
import {POLYMORPHEUS_CONTEXT} from '@taiga-ui/polymorpheus';
import {injectContext} from '@taiga-ui/polymorpheus';
import {BehaviorSubject, map, switchMap} from 'rxjs';

import type {AccountCard, DataForPayCardModal, FetchedCards} from '../helpers/models';
Expand Down Expand Up @@ -77,7 +77,7 @@ export class PayModal implements OnInit {
});

protected readonly context =
inject<TuiDialogContext<void, DataForPayCardModal>>(POLYMORPHEUS_CONTEXT);
injectContext<TuiDialogContext<void, DataForPayCardModal>>();

protected readonly iOS = inject(TUI_IS_IOS);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {NgForOf} from '@angular/common';
import {ChangeDetectionStrategy, Component, inject} from '@angular/core';
import {ChangeDetectionStrategy, Component} from '@angular/core';
import type {TuiPopover} from '@taiga-ui/cdk';
import {TuiButton} from '@taiga-ui/core';
import type {TuiPdfViewerOptions} from '@taiga-ui/kit';
import {POLYMORPHEUS_CONTEXT} from '@taiga-ui/polymorpheus';
import {injectContext} from '@taiga-ui/polymorpheus';

import type {Buttons} from '../index';

Expand All @@ -15,5 +15,5 @@ import type {Buttons} from '../index';
})
export class ActionsContent {
protected readonly context =
inject<TuiPopover<TuiPdfViewerOptions<Buttons>, string>>(POLYMORPHEUS_CONTEXT);
injectContext<TuiPopover<TuiPdfViewerOptions<Buttons>, string>>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {changeDetection} from '@demo/emulate/change-detection';
import type {TuiPopover} from '@taiga-ui/cdk';
import {TuiButton, TuiDialogCloseService} from '@taiga-ui/core';
import {
POLYMORPHEUS_CONTEXT,
injectContext,
PolymorpheusOutlet,
PolymorpheusTemplate,
} from '@taiga-ui/polymorpheus';
Expand All @@ -21,8 +21,7 @@ import type {PromptOptions} from './prompt-options';
providers: [TuiDialogCloseService],
})
export class Prompt {
protected readonly context =
inject<TuiPopover<PromptOptions, boolean>>(POLYMORPHEUS_CONTEXT);
protected readonly context = injectContext<TuiPopover<PromptOptions, boolean>>();

// Here you get options + content + id + observer
constructor() {
Expand Down
7 changes: 2 additions & 5 deletions projects/kit/components/confirm/confirm.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import {TUI_IS_MOBILE} from '@taiga-ui/cdk/tokens';
import {TuiButton} from '@taiga-ui/core/components/button';
import type {TuiDialogContext} from '@taiga-ui/core/components/dialog';
import {TUI_CONFIRM_WORDS} from '@taiga-ui/kit/tokens';
import type {PolymorpheusContent} from '@taiga-ui/polymorpheus';
import {injectContext, type PolymorpheusContent} from '@taiga-ui/polymorpheus';
import {
POLYMORPHEUS_CONTEXT,
PolymorpheusComponent,
PolymorpheusOutlet,
PolymorpheusTemplate,
Expand Down Expand Up @@ -39,9 +38,7 @@ export class TuiConfirm {
protected readonly words$ = inject(TUI_CONFIRM_WORDS);

protected readonly context =
inject<TuiDialogContext<boolean, TuiConfirmData | undefined>>(
POLYMORPHEUS_CONTEXT,
);
injectContext<TuiDialogContext<boolean, TuiConfirmData | undefined>>();

protected get appearance(): string {
return this.isMobile ? 'secondary' : 'flat';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {tuiPure} from '@taiga-ui/cdk/utils/miscellaneous';
import {TuiLink} from '@taiga-ui/core/components/link';
import {TuiBreakpointService} from '@taiga-ui/core/services';
import {TUI_INPUT_FILE_TEXTS} from '@taiga-ui/kit/tokens';
import {POLYMORPHEUS_CONTEXT} from '@taiga-ui/polymorpheus';
import {injectContext} from '@taiga-ui/polymorpheus';
import type {Observable} from 'rxjs';
import {combineLatest, map, of} from 'rxjs';

Expand All @@ -24,7 +24,7 @@ import {TuiInputFiles} from './input-files.component';
export class TuiInputFilesContent {
private readonly breakpoint$ = inject(TuiBreakpointService);
private readonly text$ = inject(TUI_INPUT_FILE_TEXTS);
private readonly context = inject(POLYMORPHEUS_CONTEXT) as TuiContext<boolean>;
private readonly context = injectContext<TuiContext<boolean>>();
private readonly component = inject(TuiInputFiles);

protected get link$(): Observable<string> {
Expand Down
5 changes: 2 additions & 3 deletions projects/kit/components/pdf-viewer/pdf-viewer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '@taiga-ui/core/tokens';
import {tuiToAnimationOptions} from '@taiga-ui/core/utils/miscellaneous';
import {
POLYMORPHEUS_CONTEXT,
injectContext,
PolymorpheusOutlet,
PolymorpheusTemplate,
} from '@taiga-ui/polymorpheus';
Expand All @@ -35,8 +35,7 @@ export class TuiPdfViewerComponent<I, O> {
protected readonly options = tuiToAnimationOptions(inject(TUI_ANIMATIONS_SPEED));
protected readonly closeWord$ = inject(TUI_CLOSE_WORD);
protected readonly icons = inject(TUI_COMMON_ICONS);
protected readonly context =
inject<TuiPopover<TuiPdfViewerOptions<I>, O>>(POLYMORPHEUS_CONTEXT);
protected readonly context = injectContext<TuiPopover<TuiPdfViewerOptions<I>, O>>();

protected onKeyDownEsc(): void {
this.context.$implicit.complete();
Expand Down
Loading

0 comments on commit 6f28012

Please sign in to comment.