-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.component.ts
166 lines (129 loc) · 3.71 KB
/
select.component.ts
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import {
Component,
ElementRef,
Input,
TemplateRef,
ViewChild,
Output,
EventEmitter,
OnInit,
OnChanges,
SimpleChanges
} from '@angular/core';
/**
* The SelectComponent is a customizable select dropdown component that supports
* single or multiple selections, search, and custom templates for both the view
* and items.
*/
@Component({
selector: 'wselect',
templateUrl: './select.component.html',
styleUrls: ['./select.component.scss'],
standalone: false
})
export class SelectComponent implements OnInit, OnChanges {
/** Placeholder text for the select input. */
@Input() placeholder = '';
/** List of items to display in the dropdown. */
@Input() items: any = [];
_items: any = {};
/** Whether the select input is disabled. */
@Input() disabled = false;
/** Whether the select input is clearable. */
@Input() clearable = false;
/** Clears the selected values. */
clear(): void {
if (this.multiple) {
this._values = [];
this.modelChange.emit(this._values);
} else {
this._selected = '';
this.modelChange.emit('');
}
}
/** The name of the property to display in the dropdown items. */
@Input() name = 'name';
/** The property used as the value for each item. */
@Input() value = '_id';
/** Whether multiple items can be selected. */
@Input() multiple = false;
/** The label for the select input. */
@Input() label = '';
/** Whether the dropdown is searchable. */
@Input() searchable = false;
/** The property by which to search items. */
@Input() searchableBy = 'name';
/** Event emitted when the selected values change. */
@Output() modelChange = new EventEmitter();
_values: any = [];
_names: any = [];
_selected: string;
selectShow: any;
/** The selected value(s). */
@Input() select: any;
/** Custom template for the view (header) of the select input. */
@Input('view') t_view: TemplateRef<any>;
/** Custom template for each item in the dropdown. */
@Input('item') t_item: TemplateRef<any>;
/** Custom template for the search input. */
@Input('search') t_search: TemplateRef<any>;
search = '';
@ViewChild('e_search', { static: false }) e_search: ElementRef;
ngOnInit(): void {
for (let i = 0; i < this.items.length; i++) {
if (typeof this.items[i] === 'string') {
this.items[i] = {
name: this.items[i]
};
this.items[i][this.value] = this.items[i].name;
}
this._items[this.items[i][this.value]] = this.items[i];
}
if (this.multiple) {
this._values = this.select || [];
} else {
this._selected = this._items[this.select]
? this._items[this.select][this.name]
: this.select;
}
}
ngOnChanges(changes: SimpleChanges): void {
if (changes['select'] && !changes['select'].firstChange) {
this.ngOnInit();
}
}
/** Handles click events on items. */
item_onclick(item: any): void {
if (this.multiple) {
if (this._values.indexOf(item[this.value]) !== -1) {
this._values.splice(this._values.indexOf(item[this.value]), 1);
} else {
this._values.push(item[this.value]);
}
if (this._names.indexOf(item[this.name]) !== -1) {
this._names.splice(this._names.indexOf(item[this.name]), 1);
} else {
this._names.push(item[this.name]);
}
this._selected =
this._names.length == 0
? this.placeholder
: this._names.join(', ');
this.modelChange.emit(this._values);
} else {
this._selected = item[this.name];
this.selectShow = false;
this.modelChange.emit(item[this.value]);
}
}
/** Focuses the search input when the dropdown is opened. */
focus_search(): void {
this.search = '';
if (!this.searchable || this.t_search) return;
if (this.e_search) {
this.e_search.nativeElement.focus();
} else {
setTimeout(this.focus_search.bind(this), 100);
}
}
}