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

[Breadcrumbs]: Created breadcrumbs component #167

Merged
merged 2 commits into from
Oct 24, 2023
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
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"printWidth": 100
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<nav [attr.aria-label]="breadcrumbsAriaLabel" *ngIf="links.length > 0">
<ul class="fudis-breadcrumbs">
<li class="fudis-breadcrumbs__item" *ngFor="let link of links; let last = last">
<fudis-link
[disabled]="last"
[color]="last ? 'default' : 'primary'"
[routerLinkUrl]="link.url"
*ngIf="link.url"
[linkTitle]="link.label"
[attr.aria-current]="last ? 'page' : null" />
<fudis-icon class="fudis-breadcrumbs__icon" aria-hidden="true" icon="chevron" *ngIf="!last" />
</li>
</ul>
</nav>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@use '../../foundations/typography/mixins.scss' as typography;

.fudis-breadcrumbs {
@include typography.body-text-md-regular;

display: flex;
align-items: center;
list-style: none;

&__item {
display: flex;
align-items: center;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { By } from '@angular/platform-browser';
import { BreadcrumbsComponent } from './breadcrumbs.component';
import { LinkComponent } from '../link/link.component';

describe('BreadcrumbsComponent', () => {
let component: BreadcrumbsComponent;
let fixture: ComponentFixture<BreadcrumbsComponent>;

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [BreadcrumbsComponent, LinkComponent],
imports: [RouterTestingModule],
});
fixture = TestBed.createComponent(BreadcrumbsComponent);
component = fixture.componentInstance;
});

it('should render the correct number of breadcrumb items', () => {
component.links = [
{ label: 'Components', url: '/components' },
{ label: 'Breadcrumbs', url: '/breadcrumbs' },
{ label: 'Breadcrumb', url: '/breadcrumbs/breadcrumb' },
];
fixture.detectChanges();
const breadcrumbAnchors = fixture.debugElement.queryAll(By.css('a'));

expect(breadcrumbAnchors.length).toBe(3);
});

it('should display the correct breadcrumb labels', () => {
component.links = [
{ label: 'Components', url: '/components' },
{ label: 'Breadcrumbs', url: '/breadcrumbs' },
];
fixture.detectChanges();
const breadcrumbItems = fixture.debugElement.queryAll(By.css('a'));

expect(breadcrumbItems[0].nativeElement.textContent).toContain('Components');

expect(breadcrumbItems[1].nativeElement.textContent).toContain('Breadcrumbs');
});

