File

src/app/dialogs/remove-dialog/remove-dialog.component.ts

Description

This component is a confirmation dialog for anything that involves deleting

Example

// ...

constructor(private dialog: MatDialog, ...)

// ...

const dialogRef = this.dialog.open(RemoveDialogComponent,
{
  data: {
    title: 'This is the title of the dialog',
    messages: ['Every entry is a new line',
               'This is the next line',
               ' Lines beginning with a space will be indented'],
    verificationName: 'In order for the user to be able to click the confirm button, this text has to be typed',
    confirmButtonText: 'This is the text of the confirm button'
  }
});

dialogRef.afterClosed().subscribe(confirm => {
  if (confirm) {
    console.log('Deleted!');
  } else {
    console.log('Not deleted!');
  }
});

Implements

OnInit

Metadata

selector app-remove-dialog
styleUrls ./remove-dialog.component.scss
templateUrl ./remove-dialog.component.html

Index

Properties
Methods

Constructor

constructor(dialogRef: MatDialogRef<RemoveDialogComponent | boolean>, data: DialogData)
Parameters :
Name Type Optional
dialogRef MatDialogRef<RemoveDialogComponent | boolean> No
data DialogData No

Methods

onDeleteClick
onDeleteClick()
Returns : void
onNoClick
onNoClick()
Returns : void

Properties

Public data
Type : DialogData
Decorators :
@Inject(MAT_DIALOG_DATA)
Public dialogRef
Type : MatDialogRef<RemoveDialogComponent | boolean>
matchValidator
Type : ValidatorFn
Default value : () => {...}
verificationNameInput
Default value : new FormControl('', this.matchValidator)
import {Component, Inject, OnInit} from '@angular/core';
import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog';
import {FormControl, ValidatorFn} from '@angular/forms';

/**
 * This component is a confirmation dialog for anything that involves deleting
 *
 * #### Example
 * ```ts
 * // ...
 *
 * constructor(private dialog: MatDialog, ...)
 *
 * // ...
 *
 * const dialogRef = this.dialog.open(RemoveDialogComponent,
 * {
 *   data: {
 *     title: 'This is the title of the dialog',
 *     messages: ['Every entry is a new line',
 *                'This is the next line',
 *                ' Lines beginning with a space will be indented'],
 *     verificationName: 'In order for the user to be able to click the confirm button, this text has to be typed',
 *     confirmButtonText: 'This is the text of the confirm button'
 *   }
 * });
 *
 * dialogRef.afterClosed().subscribe(confirm => {
 *   if (confirm) {
 *     console.log('Deleted!');
 *   } else {
 *     console.log('Not deleted!');
 *   }
 * });
 * ```
 */
@Component({
  selector: 'app-remove-dialog',
  templateUrl: './remove-dialog.component.html',
  styleUrls: ['./remove-dialog.component.scss']
})
export class RemoveDialogComponent implements OnInit {
  matchValidator: ValidatorFn = (control) => {
    return control.value === this.data.verificationName ? null : {"Names don't match": true};
  };

  verificationNameInput = new FormControl('', this.matchValidator);

  constructor(public dialogRef: MatDialogRef<RemoveDialogComponent, boolean>, @Inject(MAT_DIALOG_DATA) public data: DialogData) {}

  ngOnInit(): void {
    this.verificationNameInput.markAsTouched();
  }

  onNoClick(): void {
    this.dialogRef.close(false);
  }

  onDeleteClick(): void {
    this.dialogRef.close(true);
  }
}

export interface DialogData {
  title: string;
  /** The list of lines shown in the dialog. If a line starts with a space, the line will be indented */
  messages: Array<string>;
  /** If set, shows a text box that forces the user to type the specified text before being able to click the confirm button */
  verificationName?: string;
  /** If set, shows this text as the text in the confirm button. If not set, button shows 'Delete' */
  confirmButtonText?: string;
}
<div mat-dialog-title class="title">
  <mat-icon style="font-size: 60px; height: 60px; width: 60px">delete</mat-icon>
  {{ data.title }}
</div>

<div mat-dialog-content cdkFocusInitial>
  <div class="content">
    <ng-container *ngFor="let message of data.messages">
      <ng-container *ngIf="message.startsWith(' ')">&nbsp;&nbsp;&nbsp;&nbsp;</ng-container>
      {{ message.startsWith(" ") ? message.substring(1) : message }}<br />
    </ng-container>
  </div>
  <div style="flex: 0 0 auto">
    <div *ngIf="data.verificationName; else spacing" style="margin-top: 20px">
      Type &quot;<b>{{ data.verificationName }}</b>&quot; to confirm:
      <mat-form-field style="width: 100%; margin-top: 4px" appearance="outline">
        <mat-label>Confirm name</mat-label>
        <input required matInput [formControl]="verificationNameInput" autocomplete="off" />
        <mat-error *ngIf="verificationNameInput.invalid">Names don't match!</mat-error>
      </mat-form-field>
    </div>

    <ng-template #spacing>
      <div style="height: 26px"></div>
    </ng-template>
  </div>
</div>

<div mat-dialog-actions>
  <button mat-raised-button (click)="onNoClick()">Cancel</button>
  <button mat-raised-button color="warn" (click)="onDeleteClick()" [disabled]="data.verificationName && verificationNameInput.invalid">
    {{ this.data.confirmButtonText ? this.data.confirmButtonText : "Delete" }}
  </button>
</div>

./remove-dialog.component.scss

.mat-dialog-content {
  display: flex;
  flex-direction: column;
}

.content {
  display: inline-block;
  align-self: center;
  place-self: center;
  vertical-align: middle;
  border-top: lightgray;
  border-top-style: solid;
  border-bottom: lightgray;
  border-bottom-style: solid;
  font-weight: bold;
  font-size: 1.1em;
  padding: 8px;
  margin: 0;
  width: 100%;
  overflow: auto;
  flex: 1 1 auto;
  white-space: nowrap;
}

.title {
  display: flex;
  flex-direction: row;
  align-items: center;
}

.delete-icon {
  flex: 0 0 auto;
}

.mat-success {
  background-color: green;
  color: #fff;
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""