Skip to content

Commit 68a0bac

Browse files
committed
feat: remove subscription
1 parent 6c7e235 commit 68a0bac

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

projects/components/src/multi-select/multi-select.component.ts

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from '@angular/core';
1212
import { IconType } from '@hypertrace/assets-library';
1313
import { queryListAndChanges$, SubscriptionLifecycle } from '@hypertrace/common';
14-
import { BehaviorSubject, combineLatest, EMPTY, Observable, Subject } from 'rxjs';
14+
import { BehaviorSubject, combineLatest, EMPTY, Observable, of, Subject } from 'rxjs';
1515
import { map } from 'rxjs/operators';
1616
import { ButtonRole, ButtonStyle } from '../button/button';
1717
import { IconSize } from '../icon/icon-size';
@@ -49,13 +49,14 @@ import { MultiSelectJustify } from './multi-select-justify';
4949
#triggerContainer
5050
>
5151
<ht-icon *ngIf="this.icon" [icon]="this.icon" [size]="this.iconSize"></ht-icon>
52-
<div *ngIf="!this.isIconOnlyMode()" class="trigger-label-container">
53-
<ht-label class="trigger-label" [label]="this.triggerLabel"></ht-label>
54-
<span *ngIf="this.selectedItemsCount > 1" class="trigger-more-items"
55-
>+{{ this.selectedItemsCount - 1 }}</span
56-
>
57-
<ht-icon class="trigger-icon" icon="${IconType.ChevronDown}" size="${IconSize.Small}"></ht-icon>
58-
</div>
52+
<ng-container *htLoadAsync="this.triggerValues$ as triggerValues">
53+
<div *ngIf="!this.isIconOnlyMode()" class="trigger-label-container">
54+
<ht-label class="trigger-label" [label]="triggerValues.label"></ht-label>
55+
<span *ngIf="triggerValues.selectedItemsCount > 1" class="trigger-more-items"
56+
>+{{ triggerValues.selectedItemsCount - 1 }}</span>
57+
<ht-icon class="trigger-icon" icon="${IconType.ChevronDown}" size="${IconSize.Small}"></ht-icon>
58+
</div>
59+
</ng-container>
5960
</div>
6061
</ht-popover-trigger>
6162
<ht-popover-content>
@@ -164,10 +165,7 @@ export class MultiSelectComponent<V> implements AfterContentInit, OnChanges {
164165
private readonly searchSubject: Subject<string> = new BehaviorSubject('');
165166

166167
public popoverOpen: boolean = false;
167-
public triggerLabel?: string;
168-
public selectedItemsCount: number = 0;
169-
170-
public constructor(private readonly subscriptionLifecycle: SubscriptionLifecycle) {}
168+
public triggerValues$: Observable<TriggerValues> = new Observable();
171169

172170
public ngAfterContentInit(): void {
173171
this.allOptions$ = this.allOptionsList !== undefined ? queryListAndChanges$(this.allOptionsList) : EMPTY;
@@ -239,24 +237,32 @@ export class MultiSelectComponent<V> implements AfterContentInit, OnChanges {
239237

240238
private setTriggerLabel(): void {
241239
if (this.triggerLabelDisplayMode === TriggerLabelDisplayMode.Placeholder) {
242-
this.triggerLabel = this.placeholder;
240+
this.triggerValues$ = of({
241+
label: this.placeholder,
242+
selectedItemsCount: 0
243+
});
243244

244245
return;
245246
}
246247

247-
this.subscriptionLifecycle.add(
248-
this.allOptions$?.subscribe(options => {
248+
this.triggerValues$ = this.allOptions$?.pipe(
249+
map(options => {
249250
const selectedItems: SelectOptionComponent<V>[] = options.filter(item => this.isSelectedItem(item));
250251

251-
this.selectedItemsCount = selectedItems?.length ?? 0;
252-
253-
// Trigger label is placeholder in case there is element selected on multiselect
254-
this.triggerLabel = this.selectedItemsCount === 0 ? this.placeholder : selectedItems[0]?.label;
252+
return {
253+
label: selectedItems.length === 0 ? this.placeholder : selectedItems[0]?.label,
254+
selectedItemsCount: selectedItems.length
255+
}
255256
})
256257
);
257258
}
258259
}
259260

261+
interface TriggerValues {
262+
label: string | undefined;
263+
selectedItemsCount: number;
264+
}
265+
260266
export const enum TriggerLabelDisplayMode {
261267
// These may be used as css classes
262268
Placeholder = 'placeholder-mode',

0 commit comments

Comments
 (0)