Skip to content

Commit

Permalink
fix(chip-group): Add existence checks (#7586)
Browse files Browse the repository at this point in the history
**Related Issue:** #7585 

## Summary
Adds check for existence with optional chaining.
  • Loading branch information
macandcheese committed Aug 23, 2023
1 parent f8a1b91 commit 5ca64f1
Showing 1 changed file with 7 additions and 7 deletions.
Expand Up @@ -125,7 +125,7 @@ export class ChipGroup implements InteractiveComponent {
@Listen("calciteInternalChipKeyEvent")
calciteInternalChipKeyEventListener(event: CustomEvent): void {
if (event.composedPath().includes(this.el)) {
const interactiveItems = this.items.filter((el) => !el.disabled);
const interactiveItems = this.items?.filter((el) => !el.disabled);
switch (event.detail.key) {
case "ArrowRight":
focusElementInGroup(interactiveItems, event.detail.target, "next");
Expand All @@ -146,16 +146,16 @@ export class ChipGroup implements InteractiveComponent {
@Listen("calciteChipClose")
calciteChipCloseListener(event: CustomEvent): void {
const item = event.target as HTMLCalciteChipElement;
if (this.items.includes(item)) {
if (this.items.indexOf(item) > 0) {
if (this.items?.includes(item)) {
if (this.items?.indexOf(item) > 0) {
focusElementInGroup(this.items, item as HTMLCalciteChipElement, "previous");
} else if (this.items.indexOf(item) === 0) {
} else if (this.items?.indexOf(item) === 0) {
focusElementInGroup(this.items, item as HTMLCalciteChipElement, "next");
} else {
focusElementInGroup(this.items, item as HTMLCalciteChipElement, "first");
}
}
this.items = this.items.filter((el) => el !== item);
this.items = this.items?.filter((el) => el !== item);
}

@Listen("calciteChipSelect")
Expand Down Expand Up @@ -205,7 +205,7 @@ export class ChipGroup implements InteractiveComponent {

private setSelectedItems = (emit: boolean, elToMatch?: HTMLCalciteChipElement): void => {
if (elToMatch) {
this.items.forEach((el) => {
this.items?.forEach((el) => {
const matchingEl = elToMatch === el;
switch (this.selectionMode) {
case "multiple":
Expand All @@ -225,7 +225,7 @@ export class ChipGroup implements InteractiveComponent {
});
}

this.selectedItems = this.items.filter((el) => el.selected);
this.selectedItems = this.items?.filter((el) => el.selected);

if (emit) {
this.calciteChipGroupSelect.emit();
Expand Down

0 comments on commit 5ca64f1

Please sign in to comment.