Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

218: improve clicking & sorting of multiselect dropdowns #300

Merged
merged 1 commit into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
10 changes: 6 additions & 4 deletions src/app/shared/components/multiselect/multiselect.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -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()"
>
<!-- selected items -->
<ng-template ng-multi-label-tmp let-items="items" let-clear="clear">
Expand All @@ -27,7 +29,7 @@
let-item$="item$"
let-index="index"
>
<input id="{{ index }}" type="checkbox" [ngModel]="item$.selected" />
<input id="item-{{ index }}" type="checkbox" [ngModel]="item$.selected" />
{{ item.Group }}
</ng-template>

Expand All @@ -38,7 +40,7 @@
let-item$="item$"
let-index="index"
>
<input id="{{ index }}" type="checkbox" [ngModel]="item$.selected" />
<input id="item-{{ index }}" type="checkbox" [ngModel]="item$.selected" />
{{ item.Value }}
</ng-template>
</ng-select>
53 changes: 6 additions & 47 deletions src/app/shared/components/multiselect/multiselect.component.ts
Original file line number Diff line number Diff line change
@@ -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<DropDownGroupedItem> = [];
@Input() values: Option<number> = null;
@Output() valuesChange = new EventEmitter<Option<number>[]>();

options$ = new BehaviorSubject<ReadonlyArray<DropDownGroupedItem>>([]);
rawValues$ = new BehaviorSubject<Option<number[]>>(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<number[]> = [];
@Output() valuesChange = new EventEmitter<Option<number[]>>();

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);
}
}