it('should disable the last breadcrumb item', () => {
component.links = [
{ label: 'Components', url: '/components' },
{ label: 'Breadcrumbs', url: '/breadcrumbs' },
];
fixture.detectChanges();

const breadcrumbAnchors = fixture.debugElement.queryAll(By.css('a'));

expect(
breadcrumbAnchors[breadcrumbAnchors.length - 1].nativeElement.classList.contains('fudis-link__anchor--disabled')
).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Component, Input, ChangeDetectionStrategy } from '@angular/core';
import { FudisBreadcrumb } from '../../types/miscellaneous';

@Component({
selector: 'fudis-breadcrumbs',
templateUrl: './breadcrumbs.component.html',
styleUrls: ['./breadcrumbs.component.scss'],
RiinaKuu marked this conversation as resolved.
Show resolved Hide resolved
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class BreadcrumbsComponent {
@Input() breadcrumbsAriaLabel: string;

@Input() links: FudisBreadcrumb[] = [];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { RouterTestingModule } from '@angular/router/testing';
import { moduleMetadata, StoryFn, Meta } from '@storybook/angular';
import { BreadcrumbsComponent } from './breadcrumbs.component';

export default {
title: 'Components/Breadcrumbs',
component: BreadcrumbsComponent,
decorators: [
moduleMetadata({
imports: [RouterTestingModule],
}),
],
argTypes: {
links: {
description: 'List of breadcrumb links.',
control: 'object',
table: {
type: {
summary: '{ label: string; url: string }[]',
},
},
},
},
} as Meta;

const Template: StoryFn<BreadcrumbsComponent> = (args: BreadcrumbsComponent) => ({
props: args,
template: '<fudis-breadcrumbs [links]="links"></fudis-breadcrumbs>',
});

export const Breadcrumbs = Template.bind({});
Breadcrumbs.args = {
links: [
{ label: 'Components', url: '/Components' },
{ label: 'Breadcrumbs', url: '/components/breadcrumbs' },
{ label: 'Documentation', url: '/components/breadcrumbs/documentation' },
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<ng-container *ngIf="!routerLinkUrl">
<a
#linkRef
[class.fudis-link__anchor--disabled]="disabled"
RiinaKuu marked this conversation as resolved.
Show resolved Hide resolved
[attr.aria-disabled]="disabled"
class="fudis-link__anchor fudis-link__anchor__{{ size }} fudis-link__anchor__{{ color }}"
(focus)="_handleFocus($event)"
(blur)="_handleBlur($event)"
Expand All @@ -21,6 +23,8 @@
<a
#linkRef
class="fudis-link__anchor fudis-link__anchor__{{ size }} fudis-link__anchor__{{ color }}"
[class.fudis-link__anchor--disabled]="disabled"
[attr.aria-disabled]="disabled"
(focus)="_handleFocus($event)"
(blur)="_handleBlur($event)"
[routerLink]="routerLinkUrl"
Expand All @@ -35,6 +39,8 @@
#linkRef
(focus)="_handleFocus($event)"
(blur)="_handleBlur($event)"
[class.fudis-link__anchor--disabled]="disabled"
[attr.aria-disabled]="disabled"
attr.aria-label="{{ linkTitle ? linkTitle : href }}, {{ _externalLinkAriaLabel }}"
[href]="href ? href : null"
target="_blank"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,10 @@
&__lg {
@include typography.text-link-l-typography;
}

&--disabled {
text-decoration: none;
pointer-events: none;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ export class LinkComponent implements AfterViewInit {
*/
@ViewChild('linkRef') linkRef: ElementRef;

/**
* Determines if the link is disabled or not. When disabled, the link won't be interactive.
*/
@Input() disabled: boolean = false;

/**
* Link URL using native href
*/
Expand Down
3 changes: 3 additions & 0 deletions ngx-fudis/projects/ngx-fudis/src/lib/ngx-fudis.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ import { AlertComponent } from './components/alert/alert/alert.component';
import { AlertGroupComponent } from './components/alert/alert-group/alert-group.component';
import { FudisAlertService } from './services/alert/alert.service';
import { FudisBreakpointService } from './services/breakpoint/breakpoint.service';
import { BreadcrumbsComponent } from './components/breadcrumbs/breadcrumbs.component';
RiinaKuu marked this conversation as resolved.
Show resolved Hide resolved

@NgModule({
/*
Expand Down Expand Up @@ -163,6 +164,7 @@ import { FudisBreakpointService } from './services/breakpoint/breakpoint.service
TextInputComponent,
TextAreaComponent,
TooltipDirective,
BreadcrumbsComponent,
],
/*
* Include imports outside of Fudis components in 'imports' array below.
Expand Down Expand Up @@ -244,6 +246,7 @@ import { FudisBreakpointService } from './services/breakpoint/breakpoint.service
TextAreaComponent,
TextInputComponent,
TooltipDirective,
BreadcrumbsComponent,
],
providers: [
FudisAlertService,
Expand Down
5 changes: 5 additions & 0 deletions ngx-fudis/projects/ngx-fudis/src/lib/types/miscellaneous.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,8 @@ export interface FudisTranslationLanguageBadgeAriaLabel {
SELECTED: string;
MISSING_TRANSLATION: string;
}

export interface FudisBreadcrumb {
label: string;
url: string;
}
2 changes: 2 additions & 0 deletions ngx-fudis/projects/ngx-fudis/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,5 @@ export { AlertComponent } from './lib/components/alert/alert/alert.component';
export { AlertGroupComponent } from './lib/components/alert/alert-group/alert-group.component';
export { FudisAlertService } from './lib/services/alert/alert.service';
export { FudisBreakpointService } from './lib/services/breakpoint/breakpoint.service';

export { BreadcrumbsComponent } from './lib/components/breadcrumbs/breadcrumbs.component';
Loading