Skip to content

Commit

Permalink
fix(list): update selectedItems property on all item selection changes (
Browse files Browse the repository at this point in the history
#7204)

**Related Issue:** #7202

## Summary

- Updates `selectedItems` when an item's selection is toggled.
- Fixes current logic which depends on value being `true`.
- Adds test coverage
  • Loading branch information
driskull authored Jun 21, 2023
1 parent 47620ba commit da048f6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,8 @@ export class ListItem
@Prop({ reflect: true, mutable: true }) selected = false;

@Watch("selected")
handleSelectedChange(value: boolean): void {
if (value) {
this.calciteInternalListItemSelect.emit();
}
handleSelectedChange(): void {
this.calciteInternalListItemSelect.emit();
}

/**
Expand Down
12 changes: 12 additions & 0 deletions packages/calcite-components/src/components/list/list.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,5 +328,17 @@ describe("calcite-list", () => {
await calciteListChangeEvent2;
expect(await listItemOne.getProperty("selected")).toBe(false);
expect(await list.getProperty("selectedItems")).toHaveLength(0);

listItemOne.setProperty("selected", true);
await page.waitForChanges();
await page.waitForTimeout(listDebounceTimeout);
expect(await listItemOne.getProperty("selected")).toBe(true);
expect(await list.getProperty("selectedItems")).toHaveLength(1);

listItemOne.setProperty("selected", false);
await page.waitForChanges();
await page.waitForTimeout(listDebounceTimeout);
expect(await listItemOne.getProperty("selected")).toBe(false);
expect(await list.getProperty("selectedItems")).toHaveLength(0);
});
});
8 changes: 3 additions & 5 deletions packages/calcite-components/src/components/list/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,9 @@ export class List implements InteractiveComponent, LoadableComponent {
const target = event.target as HTMLCalciteListItemElement;
const { listItems, selectionMode } = this;

listItems.forEach((listItem) => {
if (selectionMode === "single" || selectionMode === "single-persist") {
listItem.selected = listItem === target;
}
});
if (target.selected && (selectionMode === "single" || selectionMode === "single-persist")) {
listItems.forEach((listItem) => (listItem.selected = listItem === target));
}

this.updateSelectedItems();
}
Expand Down

0 comments on commit da048f6

Please sign in to comment.