• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

atinc / ngx-tethys / #41

14 Jul 2025 08:03AM UTC coverage: 90.295% (+0.002%) from 90.293%
#41

push

web-flow
ci: update environment to env

5521 of 6794 branches covered (81.26%)

Branch coverage included in aggregate %.

13747 of 14545 relevant lines covered (94.51%)

903.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.53
/src/shared/select/select-control/select-control.component.ts
1
import { ThyTagSize } from 'ngx-tethys/tag';
2
import { coerceBooleanProperty, isUndefinedOrNull } from 'ngx-tethys/util';
3

4
import {
5
    ChangeDetectionStrategy,
6
    Component,
7
    ElementRef,
8
    EventEmitter,
9
    Input,
10
    OnInit,
11
    Output,
12
    Renderer2,
13
    TemplateRef,
14
    ViewChild,
1✔
15
    numberAttribute,
16
    inject
288✔
17
} from '@angular/core';
288✔
18
import { useHostRenderer } from '@tethys/cdk/dom';
288✔
19

288✔
20
import { NgClass, NgStyle, NgTemplateOutlet } from '@angular/common';
288✔
21
import { FormsModule } from '@angular/forms';
288✔
22
import { ThyGridModule } from 'ngx-tethys/grid';
288✔
23
import { ThyIcon } from 'ngx-tethys/icon';
288✔
24
import { ThyTag } from 'ngx-tethys/tag';
288✔
25
import { SelectOptionBase } from '../../option/select-option-base';
288✔
26

288✔
27
export type SelectControlSize = 'xs' | 'sm' | 'md' | 'lg' | '';
288✔
28

288✔
29
/**
288✔
30
 * @private
288✔
31
 */
