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: create simple dialog component #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
18,503 changes: 18,482 additions & 21 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
},
"dependencies": {
"@angular/animations": "~13.3.0",
"@angular/cdk": "~13.3.0",
"@angular/common": "~13.3.0",
"@angular/compiler": "~13.3.0",
"@angular/core": "~13.3.0",
"@angular/forms": "~13.3.0",
"@angular/platform-browser": "~13.3.0",
"@angular/platform-browser-dynamic": "~13.3.0",
"@angular/router": "~13.3.0",
"@tylertech/angular-core": "^1.6.0",
"@tylertech/forge": "2.0.0-rc.2",
"@tylertech/tyler-icons": "^1.12.0",
"rxjs": "~7.5.0",
Expand Down
4 changes: 3 additions & 1 deletion projects/forge-angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
"license": "Apache-2.0",
"version": "0.0.0",
"peerDependencies": {
"@angular/cdk": "^13.3.0",
"@angular/common": "^13.3.0",
"@angular/core": "^13.3.0",
"@tylertech/angular-core": "^1.6.0",
"@tylertech/forge": "^2.0.0-rc.0"
},
"dependencies": {
"tslib": "^2.3.0"
}
}
}
15 changes: 15 additions & 0 deletions projects/forge-angular/src/lib/dialog/dialog.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DialogRef } from './dialog-ref';
import { DialogInjector } from './dialog-injector';
import { DynamicComponentService } from '../core/dynamic-component/dynamic-component.service';
import { IDynamicComponentRef } from '../core/dynamic-component/dynamic-component.service';
import { ForgeSimpleDialogComponent, SimpleDialogConfig } from '../simple-dialog';

