Skip to content

Commit

Permalink
fix(accordion): accept ids that start with a digit (#4196)
Browse files Browse the repository at this point in the history
Closes #4195
  • Loading branch information
ExFlo authored and maxokorokov committed Apr 7, 2022
1 parent 5ce1c49 commit ab87fc7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
16 changes: 14 additions & 2 deletions src/accordion/accordion.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';

import {NgbAccordion, NgbPanel, NgbPanelTitle, NgbPanelContent, NgbPanelHeader, NgbPanelToggle} from './accordion';
import {
NgbAccordion,
NgbPanel,
NgbPanelTitle,
NgbPanelContent,
NgbPanelHeader,
NgbPanelToggle,
NgbRefDirective
} from './accordion';

export {
NgbAccordion,
Expand All @@ -18,6 +26,10 @@ export {NgbAccordionConfig} from './accordion-config';
const NGB_ACCORDION_DIRECTIVES =
[NgbAccordion, NgbPanel, NgbPanelTitle, NgbPanelContent, NgbPanelHeader, NgbPanelToggle];

@NgModule({declarations: NGB_ACCORDION_DIRECTIVES, exports: NGB_ACCORDION_DIRECTIVES, imports: [CommonModule]})
@NgModule({
declarations: [NgbRefDirective, ...NGB_ACCORDION_DIRECTIVES],
exports: NGB_ACCORDION_DIRECTIVES,
imports: [CommonModule]
})
export class NgbAccordionModule {
}
13 changes: 13 additions & 0 deletions src/accordion/accordion.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ describe('ngb-accordion', () => {
expect(accordionCmp.closeOtherPanels).toBe(defaultConfig.closeOthers);
});

it('should panel authorize number as Ids', () => {
const testCompFixture = TestBed.createComponent(TestComponent);
const testComp = testCompFixture.componentInstance;
testComp.panels = [
{id: '1', disabled: false, title: 'Panel 1', content: 'foo', type: ''},
{id: '2', disabled: false, title: 'Panel 2', content: 'bar', type: ''},
{id: '3', disabled: false, title: 'Panel 3', content: 'baz', type: ''}
];
testComp.activeIds = ['2'];
testCompFixture.detectChanges();
expectOpenPanels(testCompFixture.nativeElement, [false, true, false]);
});

it('should have no open panels', () => {
const fixture = TestBed.createComponent(TestComponent);
const accordionEl = fixture.nativeElement.children[0];
Expand Down
27 changes: 17 additions & 10 deletions src/accordion/accordion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
TemplateRef,
ViewEncapsulation,
NgZone,
OnInit,
OnDestroy,
} from '@angular/core';

import {isString} from '../util/util';
Expand Down Expand Up @@ -134,6 +136,7 @@ export class NgbPanel implements AfterContentChecked {
titleTpl: NgbPanelTitle;
headerTpl: NgbPanelHeader;
contentTpl: NgbPanelContent;
panelDiv: HTMLElement | null;

@ContentChildren(NgbPanelTitle, {descendants: false}) titleTpls: QueryList<NgbPanelTitle>;
@ContentChildren(NgbPanelHeader, {descendants: false}) headerTpls: QueryList<NgbPanelHeader>;
Expand Down Expand Up @@ -172,6 +175,16 @@ export interface NgbPanelChangeEvent {
preventDefault: () => void;
}

@Directive({selector: '[ngbRef]'})
export class NgbRefDirective implements OnInit, OnDestroy {
@Output() ngbRef = new EventEmitter<HTMLElement | null>();
constructor(private _El: ElementRef) {}

ngOnInit() { this.ngbRef.emit(this._El.nativeElement); }

ngOnDestroy() { this.ngbRef.emit(null); }
}

/**
* Accordion is a collection of collapsible panels (bootstrap cards).
*
Expand All @@ -195,7 +208,7 @@ export interface NgbPanelChangeEvent {
<ng-template [ngTemplateOutlet]="panel.headerTpl?.templateRef || t"
[ngTemplateOutletContext]="{$implicit: panel, opened: panel.isOpen}"></ng-template>
</div>
<div id="{{panel.id}}" role="tabpanel" [attr.aria-labelledby]="panel.id + '-header'"
<div id="{{panel.id}}" (ngbRef)="panel.panelDiv = $event" role="tabpanel" [attr.aria-labelledby]="panel.id + '-header'"
*ngIf="!destroyOnHide || panel.isOpen || panel.transitionRunning">
<div class="card-body">
<ng-template [ngTemplateOutlet]="panel.contentTpl?.templateRef || null"></ng-template>
Expand Down Expand Up @@ -265,9 +278,7 @@ export class NgbAccordion implements AfterContentChecked {
*/
@Output() hidden = new EventEmitter<string>();

constructor(
config: NgbAccordionConfig, private _element: ElementRef, private _ngZone: NgZone,
private _changeDetector: ChangeDetectorRef) {
constructor(config: NgbAccordionConfig, private _ngZone: NgZone, private _changeDetector: ChangeDetectorRef) {
this.animation = config.animation;
this.type = config.type;
this.closeOtherPanels = config.closeOthers;
Expand Down Expand Up @@ -344,7 +355,7 @@ export class NgbAccordion implements AfterContentChecked {
// Setup the initial classes here
this._ngZone.onStable.pipe(take(1)).subscribe(() => {
this.panels.forEach(panel => {
const panelElement = this._getPanelElement(panel.id);
const panelElement = panel.panelDiv;
if (panelElement) {
if (!panel.initClassDone) {
panel.initClassDone = true;
Expand Down Expand Up @@ -406,7 +417,7 @@ export class NgbAccordion implements AfterContentChecked {
// When panel.transitionRunning is true, the transition needs to be started OR reversed,
// The direction (show or hide) is choosen by each panel.isOpen state
if (panel.transitionRunning) {
const panelElement = this._getPanelElement(panel.id);
const panelElement = panel.panelDiv;
ngbRunTransition(this._ngZone, panelElement !, ngbCollapsingTransition, {
animation,
runningTransition: 'stop',
Expand All @@ -425,10 +436,6 @@ export class NgbAccordion implements AfterContentChecked {
}
});
}

private _getPanelElement(panelId: string): HTMLElement | null {
return this._element.nativeElement.querySelector('#' + panelId);
}
}

/**
Expand Down

0 comments on commit ab87fc7

Please sign in to comment.