From 4621e95481fc48b682c46d75d94b5af66426368e Mon Sep 17 00:00:00 2001 From: Dan Percic Date: Mon, 22 Nov 2021 21:23:29 +0200 Subject: [PATCH] fix item select --- src/lib/listing/services/listing.service.ts | 2 +- .../table-item/table-item.component.ts | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/lib/listing/services/listing.service.ts b/src/lib/listing/services/listing.service.ts index 4f348fd..4f713b1 100644 --- a/src/lib/listing/services/listing.service.ts +++ b/src/lib/listing/services/listing.service.ts @@ -99,7 +99,7 @@ export class ListingService { isSelected$(entity: E): Observable { return this._selected$.pipe( any(selectedId => selectedId === entity.id), - shareDistinctLast(), + shareLast(), ); } diff --git a/src/lib/listing/table-content/table-item/table-item.component.ts b/src/lib/listing/table-content/table-item/table-item.component.ts index 547f699..8c25551 100644 --- a/src/lib/listing/table-content/table-item/table-item.component.ts +++ b/src/lib/listing/table-content/table-item/table-item.component.ts @@ -1,8 +1,9 @@ -import { ChangeDetectionStrategy, Component, forwardRef, Inject, Input, OnInit } from '@angular/core'; -import { Observable } from 'rxjs'; +import { ChangeDetectionStrategy, Component, forwardRef, Inject, Input, OnChanges } from '@angular/core'; +import { BehaviorSubject, Observable } from 'rxjs'; import { ListingComponent } from '../..'; import { IListable } from '../../models'; import { ListingService } from '../../services'; +import { switchMap } from 'rxjs/operators'; @Component({ selector: 'iqser-table-item', @@ -10,18 +11,21 @@ import { ListingService } from '../../services'; styleUrls: ['./table-item.component.scss'], changeDetection: ChangeDetectionStrategy.OnPush, }) -export class TableItemComponent implements OnInit { +export class TableItemComponent implements OnChanges { @Input() entity!: T; @Input() selectionEnabled = false; isSelected$!: Observable; + private readonly _entityChanged$ = new BehaviorSubject(this.entity); constructor( @Inject(forwardRef(() => ListingComponent)) readonly listingComponent: ListingComponent, readonly listingService: ListingService, - ) {} + ) { + this.isSelected$ = this._entityChanged$.pipe(switchMap(entity => this.listingService.isSelected$(entity))); + } - ngOnInit(): void { - this.isSelected$ = this.listingService.isSelected$(this.entity); + ngOnChanges(): void { + this._entityChanged$.next(this.entity); } }