mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[ADF-2709] Replace the ADF accordion with material accordion (#3187)
* [DW-569] Replace the ADF accordion with material accordion * * Added testcases for the recent changes. * * Used angular materail mat-accordion * Refactored accordion/group component * * Refactored accordion group component. * * Refactored accordion scss
This commit is contained in:
committed by
Eugenio Romano
parent
5a255d27fd
commit
9fbfcfa96e
@@ -1,5 +1,13 @@
|
|||||||
<div class="adf-panel adf-panel-default" [ngClass]="{'adf-panel-open': isOpen}">
|
<mat-accordion class="adf-panel">
|
||||||
<div class="adf-panel-heading" [ngClass]="{'adf-panel-heading-selected': isSelected}" (click)="toggleOpen($event)">
|
<mat-expansion-panel #expansionPanel
|
||||||
|
id="adf-expansion-panel-id"
|
||||||
|
(click)="isExpandable($event)"
|
||||||
|
[expanded]="toggleExpansion()"
|
||||||
|
(opened)="onHeaderClick()"
|
||||||
|
[hideToggle]="!hasAccordionIcon">
|
||||||
|
<mat-expansion-panel-header>
|
||||||
|
<mat-panel-title>
|
||||||
|
<div class="adf-panel-heading" [ngClass]="{'adf-panel-heading-selected': isSelected}">
|
||||||
<div id="heading-icon" *ngIf="hasHeadingIcon()" class="adf-panel-heading-icon">
|
<div id="heading-icon" *ngIf="hasHeadingIcon()" class="adf-panel-heading-icon">
|
||||||
<mat-icon class="material-icons"
|
<mat-icon class="material-icons"
|
||||||
[matTooltip]="headingIconTooltip"
|
[matTooltip]="headingIconTooltip"
|
||||||
@@ -8,13 +16,11 @@
|
|||||||
</mat-icon>
|
</mat-icon>
|
||||||
</div>
|
</div>
|
||||||
<div id="heading-text" class="adf-panel-heading-text">{{heading}}</div>
|
<div id="heading-text" class="adf-panel-heading-text">{{heading}}</div>
|
||||||
<div id="accordion-button" *ngIf="hasAccordionIcon" class="adf-panel-heading-toggle">
|
|
||||||
<i class="material-icons">{{getAccordionIcon()}}</i>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</mat-panel-title>
|
||||||
<div class="adf-panel-collapse" [hidden]="!isOpen">
|
</mat-expansion-panel-header>
|
||||||
<div class="adf-panel-body" #contentWrapper>
|
<div id="adf-expansion-panel-content-id" #contentWrapper>
|
||||||
<ng-content></ng-content>
|
<ng-content></ng-content>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</mat-expansion-panel>
|
||||||
</div>
|
</mat-accordion>
|
@@ -22,22 +22,31 @@
|
|||||||
float: left;
|
float: left;
|
||||||
}
|
}
|
||||||
|
|
||||||
.adf-panel-body {
|
|
||||||
display: inline-block;
|
|
||||||
}
|
|
||||||
|
|
||||||
.adf-panel-heading-text {
|
.adf-panel-heading-text {
|
||||||
float: left;
|
float: left;
|
||||||
padding-left: 20px;
|
padding-left: 20px;
|
||||||
padding-top: 4px;
|
padding-top: 4px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.adf-panel-heading-toggle {
|
.mat-expansion-panel {
|
||||||
float: right;
|
transition: none !important;
|
||||||
cursor: pointer;
|
box-shadow: none !important;
|
||||||
|
background: none !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
.adf-panel-heading-toggle:hover {
|
.mat-expansion-panel-body {
|
||||||
opacity: 0.4;
|
padding: 1px !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mat-expansion-panel-header {
|
||||||
|
padding: 0px !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mat-expansion-panel-header-title {
|
||||||
|
margin-right: 0px !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mat-expansion-indicator {
|
||||||
|
margin-right: 25px !important;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -46,62 +46,68 @@ describe('AccordionGroupComponent', () => {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be closed by default', async(() => {
|
it('should define mat-accordion ', async(() => {
|
||||||
component.heading = 'Fake Header';
|
|
||||||
component.headingIcon = 'fake-icon';
|
|
||||||
component.contentWrapper.nativeElement.innerHTML = '<a>Test</a>';
|
|
||||||
fixture.whenStable().then(() => {
|
|
||||||
fixture.detectChanges();
|
|
||||||
let headerText = element.querySelector('#heading-text');
|
|
||||||
expect(headerText.innerText).toEqual('Fake Header');
|
|
||||||
let headerIcon = element.querySelector('#heading-icon .material-icons');
|
|
||||||
expect(headerIcon.innerText).toEqual('fake-icon');
|
|
||||||
let headerToggle = element.querySelector('#accordion-button .material-icons');
|
|
||||||
expect(headerToggle.innerText).toEqual('expand_more');
|
|
||||||
});
|
|
||||||
}));
|
|
||||||
|
|
||||||
it('should be open when click', async(() => {
|
|
||||||
component.isSelected = true;
|
component.isSelected = true;
|
||||||
component.heading = 'Fake Header';
|
component.heading = 'Fake Header';
|
||||||
component.headingIcon = 'fake-icon';
|
component.headingIcon = 'fake-icon';
|
||||||
component.contentWrapper.nativeElement.innerHTML = '<a>Test</a>';
|
component.contentWrapper.nativeElement.innerHTML = '<a>Test</a>';
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
element.querySelector('#accordion-button').click();
|
fixture.whenStable().then(() => {
|
||||||
|
fixture.detectChanges();
|
||||||
|
let accordin = element.querySelector('mat-accordion');
|
||||||
|
let expansionPanel = element.querySelector('mat-expansion-panel');
|
||||||
|
let accordinHeader = element.querySelector('mat-expansion-panel-header');
|
||||||
|
let content = element.querySelector('#adf-expansion-panel-content-id').innerHTML;
|
||||||
|
expect(accordin).toBeDefined();
|
||||||
|
expect(expansionPanel).toBeDefined();
|
||||||
|
expect(accordinHeader).toBeDefined();
|
||||||
|
expect(content).toEqual('<a>Test</a>');
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
|
||||||
|
it('should be display accordion title and icon', async(() => {
|
||||||
|
component.heading = 'Fake Header';
|
||||||
|
component.headingIcon = 'fake-icon';
|
||||||
|
component.contentWrapper.nativeElement.innerHTML = '<a>Test</a>';
|
||||||
fixture.whenStable().then(() => {
|
fixture.whenStable().then(() => {
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
let headerText = element.querySelector('#heading-text');
|
let headerText = element.querySelector('#heading-text');
|
||||||
|
let headerIcon = element.querySelector('#adf-expansion-panel-id .material-icons');
|
||||||
expect(headerText.innerText).toEqual('Fake Header');
|
expect(headerText.innerText).toEqual('Fake Header');
|
||||||
let headerIcon = element.querySelector('#heading-icon .material-icons');
|
|
||||||
expect(headerIcon.innerText).toEqual('fake-icon');
|
expect(headerIcon.innerText).toEqual('fake-icon');
|
||||||
let headerToggle = element.querySelector('#accordion-button .material-icons');
|
|
||||||
expect(headerToggle.innerText).toEqual('expand_less');
|
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
|
||||||
it('should show expand icon by default', async(() => {
|
it('should be display only accordion title', async(() => {
|
||||||
component.heading = 'Fake Header';
|
component.heading = 'Fake Header';
|
||||||
component.headingIcon = 'fake-icon';
|
component.headingIcon = '';
|
||||||
component.contentWrapper.nativeElement.innerHTML = '<a>Test</a>';
|
component.contentWrapper.nativeElement.innerHTML = '<a>Test</a>';
|
||||||
fixture.whenStable().then(() => {
|
fixture.whenStable().then(() => {
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
let headerIcon = element.querySelector('#accordion-button');
|
let headerText = element.querySelector('#heading-text');
|
||||||
expect(headerIcon).toBeDefined();
|
let headerIcon = element.querySelector('#adf-expansion-panel-id .material-icons');
|
||||||
});
|
expect(headerText.innerText).toEqual('Fake Header');
|
||||||
}));
|
|
||||||
|
|
||||||
it('should hide expand icon', async(() => {
|
|
||||||
component.heading = 'Fake Header';
|
|
||||||
component.headingIcon = 'fake-icon';
|
|
||||||
component.hasAccordionIcon = false;
|
|
||||||
component.contentWrapper.nativeElement.innerHTML = '<a>Test</a>';
|
|
||||||
fixture.whenStable().then(() => {
|
|
||||||
fixture.detectChanges();
|
|
||||||
let headerIcon = element.querySelector('#accordion-button');
|
|
||||||
expect(headerIcon).toBeNull();
|
expect(headerIcon).toBeNull();
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
it('should be display accordion title and content', async(() => {
|
||||||
|
component.isSelected = true;
|
||||||
|
component.heading = 'Fake Header';
|
||||||
|
component.headingIcon = 'fake-icon';
|
||||||
|
component.contentWrapper.nativeElement.innerHTML = '<a>Test</a>';
|
||||||
|
fixture.detectChanges();
|
||||||
|
fixture.whenStable().then(() => {
|
||||||
|
fixture.detectChanges();
|
||||||
|
let headerText = element.querySelector('#heading-text');
|
||||||
|
let headerIcon = element.querySelector('#heading-icon .material-icons');
|
||||||
|
let content = element.querySelector('#adf-expansion-panel-content-id').innerHTML;
|
||||||
|
expect(headerText.innerText).toEqual('Fake Header');
|
||||||
|
expect(headerIcon.innerText).toEqual('fake-icon');
|
||||||
|
expect(content).toEqual('<a>Test</a>');
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
|
||||||
it('should emit an event when a heading clicked', async(() => {
|
it('should emit an event when a heading clicked', async(() => {
|
||||||
component.heading = 'Fake Header';
|
component.heading = 'Fake Header';
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
|
@@ -15,8 +15,8 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Component, EventEmitter, Input, OnDestroy, Output, ViewChild, ViewEncapsulation } from '@angular/core';
|
import { AfterViewInit , Component, ElementRef, EventEmitter, Input, Output, ViewChild, ViewEncapsulation } from '@angular/core';
|
||||||
import { AccordionComponent } from './accordion.component';
|
import { MatExpansionPanel } from '@angular/material';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'adf-accordion-group',
|
selector: 'adf-accordion-group',
|
||||||
@@ -24,12 +24,14 @@ import { AccordionComponent } from './accordion.component';
|
|||||||
styleUrls: ['./accordion-group.component.scss'],
|
styleUrls: ['./accordion-group.component.scss'],
|
||||||
encapsulation: ViewEncapsulation.None
|
encapsulation: ViewEncapsulation.None
|
||||||
})
|
})
|
||||||
export class AccordionGroupComponent implements OnDestroy {
|
export class AccordionGroupComponent implements AfterViewInit {
|
||||||
private _isOpen: boolean = false;
|
private _isOpen: boolean = false;
|
||||||
private _isSelected: boolean = false;
|
private _isSelected: boolean = false;
|
||||||
|
|
||||||
@ViewChild('contentWrapper')
|
@ViewChild('contentWrapper')
|
||||||
contentWrapper: any;
|
contentWrapper: ElementRef;
|
||||||
|
|
||||||
|
@ViewChild('expansionPanel') expansionPanel: MatExpansionPanel;
|
||||||
|
|
||||||
/** Title heading for the group. */
|
/** Title heading for the group. */
|
||||||
@Input()
|
@Input()
|
||||||
@@ -55,9 +57,6 @@ export class AccordionGroupComponent implements OnDestroy {
|
|||||||
@Input()
|
@Input()
|
||||||
set isOpen(value: boolean) {
|
set isOpen(value: boolean) {
|
||||||
this._isOpen = value;
|
this._isOpen = value;
|
||||||
if (value) {
|
|
||||||
this.accordion.closeOthers(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get isOpen() {
|
get isOpen() {
|
||||||
@@ -74,26 +73,33 @@ export class AccordionGroupComponent implements OnDestroy {
|
|||||||
return this._isSelected;
|
return this._isSelected;
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(private accordion: AccordionComponent) {
|
hasContent: boolean;
|
||||||
this.accordion.addGroup(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
ngOnDestroy() {
|
constructor() { }
|
||||||
this.accordion.removeGroup(this);
|
|
||||||
|
ngAfterViewInit() {
|
||||||
|
this.hasContent = this.contentWrapper.nativeElement && this.contentWrapper.nativeElement.children.length > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
hasHeadingIcon() {
|
hasHeadingIcon() {
|
||||||
return this.headingIcon ? true : false;
|
return this.headingIcon ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleOpen(event: MouseEvent): void {
|
onHeaderClick(): void {
|
||||||
event.preventDefault();
|
|
||||||
this.isOpen = !this.isOpen;
|
|
||||||
this.headingClick.emit(this.heading);
|
this.headingClick.emit(this.heading);
|
||||||
}
|
}
|
||||||
|
|
||||||
getAccordionIcon(): string {
|
isExpandable(event: any) {
|
||||||
return this.isOpen ? 'expand_less' : 'expand_more';
|
if (!this.hasContent || !this.isOpen) {
|
||||||
|
this.expandPanel();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
expandPanel() {
|
||||||
|
this.expansionPanel.expanded = !this.expansionPanel.expanded;
|
||||||
|
}
|
||||||
|
|
||||||
|
toggleExpansion(): boolean {
|
||||||
|
return this.isOpen && this.isSelected;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -16,16 +16,12 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
import { AccordionGroupComponent } from './accordion-group.component';
|
|
||||||
import { AccordionComponent } from './accordion.component';
|
import { AccordionComponent } from './accordion.component';
|
||||||
|
|
||||||
describe('AccordionComponent', () => {
|
describe('AccordionComponent', () => {
|
||||||
|
|
||||||
let fixture: ComponentFixture<AccordionComponent>;
|
let fixture: ComponentFixture<AccordionComponent>;
|
||||||
let component: AccordionComponent;
|
let component: AccordionComponent;
|
||||||
let componentGroup1: AccordionGroupComponent;
|
|
||||||
let componentGroup2: AccordionGroupComponent;
|
|
||||||
let componentGroup3: AccordionGroupComponent;
|
|
||||||
|
|
||||||
beforeEach(async(() => {
|
beforeEach(async(() => {
|
||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
@@ -40,42 +36,7 @@ describe('AccordionComponent', () => {
|
|||||||
component = fixture.componentInstance;
|
component = fixture.componentInstance;
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
|
||||||
component.groups = [];
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should create the component', () => {
|
it('should create the component', () => {
|
||||||
expect(component).toBeTruthy();
|
expect(component).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should add the AccordionGroup', () => {
|
|
||||||
component.addGroup(componentGroup1);
|
|
||||||
expect(component.groups.length).toBe(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should close all the other group', () => {
|
|
||||||
componentGroup1 = new AccordionGroupComponent(component);
|
|
||||||
componentGroup2 = new AccordionGroupComponent(component);
|
|
||||||
componentGroup3 = new AccordionGroupComponent(component);
|
|
||||||
componentGroup1.isOpen = false;
|
|
||||||
componentGroup2.isOpen = true;
|
|
||||||
componentGroup3.isOpen = false;
|
|
||||||
|
|
||||||
expect(component.groups[0].isOpen).toBeFalsy();
|
|
||||||
expect(component.groups[1].isOpen).toBeTruthy();
|
|
||||||
expect(component.groups[2].isOpen).toBeFalsy();
|
|
||||||
|
|
||||||
componentGroup1.isOpen = true;
|
|
||||||
|
|
||||||
expect(component.groups[0].isOpen).toBeTruthy();
|
|
||||||
expect(component.groups[1].isOpen).toBeFalsy();
|
|
||||||
expect(component.groups[2].isOpen).toBeFalsy();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should remove the AccordionGroup', () => {
|
|
||||||
component.addGroup(componentGroup1);
|
|
||||||
component.removeGroup(componentGroup1);
|
|
||||||
expect(component.groups.length).toBe(0);
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
@@ -16,7 +16,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { Component, ViewEncapsulation } from '@angular/core';
|
import { Component, ViewEncapsulation } from '@angular/core';
|
||||||
import { AccordionGroupComponent } from './accordion-group.component';
|
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'adf-accordion',
|
selector: 'adf-accordion',
|
||||||
@@ -28,25 +27,4 @@ import { AccordionGroupComponent } from './accordion-group.component';
|
|||||||
},
|
},
|
||||||
encapsulation: ViewEncapsulation.None
|
encapsulation: ViewEncapsulation.None
|
||||||
})
|
})
|
||||||
export class AccordionComponent {
|
export class AccordionComponent {}
|
||||||
groups: Array<AccordionGroupComponent> = [];
|
|
||||||
|
|
||||||
addGroup(group: AccordionGroupComponent): void {
|
|
||||||
this.groups.push(group);
|
|
||||||
}
|
|
||||||
|
|
||||||
closeOthers(openGroup: AccordionGroupComponent): void {
|
|
||||||
this.groups.forEach((group: AccordionGroupComponent) => {
|
|
||||||
if (group !== openGroup) {
|
|
||||||
group.isOpen = false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
removeGroup(group: AccordionGroupComponent): void {
|
|
||||||
const index = this.groups.indexOf(group);
|
|
||||||
if (index !== -1) {
|
|
||||||
this.groups.splice(index, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@@ -24,7 +24,7 @@ import {
|
|||||||
MatInputModule, MatListModule, MatNativeDateModule, MatOptionModule, MatProgressSpinnerModule, MatRadioModule,
|
MatInputModule, MatListModule, MatNativeDateModule, MatOptionModule, MatProgressSpinnerModule, MatRadioModule,
|
||||||
MatRippleModule, MatSelectModule, MatSlideToggleModule, MatTableModule, MatTabsModule,
|
MatRippleModule, MatSelectModule, MatSlideToggleModule, MatTableModule, MatTabsModule,
|
||||||
MatMenuModule, MatProgressBarModule, MatSidenavModule, MatSnackBarModule, MatToolbarModule,
|
MatMenuModule, MatProgressBarModule, MatSidenavModule, MatSnackBarModule, MatToolbarModule,
|
||||||
MatTooltipModule
|
MatTooltipModule, MatExpansionModule
|
||||||
} from '@angular/material';
|
} from '@angular/material';
|
||||||
|
|
||||||
export function modules() {
|
export function modules() {
|
||||||
@@ -34,7 +34,7 @@ export function modules() {
|
|||||||
MatInputModule, MatListModule, MatNativeDateModule, MatOptionModule, MatProgressSpinnerModule, MatRadioModule,
|
MatInputModule, MatListModule, MatNativeDateModule, MatOptionModule, MatProgressSpinnerModule, MatRadioModule,
|
||||||
MatRippleModule, MatSelectModule, MatSlideToggleModule, MatTableModule, MatTabsModule,
|
MatRippleModule, MatSelectModule, MatSlideToggleModule, MatTableModule, MatTabsModule,
|
||||||
MatMenuModule, MatProgressBarModule, MatSidenavModule, MatSnackBarModule, MatToolbarModule,
|
MatMenuModule, MatProgressBarModule, MatSidenavModule, MatSnackBarModule, MatToolbarModule,
|
||||||
MatTooltipModule, MatDatetimepickerModule, MatNativeDatetimeModule
|
MatTooltipModule, MatDatetimepickerModule, MatNativeDatetimeModule, MatExpansionModule
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user