Skip to content

Commit 47029ac

Browse files
authored
AdvancedTable bug - Handle model updates (#2919)
1 parent 2f1cc2c commit 47029ac

File tree

5 files changed

+75
-4
lines changed

5 files changed

+75
-4
lines changed

.changeset/fresh-apes-return.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hashicorp/design-system-components": patch
3+
---
4+
5+
`AdvancedTable` - Fixed a bug that prevented the `model` from updating when the argument changes

packages/components/src/components/hds/advanced-table/index.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<div
66
class="hds-advanced-table__container
77
{{(if this.isStickyHeaderPinned 'hds-advanced-table__container--header-is-pinned')}}"
8+
{{this._didUpdateModel}}
89
...attributes
910
>
1011
{{! Caption }}

packages/components/src/components/hds/advanced-table/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { modifier } from 'ember-modifier';
1313
import type Owner from '@ember/owner';
1414

1515
import HdsAdvancedTableTableModel from './models/table.ts';
16+
1617
import {
1718
HdsAdvancedTableDensityValues,
1819
HdsAdvancedTableThSortOrderValues,
@@ -157,6 +158,7 @@ export default class HdsAdvancedTable extends Component<HdsAdvancedTableSignatur
157158
private _selectAllCheckbox?: HdsFormCheckboxBaseSignature['Element'] =
158159
undefined;
159160
@tracked private _isSelectAllCheckboxSelected?: boolean = undefined;
161+
160162
private _selectableRows: HdsAdvancedTableSelectableRow[] = [];
161163
private _captionId = 'caption-' + guidFor(this);
162164
private _tableModel!: HdsAdvancedTableTableModel;
@@ -393,6 +395,10 @@ export default class HdsAdvancedTable extends Component<HdsAdvancedTableSignatur
393395
return classes.join(' ');
394396
}
395397

398+
private _didUpdateModel = modifier(() => {
399+
this._tableModel.updateModel(this.args.model);
400+
});
401+
396402
private _setUpScrollWrapper = modifier((element: HTMLDivElement) => {
397403
this._scrollHandler = () => {
398404
// 6px as a buffer so the shadow doesn't appear over the border radius on the edge of the table

packages/components/src/components/hds/advanced-table/models/table.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import HdsAdvancedTableRow from './row.ts';
77
import { action } from '@ember/object';
8+
import { tracked } from '@glimmer/tracking';
89

910
import type {
1011
HdsAdvancedTableExpandState,
@@ -33,14 +34,15 @@ function getChildrenCount(rows: HdsAdvancedTableRow[]): number {
3334
}
3435

3536
export default class HdsAdvancedTableTableModel {
36-
rows: HdsAdvancedTableRow[] = [];
37+
@tracked rows: HdsAdvancedTableRow[] = [];
38+
39+
childrenKey?: string;
3740

3841
constructor(args: HdsAdvancedTableTableArgs) {
3942
const { model, childrenKey } = args;
4043

41-
this.rows = model.map((row) => {
42-
return new HdsAdvancedTableRow({ ...row, childrenKey });
43-
});
44+
this.childrenKey = childrenKey;
45+
this.updateModel(model);
4446
}
4547

4648
get totalRowCount(): number {
@@ -71,6 +73,13 @@ export default class HdsAdvancedTableTableModel {
7173
}
7274
}
7375

76+
@action
77+
updateModel(model: HdsAdvancedTableModel) {
78+
this.rows = model.map((row) => {
79+
return new HdsAdvancedTableRow({ ...row, childrenKey: this.childrenKey });
80+
});
81+
}
82+
7483
@action
7584
openAll() {
7685
this.rows.forEach((row) => row.openAll());

showcase/tests/integration/components/hds/advanced-table/index-test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,56 @@ module('Integration | Component | hds/advanced-table/index', function (hooks) {
498498
.hasText('Test 3 description');
499499
});
500500

501+
test('it should update the table when the model changes', async function (assert) {
502+
const bodySelector = '.hds-advanced-table__tbody';
503+
const rowSelector = '.hds-advanced-table__tr';
504+
const cellSelector = '.hds-advanced-table__td';
505+
506+
const getBodyContent = () => {
507+
return Array.from(
508+
document.querySelectorAll(`${bodySelector} ${rowSelector}`),
509+
).map((row) => {
510+
const cells = row.querySelectorAll(cellSelector);
511+
return Array.from(cells).map((cell) => cell.textContent.trim());
512+
});
513+
};
514+
515+
this.set('model', [
516+
{ name: 'Bob', age: 20, country: 'USA' },
517+
{ name: 'Alice', age: 25, country: 'UK' },
518+
{ name: 'Charlie', age: 30, country: 'Canada' },
519+
]);
520+
521+
await render(hbs`<Hds::AdvancedTable
522+
id='data-advanced-test-table'
523+
@model={{this.model}}
524+
@columns={{array
525+
(hash key='name' label='Name')
526+
(hash key='age' label='Age')
527+
(hash key='country' label='Country')
528+
}}
529+
>
530+
<:body as |B|>
531+
<B.Tr id={{B.rowIndex}}>
532+
<B.Td>{{B.data.name}}</B.Td>
533+
<B.Td>{{B.data.age}}</B.Td>
534+
<B.Td>{{B.data.country}}</B.Td>
535+
</B.Tr>
536+
</:body>
537+
</Hds::AdvancedTable>`);
538+
539+
assert.dom(`${bodySelector} ${rowSelector}`).exists({ count: 3 });
540+
assert.deepEqual(getBodyContent(), [
541+
['Bob', '20', 'USA'],
542+
['Alice', '25', 'UK'],
543+
['Charlie', '30', 'Canada'],
544+
]);
545+
546+
this.set('model', [{ name: 'Jane', age: 35, country: 'Mexico' }]);
547+
assert.dom(`${bodySelector} ${rowSelector}`).exists({ count: 1 });
548+
assert.deepEqual(getBodyContent(), [['Jane', '35', 'Mexico']]);
549+
});
550+
501551
// OPTIONS
502552

503553
// Sortable

0 commit comments

Comments
 (0)