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

atinc / ngx-tethys / #55

30 Jul 2025 07:08AM UTC coverage: 9.866% (-80.4%) from 90.297%
#55

push

why520crazy
feat(empty): add setMessage for update display text #TINFR-2616

92 of 6794 branches covered (1.35%)

Branch coverage included in aggregate %.

2014 of 14552 relevant lines covered (13.84%)

6.15 hits per line

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

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

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

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

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

44
    inputValue = '';
×
45

×
46
    isComposing = false;
×
47

×
48
    panelOpened = false;
×
49

50
    isMultiple = false;
51

×
52
    showSearch = false;
53

54
    disabled = false;
×
55

56
    size: SelectControlSize;
57

×
58
    selectedOptions: SelectOptionBase | SelectOptionBase[];
×
59

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

×
62
    tagSize: ThyTagSize;
63

64
    private hostRenderer = useHostRenderer();
×
65

×
66
    @Input({ transform: coerceBooleanProperty })
67
    get thyPanelOpened(): boolean {
68
        return this.panelOpened;
×
69
    }
70

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

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

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

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

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

×
108
    @Input()
109
    get thySelectedOptions(): SelectOptionBase | SelectOptionBase[] {
110
        return this.selectedOptions;
×
111
    }
×
112

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

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

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

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

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

×
158
    @Input()
×
159
    thyPlaceholder = '';
160

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

166
    set thySize(value: SelectControlSize) {
×
167
        this.size = value;
168
        this.setSelectControlClass();
169

170
        if (value === 'xs' || value === 'sm') {
×
171
            this.tagSize = 'sm';
172
        } else if (value === 'lg') {
173
            this.tagSize = 'lg';
×
174
        } else {
×
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

×
185
    @Output()
×
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()
×
195
    public thyOnBlur = new EventEmitter<Event>();
×
196

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

×
228
    get selectedValue(): any {
229
        return this.thySelectedOptions;
230
    }
×
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

© 2025 Coveralls, Inc