Skip to content

Commit 82e45aa

Browse files
author
Steven Silvester
authored
Merge pull request #249 from hbcarlos/update_title
Update title in AccordionPanel
2 parents 705ffa9 + bcd34f6 commit 82e45aa

File tree

3 files changed

+75
-7
lines changed

3 files changed

+75
-7
lines changed

packages/widgets/src/accordionlayout.ts

+21-7
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ export class AccordionLayout extends SplitLayout {
6868
*/
6969
readonly renderer: AccordionLayout.IRenderer;
7070

71+
public updateTitle(index: number, widget: Widget): void {
72+
const oldTitle = this._titles[index];
73+
const expanded = oldTitle.classList.contains('lm-mod-expanded');
74+
const newTitle = Private.createTitle(this.renderer, widget.title, expanded);
75+
this._titles[index] = newTitle;
76+
77+
// Add the title node to the parent before the widget.
78+
this.parent!.node.replaceChild(newTitle, oldTitle);
79+
}
80+
7181
/**
7282
* Attach a widget to the parent's DOM node.
7383
*
@@ -77,11 +87,6 @@ export class AccordionLayout extends SplitLayout {
7787
*/
7888
protected attachWidget(index: number, widget: Widget): void {
7989
const title = Private.createTitle(this.renderer, widget.title);
80-
title.style.position = 'absolute';
81-
title.setAttribute('aria-label', `${widget.title.label} Section`);
82-
title.setAttribute('aria-expanded', 'true');
83-
title.setAttribute('aria-controls', widget.id);
84-
title.classList.add('lm-mod-expanded');
8590

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

@@ -226,8 +231,17 @@ namespace Private {
226231
*/
227232
export function createTitle(
228233
renderer: AccordionLayout.IRenderer,
229-
data: Title<Widget>
234+
data: Title<Widget>,
235+
expanded: boolean = true
230236
): HTMLElement {
231-
return renderer.createSectionTitle(data);
237+
const title = renderer.createSectionTitle(data);
238+
title.style.position = 'absolute';
239+
title.setAttribute('aria-label', `${data.label} Section`);
240+
title.setAttribute('aria-expanded', expanded ? 'true' : 'false');
241+
title.setAttribute('aria-controls', data.owner.id);
242+
if (expanded) {
243+
title.classList.add('lm-mod-expanded');
244+
}
245+
return title;
232246
}
233247
}

packages/widgets/src/accordionpanel.ts

+42
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,34 @@ export class AccordionPanel extends SplitPanel {
5353
return (this.layout as AccordionLayout).titles;
5454
}
5555

56+
/**
57+
* Add a widget to the end of the panel.
58+
*
59+
* @param widget - The widget to add to the panel.
60+
*
61+
* #### Notes
62+
* If the widget is already contained in the panel, it will be moved.
63+
*/
64+
addWidget(widget: Widget): void {
65+
super.addWidget(widget);
66+
widget.title.changed.connect(this._onTitleChanged, this);
67+
}
68+
69+
/**
70+
* Insert a widget at the specified index.
71+
*
72+
* @param index - The index at which to insert the widget.
73+
*
74+
* @param widget - The widget to insert into to the panel.
75+
*
76+
* #### Notes
77+
* If the widget is already contained in the panel, it will be moved.
78+
*/
79+
insertWidget(index: number, widget: Widget): void {
80+
super.insertWidget(index, widget);
81+
widget.title.changed.connect(this._onTitleChanged, this);
82+
}
83+
5684
/**
5785
* Handle the DOM events for the accordion panel.
5886
*
@@ -93,6 +121,20 @@ export class AccordionPanel extends SplitPanel {
93121
this.node.removeEventListener('keydown', this);
94122
}
95123

124+
/**
125+
* Handle the `changed` signal of a title object.
126+
*/
127+
private _onTitleChanged(sender: Title<Widget>): void {
128+
const index = ArrayExt.findFirstIndex(this.widgets, widget => {
129+
return widget.contains(sender.owner);
130+
});
131+
132+
if (index >= 0) {
133+
(this.layout as AccordionLayout).updateTitle(index, sender.owner);
134+
this.update();
135+
}
136+
}
137+
96138
/**
97139
* Handle the `'click'` event for the accordion panel
98140
*/

packages/widgets/tests/src/accordionpanel.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,18 @@ describe('@lumino/widgets', () => {
123123
});
124124
expect(panel.titles.length).to.equal(widgets.length);
125125
});
126+
127+
it('should update the title element', () => {
128+
const text = 'Something';
129+
let panel = new AccordionPanel();
130+
let widget = new Widget();
131+
panel.addWidget(widget);
132+
widget.title.label = text;
133+
const el = panel.titles[0].querySelector(
134+
'.lm-AccordionPanel-titleLabel'
135+
)!;
136+
expect(el.textContent).to.equal(text);
137+
});
126138
});
127139

128140
describe('#handleEvent()', () => {

0 commit comments

Comments
 (0)