export interface IDialogOptions extends Omit<Partial<IDialogComponent>, 'attributes'> {
dialogClass?: string;
Expand Down Expand Up @@ -81,6 +82,20 @@ export class DialogService {
return dialogRef;
}

public showSimple<T = void>(config: SimpleDialogConfig, options?: IDialogOptions): Promise<T> {
const dialogConfig: DialogConfig<SimpleDialogConfig> = {
data: config
};
const dialogRef = this.show(ForgeSimpleDialogComponent, options, dialogConfig);

return new Promise(resolve => {
const sub = dialogRef.afterClosed.subscribe((result: T) => {
sub.unsubscribe();
resolve(result);
});
});
}

/**
* Removes a dialog from the DOM and destroys the component instance.
* @param dialogInstance An instance of a Forge dialog element.
Expand Down
3 changes: 3 additions & 0 deletions projects/forge-angular/src/lib/forge.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ForgeDatePickerModule } from './date-picker/date-picker.module';
import { ForgePopupModule } from './popup/popup.module';
import { ForgeSelectModule } from './select/select.module';
import { ForgeSliderModule } from './slider/slider.module';
import { ForgeSimpleDialogModule } from './simple-dialog';
import { ForgeSwitchModule } from './switch';
import { ForgeTimePickerModule } from './time-picker/time-picker.module';

Expand All @@ -20,6 +21,7 @@ import { ForgeTimePickerModule } from './time-picker/time-picker.module';
ForgeDateRangePickerModule,
ForgePopupModule,
ForgeSelectModule,
ForgeSimpleDialogModule,
ForgeSliderModule,
ForgeSwitchModule,
ForgeTimePickerModule
Expand All @@ -32,6 +34,7 @@ import { ForgeTimePickerModule } from './time-picker/time-picker.module';
ForgeDateRangePickerModule,
ForgePopupModule,
ForgeSelectModule,
ForgeSimpleDialogModule,
ForgeSliderModule,
ForgeSwitchModule,
ForgeTimePickerModule
Expand Down
3 changes: 3 additions & 0 deletions projects/forge-angular/src/lib/simple-dialog/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './simple-dialog.component';
export * from './simple-dialog.module';
export * from './simple-dialog-constants';
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Observable } from 'rxjs';

export class SimpleDialogConfig {
title: string;
message: string;
confirmButtonConfig?: SimpleDialogButtonConfig;
cancelButtonConfig?: SimpleDialogButtonConfig;
toolbarBorders?: boolean;
preventDismiss?: boolean;
dismissReturnValue?: any;
focus?: SimpleDialogFocus;
errorMode?: SimpleDialogErrorMode;
alert?: boolean;
id?: string;
}

export class SimpleDialogButtonConfig {
label: string;
returnValue?: any;
handler?: Observable<unknown>;
}

export class SimpleDialogError {
error: any;
}

export type SimpleDialogFocus = 'auto' | 'dismiss-button' | 'cancel-button' | 'confirm-button';

export type SimpleDialogErrorMode = 'ignore' | 'close';
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<forge-scaffold class="simple-dialog" [ngClass]="{'simple-dialog--no-borders': !config.toolbarBorders}" cdkTrapFocus [cdkTrapFocusAutoCapture]="true">
<forge-toolbar slot="header">
<h1 slot="start" class="forge-typography--headline5" id="{{ config.id }}-title">{{ config.title }}</h1>
<forge-icon-button *ngIf="!config.preventDismiss" slot="end">
<button type="button" aria-label="Close dialog" [tylAutofocus]="config.focus === 'dismiss-button'" [disabled]="isConfirming || isCanceling" (click)="onDismiss()">
<forge-icon name="close"></forge-icon>
</button>
<forge-tooltip>Close dialog</forge-tooltip>
</forge-icon-button>
</forge-toolbar>
<div slot="body" id="{{ config.id }}-message" [innerHTML]="config.message"></div>
<forge-toolbar slot="footer" inverted>
<forge-button *ngIf="config.cancelButtonConfig as button" slot="end" type="outlined">
<button type="button" [tylAutofocus]="config.focus === 'cancel-button'" [disabled]="isConfirming || isCanceling" (click)="onCancel()">{{ button.label }}</button>
</forge-button>
<forge-button slot="end" type="raised">
<button type="button" [tylAutofocus]="config.focus === 'confirm-button'" [disabled]="isConfirming || isCanceling" (click)="onConfirm()">{{ config.confirmButtonConfig?.label ?? 'Done' }}</button>
</forge-button>
</forge-toolbar>
</forge-scaffold>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.simple-dialog {
&--no-borders {
--forge-toolbar-border-bottom: none;

[slot=footer] {
&::part(root) {
border-top: none;
}
}
}

[slot=header] {
[slot=end] {
margin-right: -8px;
}
}

[slot=body] {
padding: 16px;
overflow: auto;
}

[slot=footer] {
forge-button {
margin-left: 16px;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { Component, Renderer2 } from '@angular/core';
import { randomChars } from '@tylertech/forge-core';
import { first } from 'rxjs/operators';

import { DialogConfig } from '../dialog/dialog-config';
import { DialogRef } from '../dialog/dialog-ref';
import { SimpleDialogConfig, SimpleDialogError } from './simple-dialog-constants';

@Component({
selector: 'forge-simple-dialog',
templateUrl: './simple-dialog.component.html',
styleUrls: ['./simple-dialog.component.scss']
})

export class ForgeSimpleDialogComponent {
public config: SimpleDialogConfig;
public isConfirming = false;
public isCanceling = false;

constructor(private _dialogConfig: DialogConfig<SimpleDialogConfig>, private _dialogRef: DialogRef, private _renderer: Renderer2) {
this._setConfig(_dialogConfig?.data);
this._setBeforeCloseCallback();
this._setAriaAttributes();
}

public onDismiss(): void {
this._dialogRef.close(this.config.dismissReturnValue ?? null);
}

public onCancel(): void {
if (this.config.cancelButtonConfig?.handler) {
this.isCanceling = true;
this.config.cancelButtonConfig.handler.pipe(first()).subscribe({
next: result => this._dialogRef.close(result),
error: error => {
this.isCanceling = false;
this._handleError(error);
}
});
} else {
this._dialogRef.close(this.config.cancelButtonConfig?.returnValue ?? true);
}
}

public onConfirm(): void {
if (this.config.confirmButtonConfig?.handler) {
this.isConfirming = true;
this.config.confirmButtonConfig.handler.pipe(first()).subscribe({
next: result => this._dialogRef.close(result),
error: error => {
this.isConfirming = false;
this._handleError(error);
}
});
} else {
this._dialogRef.close(this.config.confirmButtonConfig?.returnValue ?? true);
}
}

private _setConfig(config?: SimpleDialogConfig): void {
this.config = {
title: config?.title ?? 'Alert',
message: config?.message ?? '',
confirmButtonConfig: config?.confirmButtonConfig ?? { label: 'Done' },
cancelButtonConfig: config?.cancelButtonConfig,
toolbarBorders: config?.toolbarBorders ?? false,
preventDismiss: config?.preventDismiss ?? false,
dismissReturnValue: config?.dismissReturnValue ?? null,
focus: config?.focus ?? 'auto',
errorMode: config?.errorMode ?? 'close',
alert: config?.alert ?? false,
id: config?.id ?? `forge-simple-dialog-${randomChars(7)}`
}
}

private _handleError(error: any): void {
switch (this.config.errorMode) {
case 'close':
this._dialogRef.close({ error } as SimpleDialogError);
break;
default:
break;
}
}

private _setBeforeCloseCallback(): void {
// Intercept a close on escape or backdrop click
this._dialogRef.nativeElement.beforeCloseCallback = () => {
if (this.isConfirming || this.isCanceling) {
// Stop all attempts to close while the dialog is busy
return false;
}
if (!this.config.preventDismiss) {
// If dismissing is allowed, close
this.onDismiss();
} else if (this.config.cancelButtonConfig) {
// If dismissing isn't allowed but canceling is (e.g. an explicit response is desired), close
this.onCancel();
}
// Otherwise keep the dialog open - an explicit confirmation is needed
return false;
};
}

private _setAriaAttributes(): void {
if (this.config.alert) {
this._renderer.setAttribute(this._dialogRef.nativeElement, 'role', 'alertdialog');
}
this._renderer.setAttribute(this._dialogRef.nativeElement, 'aria-labelledby', `${this.config.id}-title`)
this._renderer.setAttribute(this._dialogRef.nativeElement, 'aria-describedby', `${this.config.id}-message`)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { CUSTOM_ELEMENTS_SCHEMA, NgModule } from '@angular/core';
import { A11yModule } from '@angular/cdk/a11y'
import { defineButtonComponent, defineIconButtonComponent, defineIconComponent, defineScaffoldComponent, defineToolbarComponent, defineTooltipComponent, IconRegistry } from '@tylertech/forge';
import { tylIconClose } from '@tylertech/tyler-icons/standard';

import { CommonModule } from '@angular/common';
import { AutofocusModule } from '@tylertech/angular-core';

import { ForgeSimpleDialogComponent } from './simple-dialog.component';

@NgModule({
declarations: [ForgeSimpleDialogComponent],
exports: [ForgeSimpleDialogComponent],
imports: [CommonModule, A11yModule, AutofocusModule],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class ForgeSimpleDialogModule {
constructor() {
defineButtonComponent();
defineIconButtonComponent();
defineIconComponent();
defineScaffoldComponent();
defineToolbarComponent();
defineTooltipComponent();

IconRegistry.define([
tylIconClose
]);
}
}
1 change: 1 addition & 0 deletions projects/forge-angular/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from './lib/date-range-picker';
export * from './lib/dialog';
export * from './lib/popup';
export * from './lib/select';
export * from './lib/simple-dialog';
export * from './lib/slider';
export * from './lib/switch';
export * from './lib/toast';
Expand Down
Loading