fix item select

This commit is contained in:
Dan Percic 2021-11-22 21:23:29 +02:00
parent 5d6e3053fe
commit 4621e95481
2 changed files with 11 additions and 7 deletions

View File

@ -99,7 +99,7 @@ export class ListingService<E extends IListable> {
isSelected$(entity: E): Observable<boolean> {
return this._selected$.pipe(
any(selectedId => selectedId === entity.id),
shareDistinctLast(),
shareLast(),
);
}

View File

@ -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<T extends IListable> implements OnInit {
export class TableItemComponent<T extends IListable> implements OnChanges {
@Input() entity!: T;
@Input() selectionEnabled = false;
isSelected$!: Observable<boolean>;
private readonly _entityChanged$ = new BehaviorSubject<T>(this.entity);
constructor(
@Inject(forwardRef(() => ListingComponent)) readonly listingComponent: ListingComponent<T>,
readonly listingService: ListingService<T>,
) {}
) {
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);
}
}