From 72b3a8269e2fdbb164e0347ce66bc406208daeb4 Mon Sep 17 00:00:00 2001 From: Ramon Spahr Date: Tue, 8 Jun 2021 16:36:28 +0200 Subject: [PATCH] 218: improve clicking & sorting of multiselect dropdowns --- .../edit-absences-header.component.ts | 3 ++ .../multiselect/multiselect.component.html | 10 ++-- .../multiselect/multiselect.component.ts | 53 +++---------------- 3 files changed, 15 insertions(+), 51 deletions(-) diff --git a/src/app/edit-absences/components/edit-absences-header/edit-absences-header.component.ts b/src/app/edit-absences/components/edit-absences-header/edit-absences-header.component.ts index 5507585d5..bc23680f4 100644 --- a/src/app/edit-absences/components/edit-absences-header/edit-absences-header.component.ts +++ b/src/app/edit-absences/components/edit-absences-header/edit-absences-header.component.ts @@ -23,6 +23,7 @@ import { DateParserFormatter } from 'src/app/shared/services/date-parser-formatt import { addGroupToDropdownItem, createPresenceTypesDropdownItems, + sortPresenceTypes, } from 'src/app/shared/utils/presence-types'; import { EducationalEventsRestService } from '../../../shared/services/educational-events-rest.service'; import { StudentsRestService } from 'src/app/shared/services/students-rest.service'; @@ -71,6 +72,7 @@ export class EditAbsencesHeaderComponent implements OnInit { map((presenceTypes) => presenceTypes.filter(not(isComment)).filter(not(isIncident)) ), + map(sortPresenceTypes), map(createPresenceTypesDropdownItems), map((i) => addGroupToDropdownItem( @@ -82,6 +84,7 @@ export class EditAbsencesHeaderComponent implements OnInit { incidentTypesGrouped$ = this.state.presenceTypes$.pipe( map((presenceTypes) => presenceTypes.filter(isIncident)), + map(sortPresenceTypes), map(createPresenceTypesDropdownItems), map((i) => addGroupToDropdownItem( diff --git a/src/app/shared/components/multiselect/multiselect.component.html b/src/app/shared/components/multiselect/multiselect.component.html index 6b5c0d757..79acb3912 100644 --- a/src/app/shared/components/multiselect/multiselect.component.html +++ b/src/app/shared/components/multiselect/multiselect.component.html @@ -6,9 +6,11 @@ [selectableGroup]="true" [selectableGroupAsModel]="false" [closeOnSelect]="false" - [ngModel]="values$ | async" - (change)="onChange($event)" + [(ngModel)]="values" placeholder="{{ 'shared.select.empty-option' | translate }}" + bindValue="Key" + bindLabel="Value" + (click)="itemsChanged()" > @@ -27,7 +29,7 @@ let-item$="item$" let-index="index" > - + {{ item.Group }} @@ -38,7 +40,7 @@ let-item$="item$" let-index="index" > - + {{ item.Value }} diff --git a/src/app/shared/components/multiselect/multiselect.component.ts b/src/app/shared/components/multiselect/multiselect.component.ts index ccc695d71..9d43cfb2c 100644 --- a/src/app/shared/components/multiselect/multiselect.component.ts +++ b/src/app/shared/components/multiselect/multiselect.component.ts @@ -1,61 +1,20 @@ -import { - Component, - OnInit, - Input, - Output, - EventEmitter, - OnChanges, - SimpleChanges, -} from '@angular/core'; +import { Component, Input, Output, EventEmitter } from '@angular/core'; import { DropDownGroupedItem } from '../../models/drop-down-grouped-item.model'; -import { BehaviorSubject, combineLatest } from 'rxjs'; -import { map } from 'rxjs/operators'; @Component({ selector: 'erz-multiselect', templateUrl: './multiselect.component.html', styleUrls: ['./multiselect.component.scss'], }) -export class MultiselectComponent implements OnInit, OnChanges { +export class MultiselectComponent { @Input() options: ReadonlyArray = []; - @Input() values: Option = null; - @Output() valuesChange = new EventEmitter[]>(); - - options$ = new BehaviorSubject>([]); - rawValues$ = new BehaviorSubject>(null); - - values$ = combineLatest([this.rawValues$, this.options$]).pipe( - map( - ([rawValues, options]) => - (options && - rawValues && - options.filter((o: DropDownGroupedItem) => - rawValues.includes(o.Key) - )) || - null - ) - ); + @Input() values: Option = []; + @Output() valuesChange = new EventEmitter>(); constructor() {} - ngOnInit(): void {} - - ngOnChanges(changes: SimpleChanges): void { - if (changes.values) { - this.rawValues$.next(changes.values.currentValue); - } - if (changes.options) { - this.options$.next(changes.options.currentValue); - } - } - - onChange(newValue: DropDownGroupedItem[]): void { - let value: number[] = []; - if (newValue && Array.isArray(newValue)) { - value = newValue // emit array with only keys - .map((v) => v?.Key); - } - this.valuesChange.emit(value); + itemsChanged(): void { + this.valuesChange.emit(this.values); } }