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

[Notification]: Refactor component not to strict what content is contained, remove empty spaces from body text content #367

Merged
merged 16 commits into from
May 24, 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<fudis-icon class="fudis-error-summary__icon" [icon]="'alert'"></fudis-icon>
<div class="fudis-error-summary__errors">
<ng-container *ngIf="helpText">
<fudis-body-text fudisSpacing [marginTop]="'xs'">
<fudis-body-text fudisSpacing [size]="'md-regular'" [marginTop]="'xs'">
<span class="fudis-visually-hidden">{{ _attentionText }}:&nbsp;</span
>{{ helpText }}</fudis-body-text
>
Expand All @@ -21,13 +21,15 @@
[link]="'.'"
[fragmentId]="error.id"
[title]="error.message"
[color]="'primary-dark'"
></fudis-link>
</ng-container>
<ng-container *ngIf="linkType === 'href'">
<fudis-link
[size]="'md'"
[href]="'#' + error.id"
[title]="error.message"
[color]="'primary-dark'"
></fudis-link>
</ng-container>
<ng-container *ngIf="linkType === 'onClick'">
Expand All @@ -36,6 +38,7 @@
[size]="'md'"
[href]="'#'"
[title]="error.message"
[color]="'primary-dark'"
></fudis-link>
</ng-container>
</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import readme from './readme.mdx';
<fudis-button [variant]="'tertiary'" [icon]="'plus'" [label]="'Some action'" />
</ng-template>
<ng-template fudisNotifications [type]="'fieldset'">
<fudis-notification>This is notification</fudis-notification>
<fudis-notification
><fudis-body-text>This is notification</fudis-body-text></fudis-notification
>
</ng-template>
<ng-template fudisContent [type]="'fieldset'">
<fudis-grid [columns]="{ md: 2 }">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ HTML Template
<fudis-button [variant]="'tertiary'" [icon]="'plus'" [label]="'Some action'" />
</ng-template>
<ng-template fudisNotifications [type]="'fieldset'">
<fudis-notification>This is notification inside fieldset</fudis-notification>
<fudis-notification>
<fudis-body-text>This is notification inside fieldset</fudis-body-text>
</fudis-notification>
</ng-template>
<ng-template fudisContent [type]="'fieldset'">
<fudis-text-input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,11 @@ class ExampleWithMultipleFormsComponent {
<ng-template fudisContent [type]="'form'">
<fudis-section [title]="'Main section'" [errorSummaryBreadcrumb]="true">
<ng-template fudisNotifications [type]="'section'">
<fudis-notification>This is notification for the section</fudis-notification>
<fudis-notification
><fudis-body-text
>This is notification for the section</fudis-body-text
></fudis-notification
>
</ng-template>
<ng-template fudisContent [type]="'section'">
<fudis-expandable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import {
ChangeDetectionStrategy,
Component,
EventEmitter,
Host,
Input,
OnChanges,
Optional,
Output,
Signal,
effect,
} from '@angular/core';
import { FudisTranslationService } from '../../services/translation/translation.service';
import { FudisComponentChanges, FudisTranslationConfig } from '../../types/miscellaneous';
import { LinkApiDirective } from '../../directives/link/link-api/link-api.directive';
import { NotificationComponent } from '../notification/notification.component';

@Component({
selector: 'fudis-link',
Expand All @@ -19,8 +22,16 @@ import { LinkApiDirective } from '../../directives/link/link-api/link-api.direct
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class LinkComponent extends LinkApiDirective implements OnChanges {
constructor(private _translationService: FudisTranslationService) {
constructor(
@Host() @Optional() private _parentNotification: NotificationComponent,
private _translationService: FudisTranslationService,
) {
super();

if (_parentNotification) {
this.color = 'gray-dark';
}

effect(() => {
this._translations = this._translationService.getTranslations();

Expand All @@ -38,6 +49,7 @@ export class LinkComponent extends LinkApiDirective implements OnChanges {
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@Input() link: string | any[];

/**
* Title for the link, if not defined title will be the same as link URL
*/
Expand Down Expand Up @@ -85,7 +97,10 @@ export class LinkComponent extends LinkApiDirective implements OnChanges {
protected _externalLinkTitleParsed: string[];

ngOnChanges(changes: FudisComponentChanges<LinkComponent>): void {
if (changes.externalLink || changes.title) {
if (
changes.externalLink?.currentValue !== changes.externalLink?.previousValue ||
changes.title?.currentValue !== changes.title?.previousValue
) {
this._parseExternalLinkTitle();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MockComponent } from 'ng-mocks';
import { IconComponent } from '../icon/icon.component';
import { BodyTextComponent } from '../typography/body-text/body-text.component';
import { LinkComponent } from '../link/link.component';
import { NotificationComponent } from './notification.component';
import { FudisNotification } from '../../types/miscellaneous';
Expand All @@ -16,13 +14,7 @@ describe('NotificationComponent', () => {

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [
NotificationComponent,
IconComponent,
MockComponent(BodyTextComponent),
LinkComponent,
LinkDirective,
],
declarations: [NotificationComponent, IconComponent, LinkComponent, LinkDirective],
imports: [RouterModule.forRoot([])],
})
.overrideComponent(NotificationComponent, {
Expand All @@ -46,7 +38,7 @@ describe('NotificationComponent', () => {

function notificationIconCheck(variant: FudisNotification): void {
component.variant = variant;
component.ngOnChanges();

fixture.detectChanges();

let icon: string = '';
Expand All @@ -68,15 +60,14 @@ describe('NotificationComponent', () => {
break;
}

const iconElement = getElement(fixture, `.fudis-icon#${icon}`);

expect(component.icon).toEqual(icon);
expect(iconElement).toBeTruthy();
const iconVariant = getElement(fixture, 'fudis-icon svg').getAttribute('id');
expect(iconVariant).toEqual(icon);
}

function notificationVariants(variant: FudisNotification): void {
component.variant = variant;
fixture.detectChanges();

assertNotificationHasClasses(`fudis-notification fudis-notification__${variant}`);
}

Expand All @@ -97,45 +88,4 @@ describe('NotificationComponent', () => {
notificationIconCheck('info');
});
});

describe('Link inside Notification', () => {
beforeEach(() => {
component.link = ['/test-url'];
fixture.detectChanges();
});
it('should have default CSS class', () => {
const linkElement = getElement(fixture, 'fudis-link');

expect(linkElement).toBeTruthy();
expect(linkElement.className).toContain('fudis-notification__link');
});

it('should have router link element', () => {
const anchorElement = getElement(fixture, 'a');

expect(anchorElement).toBeTruthy();
expect(anchorElement.getAttribute('href')).toEqual('/test-url');
});

it('should replace router link with linkTitle if given', () => {
component.linkTitle = 'New link title';
fixture.detectChanges();

const anchorElement = getElement(fixture, 'a');

expect(anchorElement.textContent).toEqual('New link title');
});

it('should replace router link with external link', () => {
component.externalLink = 'www.example.com';
fixture.detectChanges();

const anchorElement = getElement(fixture, 'a');

expect(anchorElement.getAttribute('href')).toEqual('www.example.com');
expect(anchorElement.getAttribute('aria-label')).toEqual(
'www.example.com, (opens in a new tab)',
);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
<div class="fudis-notification fudis-notification__{{ variant }}">
<ng-container *ngIf="!_content">
<fudis-icon [icon]="_icon" />
<fudis-body-text class="fudis-notification__body-text" [size]="'lg-regular'">
<fudis-icon *ngIf="variant === 'warning'" [icon]="'exclamation-mark-circle'" />
<fudis-icon *ngIf="variant === 'danger'" [icon]="'alert'" />
<fudis-icon *ngIf="variant === 'success'" [icon]="'checkmark-circle'" />
<fudis-icon *ngIf="variant === 'info'" [icon]="'info-circle'" />
<div class="fudis-notification__content">
<span class="fudis-visually-hidden">{{ _attentionText }}:&nbsp;</span>
<ng-content />
<fudis-link
class="fudis-notification__link"
*ngIf="!externalLink && link"
[title]="linkTitle"
[link]="link"
[color]="'gray-dark'"
/>
<fudis-link
class="fudis-notification__link"
*ngIf="externalLink"
[title]="linkTitle"
[externalLink]="externalLink"
[color]="'gray-dark'"
/>
</fudis-body-text>
</div>
</ng-container>
<ng-container *ngIf="_content?.type === 'notification'">
<ng-container *ngTemplateOutlet="_content!.templateRef" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@

box-sizing: border-box;
display: flex;
align-items: center;
align-items: flex-start;
padding: 0 spacing.$spacing-sm 0 spacing.$spacing-xxs;
width: 100%;
min-height: spacing.$spacing-md;

&__link {
margin-left: spacing.$spacing-xxs;
}

&__body-text {
white-space: pre-wrap;
&__content {
display: inline-block;
align-self: center;
margin: calc(spacing.$spacing-xxs + spacing.$pixel-2) 0;
}

&__warning {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import {
ChangeDetectionStrategy,
Component,
ContentChild,
Input,
OnChanges,
OnInit,
effect,
} from '@angular/core';
import { FudisIcon } from '../../types/icons';
import { ChangeDetectionStrategy, Component, ContentChild, Input, effect } from '@angular/core';
import { ContentDirective } from '../../directives/content-projection/content/content.directive';
import { FudisNotification } from '../../types/miscellaneous';
import { FudisTranslationService } from '../../services/translation/translation.service';
Expand All @@ -18,7 +9,7 @@ import { FudisTranslationService } from '../../services/translation/translation.
styleUrls: ['./notification.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class NotificationComponent implements OnChanges, OnInit {
export class NotificationComponent {
constructor(private _translateService: FudisTranslationService) {
effect(() => {
this._attentionText = this._translateService.getTranslations()().ICON.ATTENTION;
Expand All @@ -35,67 +26,8 @@ export class NotificationComponent implements OnChanges, OnInit {
*/
@Input() variant: FudisNotification = 'warning';

/**
* Add Angular Router link
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@Input() link: string | any[];

/**
* Add an external link as HTML href to point a target page on another domain.
* External link contains new-tab icon and assistive aria-label.
*/
@Input() externalLink: string;

/**
* Title for the link, if not defined title will be the same as link URL
*/
@Input() linkTitle: string;

/**
* Icon for notification
*/
protected _icon: FudisIcon;

/**
* Screen reader text for icon
*/
protected _attentionText: string;

/**
* Getter for notification icon
*/
get icon(): string {
return this._icon;
}

ngOnInit(): void {
this._setNotificationIcon();
}

ngOnChanges(): void {
this._setNotificationIcon();
}

/**
* Used to set correct icon for each notification variant
*/
private _setNotificationIcon(): void {
switch (this.variant) {
case 'warning':
this._icon = 'exclamation-mark-circle';
break;
case 'danger':
this._icon = 'alert';
break;
case 'success':
this._icon = 'checkmark-circle';
break;
case 'info':
this._icon = 'info-circle';
break;
default:
break;
}
}
}
Loading
Loading