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

feat(cdk)!: refactor the way new custom dialogs are created #6660

Merged
merged 2 commits into from
Feb 2, 2024
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
1 change: 0 additions & 1 deletion projects/addon-mobile/components/mobile-dialog/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ export * from './mobile-dialog.component';
export * from './mobile-dialog.module';
export * from './mobile-dialog.options';
export * from './mobile-dialog.service';
export * from './mobile-dialog-options';

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {ChangeDetectionStrategy, Component, Inject} from '@angular/core';
import {TUI_IS_IOS, TuiDialog} from '@taiga-ui/cdk';
import {POLYMORPHEUS_CONTEXT} from '@tinkoff/ng-polymorpheus';

import {TuiMobileDialogOptions} from './mobile-dialog-options';
import {TuiMobileDialogOptions} from './mobile-dialog.options';

@Component({
selector: 'tui-mobile-dialog',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import {Provider} from '@angular/core';
import {tuiCreateToken, tuiProvideOptions} from '@taiga-ui/cdk';

import {TuiMobileDialogOptions} from './mobile-dialog-options';

type TuiMobileDialogDefaultOptions = Omit<TuiMobileDialogOptions<unknown>, 'data'>;
export interface TuiMobileDialogOptions<I = undefined> {
readonly actions: readonly string[];
readonly data: I;
readonly label: string;
}

export const TUI_MOBILE_DIALOG_DEFAULT_OPTIONS: TuiMobileDialogDefaultOptions = {
export const TUI_MOBILE_DIALOG_DEFAULT_OPTIONS: TuiMobileDialogOptions = {
label: '',
actions: ['OK'],
data: undefined,
};

/**
Expand All @@ -18,7 +21,7 @@ export const TUI_MOBILE_DIALOG_OPTIONS = tuiCreateToken(
);

export function tuiMobileDialogOptionsProvider(
options: Partial<TuiMobileDialogDefaultOptions>,
options: Partial<TuiMobileDialogOptions<unknown>>,
): Provider {
return tuiProvideOptions(
TUI_MOBILE_DIALOG_OPTIONS,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
import {inject, Injectable} from '@angular/core';
import {TuiBaseDialogContext, TuiPopoverService} from '@taiga-ui/cdk';
import {TUI_DIALOGS} from '@taiga-ui/core';
import {PolymorpheusComponent, PolymorpheusContent} from '@tinkoff/ng-polymorpheus';
import {PolymorpheusContent} from '@tinkoff/ng-polymorpheus';
import {Observable} from 'rxjs';

import {TuiMobileDialogComponent} from './mobile-dialog.component';
import {TUI_MOBILE_DIALOG_OPTIONS} from './mobile-dialog.options';
import {TuiMobileDialogOptions} from './mobile-dialog-options';
import {TUI_MOBILE_DIALOG_OPTIONS, TuiMobileDialogOptions} from './mobile-dialog.options';

@Injectable({
providedIn: 'root',
useFactory: () =>
new TuiMobileDialogService(
TUI_DIALOGS,
TuiMobileDialogComponent,
inject(TUI_MOBILE_DIALOG_OPTIONS),
),
})
export class TuiMobileDialogService extends TuiPopoverService<
TuiMobileDialogOptions<any>,
number
> {
protected readonly items$ = inject(TUI_DIALOGS);
protected readonly component = new PolymorpheusComponent(TuiMobileDialogComponent);
protected readonly options = {
...inject(TUI_MOBILE_DIALOG_OPTIONS),
data: undefined,
};

override open(
content: PolymorpheusContent<
TuiBaseDialogContext<number> & TuiMobileDialogOptions<any>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {Component, DebugElement} from '@angular/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {TuiIdService} from '@taiga-ui/cdk';
import {TuiRootModule} from '@taiga-ui/core';
import {TuiPageObject} from '@taiga-ui/testing';

Expand Down Expand Up @@ -31,14 +30,7 @@ describe('Mobile Dialog with TUI_MOBILE_DIALOG_OPTIONS', () => {
TestBed.configureTestingModule({
imports: [NoopAnimationsModule, TuiRootModule, TuiMobileDialogModule],
declarations: [TestComponent],
providers: [
tuiMobileDialogOptionsProvider({label}),
{
provide: TuiMobileDialogService,
useFactory: () =>
new TuiMobileDialogService(TestBed.inject(TuiIdService)),
},
],
providers: [tuiMobileDialogOptionsProvider({label})],
});
await TestBed.compileComponents();
fixture = TestBed.createComponent(TestComponent);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Directive} from '@angular/core';
import {TuiPopoverDirective, TuiPopoverService} from '@taiga-ui/cdk';
import {tuiAsPopover, TuiPopoverDirective} from '@taiga-ui/cdk';

import {TuiSheetDialogOptions} from './sheet-dialog.options';
import {TuiSheetDialogService} from './sheet-dialog.service';
Expand All @@ -8,11 +8,6 @@ import {TuiSheetDialogService} from './sheet-dialog.service';
selector: 'ng-template[tuiSheetDialog]',
inputs: ['options: tuiSheetDialogOptions', 'open: tuiSheetDialog'],
outputs: ['openChange: tuiSheetDialogChange'],
providers: [
{
provide: TuiPopoverService,
useExisting: TuiSheetDialogService,
},
],
providers: [tuiAsPopover(TuiSheetDialogService)],
})
export class TuiSheetDialogDirective extends TuiPopoverDirective<TuiSheetDialogOptions> {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import {Provider} from '@angular/core';
import {tuiCreateToken, tuiProvideOptions} from '@taiga-ui/cdk';
import {PolymorpheusContent} from '@tinkoff/ng-polymorpheus';

type TuiSheetDialogDefaultOptions = Omit<TuiSheetDialogOptions<unknown>, 'data'>;

export interface TuiSheetDialogOptions<I = never> {
export interface TuiSheetDialogOptions<I = undefined> {
readonly closeable: boolean;
readonly data: I;
readonly initial: number;
Expand All @@ -13,12 +11,13 @@ export interface TuiSheetDialogOptions<I = never> {
readonly stops: readonly string[];
}

export const TUI_SHEET_DIALOG_DEFAULT_OPTIONS: TuiSheetDialogDefaultOptions = {
export const TUI_SHEET_DIALOG_DEFAULT_OPTIONS: TuiSheetDialogOptions = {
label: '',
stops: [],
initial: 0,
offset: 16,
closeable: true,
data: undefined,
};

/**
Expand All @@ -27,7 +26,7 @@ export const TUI_SHEET_DIALOG_DEFAULT_OPTIONS: TuiSheetDialogDefaultOptions = {
export const TUI_SHEET_DIALOG_OPTIONS = tuiCreateToken(TUI_SHEET_DIALOG_DEFAULT_OPTIONS);

export function tuiSheetDialogOptionsProvider(
options: Partial<TuiSheetDialogDefaultOptions>,
options: Partial<TuiSheetDialogOptions>,
): Provider {
return tuiProvideOptions(
TUI_SHEET_DIALOG_OPTIONS,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import {inject, Injectable} from '@angular/core';
import {TuiPopoverService} from '@taiga-ui/cdk';
import {TUI_DIALOGS} from '@taiga-ui/core';
import {PolymorpheusComponent} from '@tinkoff/ng-polymorpheus';

import {TuiSheetDialogComponent} from './sheet-dialog.component';
import {TUI_SHEET_DIALOG_OPTIONS, TuiSheetDialogOptions} from './sheet-dialog.options';

@Injectable({
providedIn: 'root',
useFactory: () =>
new TuiSheetDialogService(
TUI_DIALOGS,
TuiSheetDialogComponent,
inject(TUI_SHEET_DIALOG_OPTIONS),
),
})
export class TuiSheetDialogService extends TuiPopoverService<TuiSheetDialogOptions<any>> {
protected readonly items$ = inject(TUI_DIALOGS);
protected readonly component = new PolymorpheusComponent(TuiSheetDialogComponent);
protected readonly options = {
...inject(TUI_SHEET_DIALOG_OPTIONS),
data: undefined,
};
}
export class TuiSheetDialogService extends TuiPopoverService<
TuiSheetDialogOptions<any>
> {}
4 changes: 2 additions & 2 deletions projects/addon-mobile/const/mobile-alert-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const MOBILE_ALERT_OPTIONS: TuiMobileAlertOptions = {

export type TuiMobileAlertOptions = Omit<
TuiDialogOptions<unknown>,
'data' | 'dismissible' | 'header' | 'label' | 'required'
'appearance' | 'data' | 'dismissible' | 'header' | 'label' | 'required'
>;

export type TuiIosAlertOptions = Omit<
TuiDialogOptions<unknown>,
'data' | 'header' | 'label' | 'required'
'appearance' | 'data' | 'header' | 'label' | 'required'
>;

export const TUI_IOS_ALERT_OPTIONS: TuiIosAlertOptions = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import {inject, Injectable} from '@angular/core';
import {Injectable} from '@angular/core';
import {TuiPopoverService} from '@taiga-ui/cdk';
import {TUI_DIALOGS} from '@taiga-ui/core';
import {PolymorpheusComponent} from '@tinkoff/ng-polymorpheus';

import {TuiPreviewDialogComponent} from './preview-dialog.component';

@Injectable({providedIn: 'root'})
export class TuiPreviewDialogService extends TuiPopoverService<unknown> {
protected readonly items$ = inject(TUI_DIALOGS);
protected readonly options = {};
protected readonly component = new PolymorpheusComponent(TuiPreviewDialogComponent);
}
@Injectable({
providedIn: 'root',
useFactory: () => new TuiPreviewDialogService(TUI_DIALOGS, TuiPreviewDialogComponent),
})
export class TuiPreviewDialogService extends TuiPopoverService<unknown> {}
36 changes: 23 additions & 13 deletions projects/cdk/services/popover.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {inject, Injectable} from '@angular/core';
import {inject, Injectable, Provider, ProviderToken, Type} from '@angular/core';
import {TuiContext} from '@taiga-ui/cdk/interfaces';
import {PolymorpheusComponent, PolymorpheusContent} from '@tinkoff/ng-polymorpheus';
import {BehaviorSubject, Observable, Observer} from 'rxjs';
Expand All @@ -18,34 +18,37 @@ export type TuiPopover<T, O> = T &
};

@Injectable()
// eslint-disable-next-line @typescript-eslint/naming-convention
export abstract class TuiPopoverService<T, K = void> {
protected abstract readonly component: PolymorpheusComponent<any>;
protected abstract readonly options: T;
protected abstract readonly items$: BehaviorSubject<
ReadonlyArray<TuiPopover<T, any>>
>;

private readonly component: PolymorpheusComponent<any>;
private readonly items$: BehaviorSubject<ReadonlyArray<TuiPopover<T, any>>>;
private readonly id = inject(TuiIdService);

constructor(
items: ProviderToken<BehaviorSubject<ReadonlyArray<TuiPopover<T, any>>>>,
component: Type<any>,
protected readonly options: T = {} as T,
) {
this.component = new PolymorpheusComponent(component);
this.items$ = inject(items);
}

open<G = void>(
content: PolymorpheusContent<T & TuiPopoverContext<K extends void ? G : K>>,
options: Partial<T> = {},
): Observable<K extends void ? G : K> {
return new Observable(observer => {
const completeWith = (result: K extends void ? G : K): void => {
observer.next(result);
observer.complete();
};
const item = {
...this.options,
...options,
content,
completeWith,
$implicit: observer,
component: this.component,
createdAt: Date.now(),
id: this.id.generate(),
completeWith: (result: K extends void ? G : K): void => {
observer.next(result);
observer.complete();
},
};

this.items$.next([...this.items$.value, item]);
Expand All @@ -56,3 +59,10 @@ export abstract class TuiPopoverService<T, K = void> {
});
}
}

export function tuiAsPopover(useExisting: Type<TuiPopoverService<any>>): Provider {
return {
provide: TuiPopoverService,
useExisting,
};
}
1 change: 0 additions & 1 deletion projects/core/abstract/driver.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {ExistingProvider, Type} from '@angular/core';
import {Observable} from 'rxjs';

// eslint-disable-next-line @typescript-eslint/naming-convention
export abstract class TuiDriver extends Observable<boolean> {
abstract readonly type: string;
}
Expand Down
3 changes: 1 addition & 2 deletions projects/core/abstract/position-accessor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {ExistingProvider, FactoryProvider, Optional, SkipSelf, Type} from '@angular/core';
import {TuiPoint} from '@taiga-ui/core/types';

// eslint-disable-next-line @typescript-eslint/naming-convention
export abstract class TuiPositionAccessor {
abstract readonly type: string;
abstract getPosition(rect: ClientRect): TuiPoint;
abstract getPosition(rect: DOMRect): TuiPoint;
}

export function tuiPositionAccessorFor(
Expand Down
1 change: 0 additions & 1 deletion projects/core/abstract/rect-accessor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {ExistingProvider, FactoryProvider, SkipSelf, Type} from '@angular/core';

// TODO: Rename to getBoundingClientRect to match the DOM API
// eslint-disable-next-line @typescript-eslint/naming-convention
export abstract class TuiRectAccessor {
abstract readonly type: string;
abstract getClientRect(): DOMRect;
Expand Down
1 change: 0 additions & 1 deletion projects/core/abstract/vehicle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {ExistingProvider, Type} from '@angular/core';

// eslint-disable-next-line @typescript-eslint/naming-convention
export abstract class TuiVehicle {
abstract readonly type: string;
abstract toggle(value: boolean): void;
Expand Down
3 changes: 1 addition & 2 deletions projects/core/components/dialog/dialog.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ import {
TuiDialog,
} from '@taiga-ui/cdk';
import {tuiFadeIn, tuiSlideInTop} from '@taiga-ui/core/animations';
import {TuiDialogOptions} from '@taiga-ui/core/interfaces';
import {
TUI_ANIMATIONS_SPEED,
TUI_CLOSE_WORD,
TUI_COMMON_ICONS,
TuiCommonIcons,
} from '@taiga-ui/core/tokens';
import {TuiDialogSize} from '@taiga-ui/core/types';
import {tuiGetDuration} from '@taiga-ui/core/utils';
import {POLYMORPHEUS_CONTEXT, PolymorpheusContent} from '@tinkoff/ng-polymorpheus';
import {
Expand All @@ -35,6 +33,7 @@ import {
takeUntil,
} from 'rxjs';

import {TuiDialogOptions, TuiDialogSize} from './dialog.interfaces';
import {TUI_DIALOGS_CLOSE} from './dialog.tokens';
import {TuiDialogCloseService} from './dialog-close.service';

Expand Down
11 changes: 3 additions & 8 deletions projects/core/components/dialog/dialog.directive.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import {Directive} from '@angular/core';
import {TuiPopoverDirective, TuiPopoverService} from '@taiga-ui/cdk';
import {TuiDialogOptions} from '@taiga-ui/core/interfaces';
import {tuiAsPopover, TuiPopoverDirective} from '@taiga-ui/cdk';

import {TuiDialogOptions} from './dialog.interfaces';
import {TuiDialogService} from './dialog.service';

@Directive({
selector: 'ng-template[tuiDialog]',
inputs: ['options: tuiDialogOptions', 'open: tuiDialog'],
outputs: ['openChange: tuiDialogChange'],
providers: [
{
provide: TuiPopoverService,
useExisting: TuiDialogService,
},
],
providers: [tuiAsPopover(TuiDialogService)],
})
export class TuiDialogDirective<T> extends TuiPopoverDirective<TuiDialogOptions<T>> {}
Loading
Loading