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

atinc / ngx-tethys / 7dc107ed-5912-411d-8c10-4aca555a11bc

14 May 2025 05:43AM UTC coverage: 90.269% (-0.008%) from 90.277%
7dc107ed-5912-411d-8c10-4aca555a11bc

push

circleci

web-flow
fix(shared): update input value handling and search emission logic #TINFR-2053 (#3408)

5615 of 6881 branches covered (81.6%)

Branch coverage included in aggregate %.

3 of 3 new or added lines in 1 file covered. (100.0%)

2 existing lines in 2 files now uncovered.

13383 of 14165 relevant lines covered (94.48%)

921.5 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
287✔
17
} from '@angular/core';
287✔
18
import { useHostRenderer } from '@tethys/cdk/dom';
287✔
19

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

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

287✔
29
/**
287✔
30
 * @private
287✔
31
 */
287✔
32
@Component({
287✔
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'
465✔
39
    }
465✔
40
})
19✔
41
export class ThySelectControl implements OnInit {
19✔
42
    private renderer = inject(Renderer2);
43

44
    inputValue = '';
465✔
45

7✔
46
    isComposing = false;
7✔
47

7✔
48
    panelOpened = false;
7✔
49

50
    isMultiple = false;
51

465✔
52
    showSearch = false;
53

UNCOV
54
    disabled = false;
×
55

56
    size: SelectControlSize;
57

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

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

2,449✔
62
    tagSize: ThyTagSize;
63

64
    private hostRenderer = useHostRenderer();
302✔
65

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

71
    set thyPanelOpened(value: boolean) {
425✔
72
        this.panelOpened = value;
425✔
73
        if (this.panelOpened && this.thyShowSearch) {
425✔
74
            Promise.resolve(null).then(() => {
75✔
75
                this.inputElement.nativeElement.focus();
8✔
76
            });
77
        }
78
        if (!this.panelOpened && this.thyShowSearch) {
79
            new Promise(resolve => setTimeout(resolve, 100)).then(() => {
350✔
80
                this.inputValue = '';
6✔
81
                this.updateWidth();
82
                this.thyOnSearch.emit(this.inputValue);
83
            });
425✔
84
        }
425✔
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;
675✔
101
    }
102

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

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

291✔
113
    set thySelectedOptions(value: SelectOptionBase | SelectOptionBase[]) {
4✔
114
        let sameValue = false;
115
        const oldValue = this.selectedOptions;
287✔
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
            }
286✔
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);
539✔
139
        }
539✔
140
    }
1✔
141

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

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

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

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

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

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

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

170
        if (value === 'xs' || value === 'sm') {
286✔
171
            this.tagSize = 'sm';
172
        } else if (value === 'lg') {
173
            this.tagSize = 'lg';
1,939✔
174
        } else {
1,939✔
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,939✔
185
    @Output()
1,939✔
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

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

19✔
197
    @ViewChild('inputElement')
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
        }
48✔
211
        return { display: showSelectedValue ? 'flex' : 'none' };
2!
212
    }
2✔
213

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;
6✔
221
        }
222
        if (this.isComposing || this.inputValue) {
223
            placeholder = false;
6✔
224
        }
225
        return { display: placeholder ? 'block' : 'none' };
226
    }
216✔
227

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

1✔
232
    get multipleSelectedValue(): any {
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
        );
1✔
252
    }
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
        };
277
    }
278

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

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

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

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

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

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

320
    onBlur(event: Event) {
321
        this.thyOnBlur.emit(event);
322
    }
323
}
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