-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput-dropdown.component.ts
More file actions
102 lines (89 loc) · 2.93 KB
/
input-dropdown.component.ts
File metadata and controls
102 lines (89 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import {
ChangeDetectionStrategy,
Component,
ContentChildren,
Input,
QueryList,
ViewChild,
booleanAttribute,
numberAttribute,
} from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatFormFieldModule } from '@angular/material/form-field';
import {
MatOption,
MatSelect,
MatSelectModule,
} from '@angular/material/select';
import { InputLoadingComponent } from '../input-loading/input-loading.component';
import { InputDirective } from '../input.directive';
import { InputAppearance } from '../types';
import { InputDropdownOptionGroupComponent } from './input-dropdown-option-group.component';
import { InputDropdownOptionComponent } from './input-dropdown-option.component';
@Component({
selector: 'pro-input-dropdown[label]',
templateUrl: './input-dropdown.component.html',
imports: [
FormsModule,
InputLoadingComponent,
MatFormFieldModule,
MatSelectModule,
ReactiveFormsModule,
],
styleUrl: './input-dropdown.component.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
})
export class InputDropdownComponent<T> extends InputDirective<T> {
@Input() public appearance: InputAppearance = 'outline';
@Input({ transform: numberAttribute })
public max: number | string | undefined;
@Input({ transform: booleanAttribute })
public multiple = false;
@ContentChildren(InputDropdownOptionComponent)
public readonly options: QueryList<InputDropdownOptionComponent> =
new QueryList<InputDropdownOptionComponent>();
@ContentChildren(InputDropdownOptionGroupComponent)
public readonly optionGroups: QueryList<InputDropdownOptionGroupComponent> =
new QueryList<InputDropdownOptionGroupComponent>();
@ViewChild(MatSelect) public readonly matSelect?: MatSelect;
protected get selectedOptions(): readonly InputDropdownOptionComponent[] {
if (!this.matSelect) {
return [];
}
const allOptions = [
...this.options.toArray(),
...this.optionGroups
.toArray()
.flatMap((group) => group.options.toArray()),
];
// Single selection
if (this.matSelect.selected instanceof MatOption) {
const selectedValue = this.matSelect.selected.value;
return allOptions.filter((option) => option.value === selectedValue);
}
// Multi-selection
if (
Array.isArray(this.matSelect.selected) &&
this.matSelect.selected.every((s) => s instanceof MatOption)
) {
const selectedValues = this.matSelect.selected.map(
(selected) => (selected as MatOption).value,
);
return allOptions.filter((option) =>
selectedValues.includes(option.value),
);
}
// Default: No option is selected.
return [];
}
@Input() public compareWith: MatSelect['compareWith'] = (
optionValue,
selectedValue,
) => {
return (
JSON.stringify(optionValue) === JSON.stringify(selectedValue) ||
optionValue === selectedValue
);
};
}