288✔
32
@Component({
288✔
33
    selector: 'thy-select-control,[thySelectControl]',
34
    templateUrl: './select-control.component.html',
35
    changeDetection: ChangeDetectionStrategy.OnPush,
×
36
    imports: [FormsModule, NgClass, NgStyle, ThyTag, NgTemplateOutlet, ThyIcon, ThyGridModule],
37
    host: {
38
        '[class.select-control-borderless]': 'thyBorderless'
466✔
39
    }
466✔
40
})
19✔
41
export class ThySelectControl implements OnInit {
19✔
42
    private renderer = inject(Renderer2);
43

44
    inputValue = '';
466✔
45

7✔
46
    isComposing = false;
7✔
47

7✔
48
    panelOpened = false;
7✔
49

50
    isMultiple = false;
51

466✔
52
    showSearch = false;
53

54
    disabled = false;
×
55

56
    size: SelectControlSize;
57

301✔
58
    selectedOptions: SelectOptionBase | SelectOptionBase[];
301✔
59

60
    searchInputControlClass: { [key: string]: boolean };
61

2,457✔
62
    tagSize: ThyTagSize;
63

64
    private hostRenderer = useHostRenderer();
303✔
65

303✔
66
    @Input({ transform: coerceBooleanProperty })
67
    get thyPanelOpened(): boolean {
68
        return this.panelOpened;
2,393✔
69
    }
70

71
    set thyPanelOpened(value: boolean) {
445✔
72
        this.panelOpened = value;
445✔
73
        if (this.panelOpened && this.thyShowSearch) {
445✔
74
            Promise.resolve(null).then(() => {
94✔
75
                this.inputElement.nativeElement.focus();
27✔
76
            });
77
        }
78
        if (!this.panelOpened && this.thyShowSearch) {
79
            new Promise(resolve => setTimeout(resolve, 100)).then(() => {
351✔
80
                this.inputValue = '';
6✔
81
                this.updateWidth();
82
                this.thyOnSearch.emit(this.inputValue);
83
            });
445✔
84
        }
445✔
85
        this.setSelectControlClass();
4✔
86
    }
3✔
87

3✔
88
    @Input({ transform: coerceBooleanProperty })
3✔
89
    get thyIsMultiple(): boolean {
90
        return this.isMultiple;
91
    }
92

4✔
93
    set thyIsMultiple(value: boolean) {
4!
94
        this.isMultiple = value;
4✔
95
        this.setSelectControlClass();
96
    }
97

98
    @Input({ transform: coerceBooleanProperty })
99
    get thyShowSearch(): boolean {
100
        return this.showSearch;
2,642✔
101
    }
102

103
    set thyShowSearch(value: boolean) {
297✔
104
        this.showSearch = value;
297✔
105
        this.setSelectControlClass();
106
    }
107

7,784✔
108
    @Input()
109
    get thySelectedOptions(): SelectOptionBase | SelectOptionBase[] {
110
        return this.selectedOptions;
292✔
111
    }
292✔
112

292✔
113
    set thySelectedOptions(value: SelectOptionBase | SelectOptionBase[]) {
4✔
114
        let sameValue = false;
115
        const oldValue = this.selectedOptions;
288✔
116
        if (this.isMultiple) {
1✔
117
            if (oldValue instanceof Array && value instanceof Array && oldValue.length === value.length) {
118
                sameValue = value.every((option, index) => option.thyValue === oldValue[index].thyValue);
119
            }
287✔
120
        } else {
121
            if (oldValue && value) {
122
                sameValue = (oldValue as SelectOptionBase).thyValue === (value as SelectOptionBase).thyValue;
123
            }
65✔
124
        }
65✔
125
        this.selectedOptions = value;
7✔
126
        if (this.panelOpened && this.thyShowSearch) {
4✔
127
            if (!sameValue) {
128
                Promise.resolve(null).then(() => {
129
                    this.inputValue = '';
3✔
130
                    this.updateWidth();
131
                });
132
            }
133
            //等待组件渲染好再聚焦
58✔
134
            setTimeout(() => {
135
                if (this.panelOpened) {
65✔
136
                    this.inputElement.nativeElement.focus();
137
                }
138
            }, 200);
560✔
139
        }
560✔
140
    }
1✔
141

142
    @Input({ transform: coerceBooleanProperty })
560✔
143
    get thyDisabled(): boolean {
126✔
144
        return this.disabled;
145
    }
560✔
146

19✔
147
    set thyDisabled(value: boolean) {
148
        this.disabled = value;
560✔
149
        this.setSelectControlClass();
150
    }
151

112✔
152
    @Input()
153
    customDisplayTemplate: TemplateRef<any>;
154

2✔
155
    @Input({ transform: coerceBooleanProperty })
156
    thyAllowClear = false;
157

210✔
158
    @Input()
1✔
159
    thyPlaceholder = '';
160

209✔
161
    @Input()
162
    get thySize(): SelectControlSize {
163
        return this.size;
696✔
164
    }
165

166
    set thySize(value: SelectControlSize) {
2,067✔
167
        this.size = value;
168
        this.setSelectControlClass();
169

170
        if (value === 'xs' || value === 'sm') {
287✔
171
            this.tagSize = 'sm';
172
        } else if (value === 'lg') {
173
            this.tagSize = 'lg';
1,946✔
174
        } else {
1,946✔
175
            this.tagSize = 'md';
176
        }
177
    }
178

179
    @Input({ transform: numberAttribute }) thyMaxTagCount = 0;
180

181
    @Input({ transform: coerceBooleanProperty }) thyBorderless = false;
182

183
    @Input() thyPreset: string = '';
184

1,946✔
185
    @Output()
1,946✔
186
    thyOnSearch = new EventEmitter<string>();
187

188
    @Output()
189
    public thyOnRemove = new EventEmitter<{ item: SelectOptionBase; $eventOrigin: Event }>();
190

191
    @Output()
192
    public thyOnClear = new EventEmitter<Event>();
193

194
    @Output()
19!
195
    public thyOnBlur = new EventEmitter<Event>();
19✔
196

19✔
197
    @ViewChild('inputElement')
19✔
198
    inputElement: ElementRef;
199

200
    get selectedValueStyle(): { [key: string]: string } {
201
        let showSelectedValue = false;
×
202
        if (this.showSearch) {
×
203
            if (this.panelOpened) {
204
                showSelectedValue = !(this.isComposing || this.inputValue);
×
205
            } else {
×
206
                showSelectedValue = true;
×
207
            }
208
        } else {
209
            showSelectedValue = true;
210
        }
211
        return { display: showSelectedValue ? 'flex' : 'none' };
48✔
212
    }
2!
213

2✔
214
    get placeholderStyle(): { [key: string]: string } {
215
        let placeholder = true;
216
        if (this.isSelectedValue) {
×
217
            placeholder = false;
218
        }
219
        if (!this.thyPlaceholder) {
220
            placeholder = false;
221
        }
6✔
222
        if (this.isComposing || this.inputValue) {
223
            placeholder = false;
224
        }
6✔
225
        return { display: placeholder ? 'block' : 'none' };
226
    }
227

216✔
228
    get selectedValue(): any {
229
        return this.thySelectedOptions;
230
    }
2✔
231

232
    get multipleSelectedValue(): any {
1✔
233
        return this.thySelectedOptions;
234
    }
235

236
    get maxSelectedTags() {
237
        if (this.thyMaxTagCount > 0 && this.thySelectedOptions instanceof Array && this.thySelectedOptions.length > this.thyMaxTagCount) {
238
            return this.thySelectedOptions.slice(0, this.thyMaxTagCount - 1);
239
        }
240
        return this.thySelectedOptions as SelectOptionBase[];
241
    }
242

243
    get showClearIcon(): boolean {
244
        return this.thyAllowClear && this.isSelectedValue;
245
    }
246

247
    get isSelectedValue(): boolean {
248
        return (
249
            (!this.isMultiple && !isUndefinedOrNull(this.thySelectedOptions)) ||
250
            (this.isMultiple && (<SelectOptionBase[]>this.thySelectedOptions).length > 0)
251
        );
252
    }
1✔
253

254
    ngOnInit() {
255
        this.setSelectControlClass();
256
    }
257

258
    setSelectControlClass() {
259
        const modeType = this.isMultiple ? 'multiple' : 'single';
260
        const selectControlClass = {
261
            [`form-control`]: true,
262
            [`form-control-${this.thySize}`]: !!this.thySize,
263
            [`form-control-custom`]: true,
264
            [`select-control`]: true,
265
            [`select-control-${modeType}`]: true,
266
            [`select-control-show-search`]: this.showSearch,
267
            [`panel-is-opened`]: this.panelOpened,
268
            [`disabled`]: this.disabled
269
        };
270
        this.hostRenderer.updateClassByMap(selectControlClass);
271
        this.searchInputControlClass = {
272
            [`form-control`]: true,
273
            [`form-control-${this.thySize}`]: !!this.thySize,
274
            [`search-input-field`]: true,
275
            [`hidden`]: !this.thyShowSearch,
276
            [`disabled`]: this.thyDisabled
277
        };
278
    }
279

280
    setInputValue(value: string) {
281
        if (value !== this.inputValue) {
282
            this.inputValue = value;
283
            this.updateWidth();
284
            this.thyOnSearch.emit(this.inputValue);
285
        }
286
    }
287

288
    handleBackspace(event: Event) {
289
        if ((event as KeyboardEvent).isComposing) {
290
            return;
291
        }
292
        if (!this.inputValue?.length && this.selectedOptions instanceof Array) {
293
            if (this.selectedOptions.length > 0) {
294
                this.removeHandle(this.selectedOptions[this.selectedOptions.length - 1], event);
295
            }
296
        }
297
    }
298

299
    updateWidth() {
300
        if (this.isMultiple && this.thyShowSearch) {
301
            if (this.inputValue || this.isComposing) {
302
                this.renderer.setStyle(this.inputElement.nativeElement, 'width', `${this.inputElement.nativeElement.scrollWidth}px`);
303
            } else {
304
                this.renderer.removeStyle(this.inputElement.nativeElement, 'width');
305
            }
306
        }
307
    }
308

309
    removeHandle(item: SelectOptionBase, $event: Event) {
310
        this.thyOnRemove.emit({ item: item, $eventOrigin: $event });
311
    }
312

313
    clearHandle($event: Event) {
314
        this.thyOnClear.emit($event);
315
    }
316

317
    trackValue(_index: number, option: SelectOptionBase): any {
318
        return option.thyValue;
319
    }
320

321
    onBlur(event: Event) {
322
        this.thyOnBlur.emit(event);
323
    }
324
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc