From 15b77166203e3adca57d70c5b1ae66287afe4738 Mon Sep 17 00:00:00 2001 From: Victoria League Date: Thu, 26 Sep 2024 11:52:10 -0400 Subject: [PATCH] [CL-459] Fix chip behavior when opening menu while item is selected (#11227) --- .../src/chip-select/chip-select.component.ts | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/libs/components/src/chip-select/chip-select.component.ts b/libs/components/src/chip-select/chip-select.component.ts index 2e6720a3dee..9862ea22c2e 100644 --- a/libs/components/src/chip-select/chip-select.component.ts +++ b/libs/components/src/chip-select/chip-select.component.ts @@ -108,8 +108,20 @@ export class ChipSelectComponent implements ControlValueAccessor, A return this.selectedOption?.icon || this.placeholderIcon; } + /** + * Set the rendered options based on whether or not an option is already selected, so that the correct + * submenu displays. + */ + protected setOrResetRenderedOptions(): void { + this.renderedOptions = this.selectedOption + ? this.selectedOption.children?.length > 0 + ? this.selectedOption + : this.getParent(this.selectedOption) + : this.rootTree; + } + protected handleMenuClosed(): void { - this.renderedOptions = this.selectedOption ?? this.rootTree; + this.setOrResetRenderedOptions(); } protected selectOption(option: ChipSelectOption, _event: MouseEvent) { @@ -197,11 +209,7 @@ export class ChipSelectComponent implements ControlValueAccessor, A /** Implemented as part of NG_VALUE_ACCESSOR */ writeValue(obj: T): void { this.selectedOption = this.findOption(this.rootTree, obj); - - /** Update the rendered options for next time the menu is opened */ - this.renderedOptions = this.selectedOption - ? this.getParent(this.selectedOption) - : this.rootTree; + this.setOrResetRenderedOptions(); } /** Implemented as part of NG_VALUE_ACCESSOR */