-
Notifications
You must be signed in to change notification settings - Fork 77
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
Conversation
…gle selection modes
packages/calcite-components/src/components/tile-group/tile-group.tsx
Outdated
Show resolved
Hide resolved
There was a problem hiding this 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
.
@@ -492,5 +492,41 @@ describe("calcite-tile-group", () => { | |||
|
|||
await selectedItemAsserter([]); | |||
}); | |||
|
|||
it("single selection mode allows only one tile with selected attribute", async () => { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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 ?? []; |
There was a problem hiding this comment.
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.
Related Issue: #9271
Summary
Displayed only the last tile as selected when multiple tiles have the selected attribute.