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

Backport PR #321 on branch 1.x (Add collapse and expand methods to AccordionPanel) #326

Merged
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
70 changes: 53 additions & 17 deletions packages/widgets/src/accordionpanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,38 @@ export class AccordionPanel extends SplitPanel {
widget.title.changed.connect(this._onTitleChanged, this);
}

/**
* Collapse the widget at position `index`.
*
* #### Notes
* If no widget is found for `index`, this will bail.
*
* @param index Widget index
*/
collapse(index: number): void {
const widget = (this.layout as AccordionLayout).widgets[index];

if (widget && !widget.isHidden) {
this._toggleExpansion(index);
}
}

/**
* Expand the widget at position `index`.
*
* #### Notes
* If no widget is found for `index`, this will bail.
*
* @param index Widget index
*/
expand(index: number): void {
const widget = (this.layout as AccordionLayout).widgets[index];

if (widget && widget.isHidden) {
this._toggleExpansion(index);
}
}

/**
* Insert a widget at the specified index.
*
Expand Down Expand Up @@ -221,23 +253,7 @@ export class AccordionPanel extends SplitPanel {
if (index >= 0) {
event.preventDefault();
event.stopPropagation();
const title = this.titles[index];
const widget = (this.layout as AccordionLayout).widgets[index];

const newSize = this._computeWidgetSize(index);
if (newSize) {
this.setRelativeSizes(newSize, false);
}

if (widget.isHidden) {
title.classList.add('lm-mod-expanded');
title.setAttribute('aria-expanded', 'true');
widget.show();
} else {
title.classList.remove('lm-mod-expanded');
title.setAttribute('aria-expanded', 'false');
widget.hide();
}
this._toggleExpansion(index);
}
}
}
Expand Down Expand Up @@ -296,6 +312,26 @@ export class AccordionPanel extends SplitPanel {
}
}

private _toggleExpansion(index: number) {
const title = this.titles[index];
const widget = (this.layout as AccordionLayout).widgets[index];

const newSize = this._computeWidgetSize(index);
if (newSize) {
this.setRelativeSizes(newSize, false);
}

if (widget.isHidden) {
title.classList.add('lm-mod-expanded');
title.setAttribute('aria-expanded', 'true');
widget.show();
} else {
title.classList.remove('lm-mod-expanded');
title.setAttribute('aria-expanded', 'false');
widget.hide();
}
}

private _widgetSizesCache: WeakMap<Widget, number> = new WeakMap();
}

Expand Down
63 changes: 63 additions & 0 deletions packages/widgets/tests/src/accordionpanel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,69 @@ describe('@lumino/widgets', () => {
});
});

describe('#collapse()', () => {
let panel: AccordionPanel;
let layout: AccordionLayout;

beforeEach(() => {
panel = new AccordionPanel();
layout = panel.layout as AccordionLayout;
let widgets = [new Widget(), new Widget(), new Widget()];
widgets.forEach(w => {
panel.addWidget(w);
});
panel.setRelativeSizes([10, 10, 10, 20]);
Widget.attach(panel, document.body);
MessageLoop.flush();
});

afterEach(() => {
panel.dispose();
});

it('should collapse an expanded widget', () => {
panel.collapse(1);

expect(layout.titles[1].getAttribute('aria-expanded')).to.equal(
'false'
);
expect(layout.titles[1].classList.contains('lm-mod-expanded')).to.be
.false;
expect(layout.widgets[1].isHidden).to.be.true;
});
});

describe('#expand()', () => {
let panel: AccordionPanel;
let layout: AccordionLayout;

beforeEach(() => {
panel = new AccordionPanel();
layout = panel.layout as AccordionLayout;
let widgets = [new Widget(), new Widget(), new Widget()];
widgets.forEach(w => {
panel.addWidget(w);
});
panel.setRelativeSizes([10, 10, 10, 20]);
Widget.attach(panel, document.body);
MessageLoop.flush();
});

afterEach(() => {
panel.dispose();
});

it('should expand a collapsed widget', () => {
panel.collapse(1);
panel.expand(1);

expect(layout.titles[0].getAttribute('aria-expanded')).to.equal('true');
expect(layout.titles[0].classList.contains('lm-mod-expanded')).to.be
.true;
expect(layout.widgets[0].isHidden).to.be.false;
});
});

describe('#handleEvent()', () => {
let panel: LogAccordionPanel;
let layout: AccordionLayout;
Expand Down