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

atinc / ngx-tethys / 1b395ef9-9b5d-4553-9091-e0714143e3a7

13 Dec 2023 02:40AM UTC coverage: 17.395% (-73.0%) from 90.364%
1b395ef9-9b5d-4553-9091-e0714143e3a7

push

circleci

luxiaobei
fix(select): remove focus() when close panel #INFR-10914

502 of 6578 branches covered (0.0%)

Branch coverage included in aggregate %.

1 of 2 new or added lines in 1 file covered. (50.0%)

10248 existing lines in 329 files now uncovered.

3092 of 14083 relevant lines covered (21.96%)

92.82 hits per line

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

71.13
/src/shared/select/select-control/select-control.component.ts
1
import { InputBoolean, InputNumber } from 'ngx-tethys/core';
2
import { ThyTagSize } from 'ngx-tethys/tag';
3
import { isUndefinedOrNull } from 'ngx-tethys/util';
4

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

19
import { NgClass, NgFor, NgIf, NgStyle, NgTemplateOutlet } from '@angular/common';
20
import { FormsModule } from '@angular/forms';
201✔
21
import { ThyIconComponent } from 'ngx-tethys/icon';
201✔
22
import { ThyTagComponent } from 'ngx-tethys/tag';
7✔
23
import { SelectOptionBase } from '../../option/select-option-base';
7✔
24
import { ThyGridModule } from 'ngx-tethys/grid';
25

26
export type SelectControlSize = 'sm' | 'md' | 'lg' | '';
201!
UNCOV
27

×
UNCOV
28
/**
×
29
 * @private
30
 */
31
@Component({
201✔
32
    selector: 'thy-select-control,[thySelectControl]',
33
    templateUrl: './select-control.component.html',
34
    changeDetection: ChangeDetectionStrategy.OnPush,
×
35
    standalone: true,
36
    imports: [FormsModule, NgClass, NgIf, NgStyle, NgFor, ThyTagComponent, NgTemplateOutlet, ThyIconComponent, ThyGridModule],
37
    host: {
106✔
38
        '[class.select-control-borderless]': 'thyBorderless'
106✔
39
    }
40
})
41
export class ThySelectControlComponent implements OnInit {
926✔
42
    inputValue = '';
43

44
    isComposing = false;
103✔
45

103✔
46
    panelOpened = false;
47

48
    isMultiple = false;
945✔
49

50
    showSearch = false;
51

147✔
52
    disabled = false;
147✔
53

147✔
54
    size: SelectControlSize;
25✔
55

1✔
56
    selectedOptions: SelectOptionBase | SelectOptionBase[];
57

58
    searchInputControlClass: { [key: string]: boolean };
59

122✔
60
    tagSize: ThyTagSize;
3✔
61

62
    private hostRenderer = useHostRenderer();
63

147✔
64
    @Input()
147!
UNCOV
65
    @InputBoolean()
×
UNCOV
66
    get thyPanelOpened(): boolean {
×
UNCOV
67
        return this.panelOpened;
×
68
    }
69

70
    set thyPanelOpened(value: boolean) {
UNCOV
71
        this.panelOpened = value;
×
UNCOV
72
        if (this.panelOpened && this.thyShowSearch) {
×
UNCOV
73
            Promise.resolve(null).then(() => {
×
74
                this.inputElement.nativeElement.focus();
75
            });
76
        }
77
        if (!this.panelOpened && this.thyShowSearch) {
78
            Promise.resolve(null).then(() => {
79
                this.setInputValue('');
293✔
80
            });
81
        }
82
        this.setSelectControlClass();
104✔
83
    }
104✔
84

85
    @Input()
86
    @InputBoolean()
2,864✔
87
    get thyIsMultiple(): boolean {
88
        return this.isMultiple;
89
    }
103✔
90

103✔
91
    set thyIsMultiple(value: boolean) {
103✔
92
        this.isMultiple = value;
1✔
93
        this.setSelectControlClass();
94
    }
102✔
95

1✔
96
    @Input()
97
    @InputBoolean()
98
    get thyShowSearch(): boolean {
101✔
99
        return this.showSearch;
100
    }
101

102
    set thyShowSearch(value: boolean) {
22✔
103
        this.showSearch = value;
22!
UNCOV
104
        this.setSelectControlClass();
×
UNCOV
105
    }
×
106

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

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

56✔
140
    @Input()
141
    @InputBoolean()
142
    get thyDisabled(): boolean {
293✔
143
        return this.disabled;
144
    }
145

804✔
146
    set thyDisabled(value: boolean) {
147
        this.disabled = value;
148
        this.setSelectControlClass();
149
    }
100✔
150

100✔
151
    @Input()
100✔
152
    customDisplayTemplate: TemplateRef<any>;
100✔
153

100✔
154
    @Input()
100✔
155
    @InputBoolean()
100✔
156
    thyAllowClear = false;
100✔
157

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

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

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

170
        if (value === 'sm') {
171
            this.tagSize = 'sm';
716✔
172
        } else if (value === 'lg') {
716✔
173
            this.tagSize = 'lg';
174
        } else {
175
            this.tagSize = 'md';
176
        }
177
    }
178

179
    @Input() @InputNumber() thyMaxTagCount = 0;
180

181
    @Input() @InputBoolean() thyBorderless = false;
182

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

185
    @Output()
186
    thyOnSearch = new EventEmitter<string>();
187

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

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

11✔
194
    @Output()
11✔
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 {
22!
UNCOV
209
            showSelectedValue = true;
×
UNCOV
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
        }
4✔
219
        if (!this.thyPlaceholder) {
220
            placeholder = false;
221
        }
3✔
222
        if (this.isComposing || this.inputValue) {
223
            placeholder = false;
224
        }
28✔
225
        return { display: placeholder ? 'block' : 'none' };
226
    }
UNCOV
227

×
228
    get selectedValue(): any {
229
        return this.thySelectedOptions;
1✔
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
    constructor(private renderer: Renderer2) {}
255

256
    ngOnInit() {
257
        this.setSelectControlClass();
1✔
258
    }
259

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

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

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

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

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

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

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

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