Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tile-group): disallow multiple selected attributes on single selection modes #9284

Merged
merged 3 commits into from
May 9, 2024

Conversation

josercarcamo
Copy link
Contributor

Related Issue: #9271

Summary

Displayed only the last tile as selected when multiple tiles have the selected attribute.

@josercarcamo josercarcamo requested a review from a team as a code owner May 8, 2024 01:44
@github-actions github-actions bot added the bug Bug reports for broken functionality. Issues should include a reproduction of the bug. label May 8, 2024
@josercarcamo josercarcamo changed the title fix(tile-group): disallow selected attribute on multiple tiles in sin… fix(tile-group): disallow multiple selected attributes on single selection modes May 8, 2024
Copy link
Contributor

@eriklharper eriklharper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks @josercarcamo ! Just left a suggestion that can save on an unnecessary extra re-render that occurs when setting selectedItems.

@eriklharper eriklharper added the low risk Issues with low risk for consideration in low risk milestones label May 8, 2024
@eriklharper eriklharper added the pr ready for visual snapshots Adding this label will run visual snapshot testing. label May 9, 2024
@josercarcamo josercarcamo merged commit 885909e into main May 9, 2024
15 checks passed
@josercarcamo josercarcamo deleted the josercarcamo/9271-multiple-tiles-selected branch May 9, 2024 00:11
@@ -492,5 +492,41 @@ describe("calcite-tile-group", () => {

await selectedItemAsserter([]);
});

it("single selection mode allows only one tile with selected attribute", async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@josercarcamo These new tests share a bit of code, so could you look into DRYing them up?

@@ -151,7 +151,20 @@ export class TileGroup implements InteractiveComponent, SelectableGroupComponent
};

private updateSelectedItems = (): void => {
this.selectedItems = this.items?.filter((el) => el.selected);
const selectedItems = this.items?.filter((el) => el.selected);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eriklharper Can items be null? It seems like it's guaranteed as it's initialized as [] and I think getSlottedTiles would always return an array. This would help us remove a lot of guards in the component.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this.items can be undefined since its populated by the slotted tiles and that can't always be guaranteed. getSlottedTiles doesn't do anything extra to prevent from returning undefined in some cases, but we could add that in I suppose.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's worth getting rid of those guards by updating getSlottedTiles to always return an array.

) {
this.selectedItems = [selectedItems.pop()];
this.items?.forEach((el) => {
if (this.selectedItems.indexOf(el) === -1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion to simplify: if (this.selectedItems.includes(el)) {

}
});
} else {
this.selectedItems = selectedItems ?? [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If items is guaranteed, we can eliminate the fallback empty array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug reports for broken functionality. Issues should include a reproduction of the bug. low risk Issues with low risk for consideration in low risk milestones pr ready for visual snapshots Adding this label will run visual snapshot testing.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants