Browse Source

Merge pull request #18737 from abpframework/feat/toaster-icon-class

Add `iconClass` option to toaster
pull/18773/head
Mahmut Gundogdu 3 years ago
committed by GitHub
parent
commit
4086ccb48c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      docs/en/UI/Angular/Toaster-Service.md
  2. 6
      npm/ng-packs/packages/theme-shared/src/lib/components/toast/toast.component.ts
  3. 1
      npm/ng-packs/packages/theme-shared/src/lib/models/toaster.ts
  4. 23
      npm/ng-packs/packages/theme-shared/src/lib/tests/toaster.service.spec.ts

4
docs/en/UI/Angular/Toaster-Service.md

@ -47,7 +47,8 @@ const options: Partial<Toaster.ToastOptions> = {
closable: true, closable: true,
tapToDismiss: true, tapToDismiss: true,
messageLocalizationParams: ['Demo', '1'], messageLocalizationParams: ['Demo', '1'],
titleLocalizationParams: [] titleLocalizationParams: [],
iconClass: 'custom-icon-name';
}; };
this.toaster.error('AbpUi::EntityNotFoundErrorMessage', 'AbpUi::Error', options); this.toaster.error('AbpUi::EntityNotFoundErrorMessage', 'AbpUi::Error', options);
@ -60,6 +61,7 @@ const options: Partial<Toaster.ToastOptions> = {
- `yesText` is the text of the confirmation button. A localization key or localization object can be passed. Default value is `AbpUi::Yes`. - `yesText` is the text of the confirmation button. A localization key or localization object can be passed. Default value is `AbpUi::Yes`.
- `messageLocalizationParams` is the interpolation parameters for the localization of the message. - `messageLocalizationParams` is the interpolation parameters for the localization of the message.
- `titleLocalizationParams` is the interpolation parameters for the localization of the title. - `titleLocalizationParams` is the interpolation parameters for the localization of the title.
- `iconClass` is the CSS class assigned to the icon displayed on a toast overlay.
With the options above, the toast overlay looks like this: With the options above, the toast overlay looks like this:

6
npm/ng-packs/packages/theme-shared/src/lib/components/toast/toast.component.ts

@ -17,6 +17,12 @@ export class ToastComponent implements OnInit {
} }
get iconClass(): string { get iconClass(): string {
const { iconClass } = this.toast.options || {};
if (iconClass) {
return iconClass;
}
switch (this.toast.severity) { switch (this.toast.severity) {
case 'success': case 'success':
return 'fa-check-circle'; return 'fa-check-circle';

1
npm/ng-packs/packages/theme-shared/src/lib/models/toaster.ts

@ -10,6 +10,7 @@ export namespace Toaster {
titleLocalizationParams?: string[]; titleLocalizationParams?: string[];
id: any; id: any;
containerKey?: string; containerKey?: string;
iconClass?: string;
} }
export interface Toast { export interface Toast {

23
npm/ng-packs/packages/theme-shared/src/lib/tests/toaster.service.spec.ts

@ -1,7 +1,7 @@
import { CoreTestingModule } from '@abp/ng.core/testing'; import { CoreTestingModule } from '@abp/ng.core/testing';
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { createServiceFactory, SpectatorService } from '@ngneat/spectator/jest'; import { createServiceFactory, SpectatorService } from '@ngneat/spectator/jest';
import { timer } from 'rxjs'; import { firstValueFrom, timer } from 'rxjs';
import { ToastContainerComponent } from '../components/toast-container/toast-container.component'; import { ToastContainerComponent } from '../components/toast-container/toast-container.component';
import { ToastComponent } from '../components/toast/toast.component'; import { ToastComponent } from '../components/toast/toast.component';
import { ToasterService } from '../services/toaster.service'; import { ToasterService } from '../services/toaster.service';
@ -34,7 +34,7 @@ describe('ToasterService', () => {
test('should display a toast', async () => { test('should display a toast', async () => {
service.show('MESSAGE', 'TITLE'); service.show('MESSAGE', 'TITLE');
await timer(0).toPromise(); await firstValueFrom(timer(0));
service['containerComponentRef'].changeDetectorRef.detectChanges(); service['containerComponentRef'].changeDetectorRef.detectChanges();
expect(selectToasterElement('.fa-exclamation-circle')).toBeTruthy(); expect(selectToasterElement('.fa-exclamation-circle')).toBeTruthy();
@ -51,7 +51,7 @@ describe('ToasterService', () => {
`('should display $type toast', async ({ type, selector, icon }) => { `('should display $type toast', async ({ type, selector, icon }) => {
service[type]('MESSAGE', 'TITLE'); service[type]('MESSAGE', 'TITLE');
await timer(0).toPromise(); await firstValueFrom(timer(0));
service['containerComponentRef'].changeDetectorRef.detectChanges(); service['containerComponentRef'].changeDetectorRef.detectChanges();
expect(selectToasterContent(`.${toastClassPrefix}-title`)).toBe('TITLE'); expect(selectToasterContent(`.${toastClassPrefix}-title`)).toBe('TITLE');
expect(selectToasterContent(`.${toastClassPrefix}-message`)).toBe('MESSAGE'); expect(selectToasterContent(`.${toastClassPrefix}-message`)).toBe('MESSAGE');
@ -63,7 +63,7 @@ describe('ToasterService', () => {
service.show('MESSAGE_1', 'TITLE_1'); service.show('MESSAGE_1', 'TITLE_1');
service.show('MESSAGE_2', 'TITLE_2'); service.show('MESSAGE_2', 'TITLE_2');
await timer(0).toPromise(); await firstValueFrom(timer(0));
service['containerComponentRef'].changeDetectorRef.detectChanges(); service['containerComponentRef'].changeDetectorRef.detectChanges();
const titles = document.querySelectorAll(`.${toastClassPrefix}-title`); const titles = document.querySelectorAll(`.${toastClassPrefix}-title`);
@ -77,7 +77,7 @@ describe('ToasterService', () => {
service.show('MESSAGE'); service.show('MESSAGE');
service.remove(0); service.remove(0);
await timer(0).toPromise(); await firstValueFrom(timer(0));
service['containerComponentRef'].changeDetectorRef.detectChanges(); service['containerComponentRef'].changeDetectorRef.detectChanges();
expect(selectToasterElement()).toBeNull(); expect(selectToasterElement()).toBeNull();
@ -87,7 +87,7 @@ describe('ToasterService', () => {
service.show('MESSAGE'); service.show('MESSAGE');
service.clear(); service.clear();
await timer(0).toPromise(); await firstValueFrom(timer(0));
service['containerComponentRef'].changeDetectorRef.detectChanges(); service['containerComponentRef'].changeDetectorRef.detectChanges();
expect(selectToasterElement()).toBeNull(); expect(selectToasterElement()).toBeNull();
@ -98,13 +98,22 @@ describe('ToasterService', () => {
service.show('MESSAGE_2', 'TITLE_2', 'neutral', { containerKey: 'y' }); service.show('MESSAGE_2', 'TITLE_2', 'neutral', { containerKey: 'y' });
service.clear('x'); service.clear('x');
await timer(0).toPromise(); await firstValueFrom(timer(0));
service['containerComponentRef'].changeDetectorRef.detectChanges(); service['containerComponentRef'].changeDetectorRef.detectChanges();
expect(selectToasterElement('.fa-exclamation-circle')).toBeTruthy(); expect(selectToasterElement('.fa-exclamation-circle')).toBeTruthy();
expect(selectToasterContent(`.${toastClassPrefix}-title`)).toBe('TITLE_2'); expect(selectToasterContent(`.${toastClassPrefix}-title`)).toBe('TITLE_2');
expect(selectToasterContent(`.${toastClassPrefix}-message`)).toBe('MESSAGE_2'); expect(selectToasterContent(`.${toastClassPrefix}-message`)).toBe('MESSAGE_2');
}); });
test('should display custom icon when iconClass is provided', async () => {
service.show('MESSAGE', 'TITLE', 'neutral', { iconClass: 'custom-icon' });
await firstValueFrom(timer(0));
service['containerComponentRef'].changeDetectorRef.detectChanges();
expect(selectToasterElement('.custom-icon')).toBeTruthy();
});
}); });
function clearElements(selector = `.${toastClassPrefix}`) { function clearElements(selector = `.${toastClassPrefix}`) {

Loading…
Cancel
Save