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

Update title in AccordionPanel #249

Merged
merged 3 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
28 changes: 21 additions & 7 deletions packages/widgets/src/accordionlayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ export class AccordionLayout extends SplitLayout {
*/
readonly renderer: AccordionLayout.IRenderer;

public updateTitle(index: number, widget: Widget): void {
const oldTitle = this._titles[index];
const expanded = oldTitle.classList.contains('lm-mod-expanded');
const newTitle = Private.createTitle(this.renderer, widget.title, expanded);
this._titles[index] = newTitle;

// Add the title node to the parent before the widget.
this.parent!.node.replaceChild(newTitle, oldTitle);
}

/**
* Attach a widget to the parent's DOM node.
*
Expand All @@ -77,11 +87,6 @@ export class AccordionLayout extends SplitLayout {
*/
protected attachWidget(index: number, widget: Widget): void {
const title = Private.createTitle(this.renderer, widget.title);
title.style.position = 'absolute';
title.setAttribute('aria-label', `${widget.title.label} Section`);
title.setAttribute('aria-expanded', 'true');
title.setAttribute('aria-controls', widget.id);
title.classList.add('lm-mod-expanded');

ArrayExt.insert(this._titles, index, title);

Expand Down Expand Up @@ -226,8 +231,17 @@ namespace Private {
*/
export function createTitle(
renderer: AccordionLayout.IRenderer,
data: Title<Widget>
data: Title<Widget>,
expanded: boolean = true
): HTMLElement {
return renderer.createSectionTitle(data);
const title = renderer.createSectionTitle(data);
title.style.position = 'absolute';
title.setAttribute('aria-label', `${data.label} Section`);
title.setAttribute('aria-expanded', expanded ? 'true' : 'false');
title.setAttribute('aria-controls', data.owner.id);
if (expanded) {
title.classList.add('lm-mod-expanded');
}
return title;
}
}
42 changes: 42 additions & 0 deletions packages/widgets/src/accordionpanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,34 @@ export class AccordionPanel extends SplitPanel {
return (this.layout as AccordionLayout).titles;
}

/**
* Add a widget to the end of the panel.
*
* @param widget - The widget to add to the panel.
*
* #### Notes
* If the widget is already contained in the panel, it will be moved.
*/
addWidget(widget: Widget): void {
super.addWidget(widget);
widget.title.changed.connect(this._onTitleChanged, this);
}

/**
* Insert a widget at the specified index.
*
* @param index - The index at which to insert the widget.
*
* @param widget - The widget to insert into to the panel.
*
* #### Notes
* If the widget is already contained in the panel, it will be moved.
*/
insertWidget(index: number, widget: Widget): void {
super.insertWidget(index, widget);
widget.title.changed.connect(this._onTitleChanged, this);
}

/**
* Handle the DOM events for the accordion panel.
*
Expand Down Expand Up @@ -93,6 +121,20 @@ export class AccordionPanel extends SplitPanel {
this.node.removeEventListener('keydown', this);
}

/**
* Handle the `changed` signal of a title object.
*/
private _onTitleChanged(sender: Title<Widget>): void {
const index = ArrayExt.findFirstIndex(this.widgets, widget => {
return widget.contains(sender.owner);
});

if (index >= 0) {
(this.layout as AccordionLayout).updateTitle(index, sender.owner);
this.update();
}
}

/**
* Handle the `'click'` event for the accordion panel
*/
Expand Down