Skip to content

Commit 0f5ff78

Browse files
HDS-4273 Add integration test for testing select all on multiselect after model update, add example of updating model with fewer rows, refactor Table component code to update rows instead of deleting duplicates
1 parent 705681f commit 0f5ff78

File tree

4 files changed

+262
-231
lines changed

4 files changed

+262
-231
lines changed

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

+5-16
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,11 @@ export default class HdsTable extends Component<HdsTableSignature> {
300300
);
301301

302302
if (selectionKey) {
303+
// Remove any existing item with the same `selectionKey` identifier (this may occur if the model is updated and the rows re-rendered)
304+
this._selectableRows = this._selectableRows.filter(
305+
(row): boolean => row.selectionKey !== selectionKey
306+
);
307+
// Add the new item
303308
this._selectableRows.push({ selectionKey, checkbox });
304309
}
305310
this.setSelectAllState();
@@ -314,22 +319,6 @@ export default class HdsTable extends Component<HdsTableSignature> {
314319
this._selectableRows.map((row): string => row.selectionKey)
315320
);
316321

317-
// Fix for selectableRows state not being maintained properly when model updates:
318-
// Delete the first row with the given selection key you find from the selectableRows array
319-
// Search for the index of the row to delete. If found, delete it.
320-
// (Fixes issue of not rows not being properly selectable including "select all" functionality)
321-
const rowToDeleteIndex = this._selectableRows.findIndex(
322-
(row): boolean => row.selectionKey === selectionKey
323-
);
324-
325-
if (rowToDeleteIndex > -1) {
326-
this._selectableRows.splice(rowToDeleteIndex, 1);
327-
}
328-
329-
// ORIGINAL:
330-
// this._selectableRows = this._selectableRows.filter(
331-
// (row): boolean => row.selectionKey !== selectionKey
332-
// );
333322
this.setSelectAllState();
334323
}
335324

showcase/app/controllers/components/table-multi-select-bug.js

+9
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ export default class extends Controller {
3535
.sort(() => Math.random() - 0.5);
3636
}
3737

38+
@action dirtyModelWithLessRows() {
39+
this.lyrics = this.lyrics
40+
// Deep copy to destroy references
41+
.map((r) => ({ ...r }))
42+
// Randomly sort to see what happens in this case
43+
.sort(() => Math.random() - 0.5)
44+
.filter((row) => row.key !== 'five');
45+
}
46+
3847
printSelection = (selection) =>
3948
console.log('Printing selection:', selection.selectedRowsKeys);
4049
}

showcase/app/templates/components/table-multi-select-bug.hbs

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222

2323
<Shw::Text::H2>This will work until you click the "Force new data" button</Shw::Text::H2>
2424

25-
<Hds::Button @text="Force new data" {{on "click" this.dirtyModel}} />
25+
<p>
26+
<Hds::Button @text="Force new data" {{on "click" this.dirtyModel}} @isInline={{true}} />
27+
<Hds::Button @text="Force new data with fewer rows" {{on "click" this.dirtyModelWithLessRows}} @isInline={{true}} />
28+
</p>
2629

2730
<Hds::Table
2831
@model={{this.lyrics}}

0 commit comments

Comments
 (0)