forked from NationalBankBelgium/stark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stark-ui): implement collapsible feature
ISSUES CLOSED: NationalBankBelgium#595
- Loading branch information
Showing
30 changed files
with
519 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./collapsible/collapsible.module"; | ||
export * from "./collapsible/components"; |
13 changes: 13 additions & 0 deletions
13
packages/stark-ui/src/modules/collapsible/collapsible.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { NgModule } from "@angular/core"; | ||
import { StarkCollapsibleComponent } from "./components"; | ||
import { MatExpansionModule } from "@angular/material/expansion"; | ||
import { MatIconModule } from "@angular/material/icon"; | ||
import { CommonModule } from "@angular/common"; | ||
import { TranslateModule } from "@ngx-translate/core"; | ||
|
||
@NgModule({ | ||
declarations: [StarkCollapsibleComponent], | ||
imports: [CommonModule, MatExpansionModule, MatIconModule, TranslateModule], | ||
exports: [StarkCollapsibleComponent] | ||
}) | ||
export class StarkCollapsibleModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./components/collapsible.component"; |
51 changes: 51 additions & 0 deletions
51
packages/stark-ui/src/modules/collapsible/components/_collapsible-theme.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* ============================================================================== */ | ||
/* S t a r k C o l l a p s i b l e - T h e m e */ | ||
/* ============================================================================== */ | ||
/* stark-ui: src/modules/collapsible/components/_collapsible-theme.scss */ | ||
|
||
@mixin stark-collapsible-color($color_high, $color_low) { | ||
.mat-expansion-panel { | ||
box-shadow: $elevation-1; | ||
&.mat-expanded { | ||
box-shadow: $elevation-16; | ||
} | ||
|
||
.mat-expansion-panel-header { | ||
border-bottom: 1px solid rgba($color: $color_low, $alpha: 0.12); | ||
background-color: rgba($color: $color_high, $alpha: 0.02); | ||
&:not(.mat-expanded) { | ||
&:hover { | ||
background-color: rgba($color: $color_low, $alpha: 0.02); | ||
} | ||
} | ||
&.mat-expanded { | ||
background-color: rgba($color: $color_high, $alpha: 1); | ||
} | ||
} | ||
|
||
.mat-expansion-panel-body { | ||
background-color: rgba($color: $color_high, $alpha: 0.02); | ||
} | ||
} | ||
} | ||
|
||
.stark-collapsible { | ||
@include stark-collapsible-color( | ||
map-get(map-get($color-theme, white-transp), color), | ||
map-get(map-get($color-theme, black-opaque), color) | ||
); | ||
|
||
.mat-expansion-panel-header-description { | ||
color: mat-color(map-get($base-theme, primary-palette), 900); | ||
} | ||
|
||
.mat-expansion-panel-header-title { | ||
color: mat-color(map-get($base-theme, primary-palette), 500); | ||
} | ||
|
||
.stark-collapsible-icon { | ||
color: mat-color(map-get($base-theme, primary-palette), 500); | ||
} | ||
} | ||
|
||
/* END stark-ui: src/modules/collapsible/components/_collapsible-theme.scss */ |
44 changes: 44 additions & 0 deletions
44
packages/stark-ui/src/modules/collapsible/components/_collapsible.component.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* ============================================================================== */ | ||
/* S t a r k C o l l a p s i b l e */ | ||
/* ============================================================================== */ | ||
/* stark-ui: packages/stark-ui/src/modules/collapsible/components/_collapsible.component.scss */ | ||
|
||
.stark-collapsible { | ||
.mat-expansion-panel { | ||
transition: box-shadow 150ms linear; | ||
|
||
&.mat-expanded { | ||
.mat-expansion-panel-header-title { | ||
.stark-collapsible-icon { | ||
&.spin { | ||
transform: rotate(180deg); | ||
} | ||
} | ||
} | ||
} | ||
|
||
.mat-expansion-panel-header { | ||
padding-right: 0; | ||
} | ||
|
||
.mat-expansion-panel-body { | ||
padding-top: 16px; | ||
} | ||
|
||
.mat-expansion-panel-header-title { | ||
align-items: center; | ||
.stark-collapsible-title { | ||
font-size: mat-font-size($typography-config, subheading-1); | ||
font-weight: mat-font-weight($typography-config, subheading-1); | ||
line-height: mat-line-height($typography-config, subheading-1); | ||
} | ||
} | ||
|
||
.stark-collapsible-icon { | ||
transition: transform 150ms linear; | ||
margin-right: 6px; | ||
} | ||
} | ||
} | ||
|
||
/* END stark-ui: packages/stark-ui/src/modules/collapsible/components/_collapsible.component.scss */ |
14 changes: 14 additions & 0 deletions
14
packages/stark-ui/src/modules/collapsible/components/collapsible.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<mat-expansion-panel class="stark-collapsible" | ||
id="{{ htmlId }}" | ||
[expanded]="isExpanded" | ||
hideToggle> | ||
<mat-expansion-panel-header | ||
(click)="toggleCollapsible()"> | ||
<mat-panel-title> | ||
<mat-icon [ngClass]="{ 'spin' : iconSpinningEnabled }" class="stark-collapsible-icon" starkSvgViewBox [svgIcon]="icon"></mat-icon> | ||
<span class="stark-collapsible-title" translate>{{ titleLabel }}</span> | ||
</mat-panel-title> | ||
</mat-expansion-panel-header> | ||
|
||
<ng-content></ng-content> | ||
</mat-expansion-panel> |
95 changes: 95 additions & 0 deletions
95
packages/stark-ui/src/modules/collapsible/components/collapsible.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/*tslint:disable:completed-docs*/ | ||
import { NO_ERRORS_SCHEMA } from "@angular/core"; | ||
import { async, ComponentFixture, TestBed } from "@angular/core/testing"; | ||
import { STARK_LOGGING_SERVICE } from "@nationalbankbelgium/stark-core"; | ||
import { MockStarkLoggingService } from "@nationalbankbelgium/stark-core/testing"; | ||
import { StarkCollapsibleComponent } from "../components"; | ||
|
||
describe("CollapsibleComponent", () => { | ||
let component: StarkCollapsibleComponent; | ||
let fixture: ComponentFixture<StarkCollapsibleComponent>; | ||
|
||
/** | ||
* async beforeEach | ||
*/ | ||
beforeEach(async(() => { | ||
return ( | ||
TestBed.configureTestingModule({ | ||
imports: [], | ||
declarations: [StarkCollapsibleComponent], | ||
providers: [{ provide: STARK_LOGGING_SERVICE, useValue: new MockStarkLoggingService() }], | ||
schemas: [NO_ERRORS_SCHEMA] // tells the Angular compiler to ignore unrecognized elements and attributes (svgIcon) | ||
}) | ||
/** | ||
* Compile template and css | ||
*/ | ||
.compileComponents() | ||
); | ||
})); | ||
|
||
/** | ||
* Synchronous beforeEach | ||
*/ | ||
beforeEach(() => { | ||
fixture = TestBed.createComponent(StarkCollapsibleComponent); | ||
component = fixture.componentInstance; | ||
|
||
// inputs | ||
component.htmlId = "toto"; | ||
component.titleLabel = "titre"; | ||
component.iconSpinningEnabled = true; | ||
|
||
fixture.detectChanges(); // trigger initial data binding | ||
}); | ||
|
||
describe("on initialization", () => { | ||
it("should set internal component properties", () => { | ||
expect(fixture).toBeDefined(); | ||
expect(component).toBeDefined(); | ||
|
||
expect(component.logger).not.toBeNull(); | ||
expect(component.logger).toBeDefined(); | ||
|
||
expect(component.htmlId).not.toBeNull(); | ||
expect(component.htmlId).toBeDefined(); | ||
expect(component.htmlId).toBe("toto"); | ||
|
||
expect(component.isExpanded).not.toBeNull(); | ||
expect(component.isExpanded).toBeDefined(); | ||
expect(component.isExpanded).toBe(false); | ||
|
||
expect(component.titleLabel).not.toBeNull(); | ||
expect(component.titleLabel).toBeDefined(); | ||
expect(component.titleLabel).toBe("titre"); | ||
|
||
expect(component.iconSpinningEnabled).not.toBeNull(); | ||
expect(component.iconSpinningEnabled).toBeDefined(); | ||
expect(component.iconSpinningEnabled).toBe(true); | ||
}); | ||
}); | ||
|
||
describe("@Input()", () => { | ||
it("should have modified the view accordingly", () => { | ||
const actionBar: HTMLElement = fixture.nativeElement.querySelector("#" + component.htmlId); | ||
expect(actionBar).toBeDefined(); | ||
|
||
const matIcon: HTMLElement = fixture.nativeElement.querySelector("mat-icon"); | ||
expect(matIcon.className).toContain("spin"); | ||
}); | ||
}); | ||
|
||
describe("toggleCollapsible()", () => { | ||
it("should change the isExpanded value and send it as output", () => { | ||
const exp: boolean = component.isExpanded; | ||
|
||
// Test output | ||
component.isExpandedChange.subscribe((value: boolean) => { | ||
expect(value).toBe(!exp); | ||
}); | ||
|
||
component.toggleCollapsible(); | ||
|
||
expect(component.isExpanded).toBe(!exp); | ||
}); | ||
}); | ||
}); |
83 changes: 83 additions & 0 deletions
83
packages/stark-ui/src/modules/collapsible/components/collapsible.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { Component, EventEmitter, HostBinding, Inject, Input, OnInit, Output } from "@angular/core"; | ||
import { STARK_LOGGING_SERVICE, StarkLoggingService } from "@nationalbankbelgium/stark-core"; | ||
|
||
/** | ||
* Name of the component | ||
*/ | ||
const componentName: string = "stark-collapsible"; | ||
|
||
/** | ||
* Component to display an accordion around included html using angular material's expansion panel. | ||
*/ | ||
@Component({ | ||
selector: "stark-collapsible", | ||
templateUrl: "./collapsible.component.html" | ||
}) | ||
export class StarkCollapsibleComponent implements OnInit { | ||
/** | ||
* Adds class="stark-app-logo" attribute on the host component | ||
*/ | ||
@HostBinding("class") | ||
public class: string = componentName; | ||
|
||
/** | ||
* ID to set to the collapsible | ||
*/ | ||
@Input() | ||
public htmlId: string; | ||
|
||
/** | ||
* Icon to use in the header (based on Material Design Icons) | ||
*/ | ||
@Input() | ||
public icon: "chevron-right" | string = "chevron-right"; | ||
|
||
/** | ||
* Boolean value triggering the collapsing of the collapsible | ||
*/ | ||
@Input() | ||
public isExpanded: boolean = false; | ||
|
||
/** | ||
* Output value giving the collapse state of the collapsible | ||
* NOTE : isExpandedChange is used with isExpanded to create a two-way binding (using a banana in the box "[(isExpanded)]" ). | ||
* For more info, see : https://angular.io/guide/template-syntax#two-way-binding--- | ||
*/ | ||
@Output() | ||
public isExpandedChange: EventEmitter<boolean> = new EventEmitter(); | ||
|
||
/** | ||
* String or translation key that will be displayed in the title part of the header. | ||
*/ | ||
@Input() | ||
public titleLabel: string; | ||
|
||
/** | ||
* Design options related to the component | ||
*/ | ||
@Input() | ||
public iconSpinningEnabled: boolean = false; | ||
|
||
/** | ||
* Class constructor | ||
* @param logger - The logger of the application | ||
*/ | ||
public constructor(@Inject(STARK_LOGGING_SERVICE) public logger: StarkLoggingService) { | ||
// empty constructor | ||
} | ||
|
||
/** | ||
* Component lifecycle hook | ||
*/ | ||
public ngOnInit(): void { | ||
this.logger.debug(componentName + ": component initialized"); | ||
} | ||
|
||
/** | ||
* Switch and triggers the dispatch of the collapse state information. | ||
*/ | ||
public toggleCollapsible(): void { | ||
this.isExpanded = !this.isExpanded; | ||
this.isExpandedChange.emit(this.isExpanded); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.