Skip to content

Commit

Permalink
Add remove() to StatsWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaac Brodsky committed Oct 18, 2021
1 parent d22d537 commit 14bfda5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions modules/stats-widget/src/stats-widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export default class StatsWidget {
_statsContainer: HTMLElement = null;
_header: HTMLElement = null;
_items = {};
_added: boolean = false;

constructor(stats: Stats, options?: StatWidgetProps) {
this.stats = stats;
Expand Down Expand Up @@ -104,6 +105,16 @@ export default class StatsWidget {
this._update();
}

/**
* Remove the stats widget from the container it was added to.
* The stats widget cannot be reused after this is called.
*/
remove(): void {
// if re-adding the stats widget is needed, a code path to
// re-add the _innerContainer should be added, e.g. in _update.
this._container.removeChild(this._innerContainer);
}

setCollapsed(collapsed: boolean): void {
this.collapsed = collapsed;
if (this._statsContainer) {
Expand Down
11 changes: 11 additions & 0 deletions modules/stats-widget/test/stats-widget.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,14 @@ test('StatsWidget#resetOnUpdate', t => {

t.end();
});

test('StatsWidget#remove', t => {
const container = _global.document.createElement('div');
container.id = 'test-stats-widget-container';
const statsWidget = new StatsWidget(null, {container});
t.ok(statsWidget._container === container);
t.equals(statsWidget._container.childNodes.length, 1, 'Should have 1 child node.');
statsWidget.remove();
t.equals(statsWidget._container.childNodes.length, 0, 'Should have 0 child nodes.');
t.end();
});

0 comments on commit 14bfda5

Please sign in to comment.