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

atinc / ngx-tethys / cfddb9aa-7590-4a96-a414-ff9fa1bfc1f2

13 Oct 2023 03:40AM UTC coverage: 90.218%. Remained the same
cfddb9aa-7590-4a96-a414-ff9fa1bfc1f2

push

circleci

web-flow
feat(select): #INFR-5471 custom-select support display avatar or icon (#2855)

* feat(select): #INFR-5471 custom-select support display avatar or icon

5166 of 6385 branches covered (0.0%)

Branch coverage included in aggregate %.

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

13040 of 13795 relevant lines covered (94.53%)

972.77 hits per line

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

90.67
/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';
407✔
21
import { ThyIconComponent } from 'ngx-tethys/icon';
407✔
22
import { ThyTagComponent } from 'ngx-tethys/tag';
15✔
23
import { SelectOptionBase } from '../../option/select-option-base';
15✔
24
import { ThyGridModule } from 'ngx-tethys/grid';
25

26
export type SelectControlSize = 'sm' | 'md' | 'lg' | '';
407✔
27

2✔
28
/**
2✔
29
 * @private
30
 */
31
@Component({
407✔
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: {
267✔
38
        '[class.select-control-borderless]': 'thyBorderless'
267✔
39
    }
40
})
41
export class ThySelectControlComponent implements OnInit {
2,159✔
42
    inputValue = '';
43

44
    isComposing = false;
269✔
45

269✔
46
    panelOpened = false;
47

48
    isMultiple = false;
2,026✔
49

50
    showSearch = false;
51

365✔
52
    disabled = false;
365✔
53

365✔
54
    size: SelectControlSize;
52✔
55

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

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

313✔
60
    tagSize: ThyTagSize;
7✔
61

62
    private hostRenderer = useHostRenderer();
63

365✔
64
    @Input()
365✔
65
    @InputBoolean()
4✔
66
    get thyPanelOpened(): boolean {
3✔
67
        return this.panelOpened;
3✔
68
    }
69

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

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

261✔
91
    set thyIsMultiple(value: boolean) {
261✔
92
        this.isMultiple = value;
3✔
93
        this.setSelectControlClass();
94
    }
258✔
95

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

102
    set thyShowSearch(value: boolean) {
57✔
103
        this.showSearch = value;
57✔
104
        this.setSelectControlClass();
5✔
105
    }
3✔
106

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

112
    set thySelectedOptions(value: SelectOptionBase | SelectOptionBase[]) {
52✔
113
        let sameValue = false;
114
        const oldValue = this.selectedOptions;
57✔
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);
473✔
118
            }
473✔
119
        } else {
1✔
120
            if (oldValue && value) {
121
                sameValue = (oldValue as SelectOptionBase).thyValue === (value as SelectOptionBase).thyValue;
473✔
122
            }
224✔
123
        }
124
        this.selectedOptions = value;
473✔
125
        if (this.panelOpened && this.thyShowSearch) {
17✔
126
            if (!sameValue) {
127
                Promise.resolve(null).then(() => {
473✔
128
                    this.setInputValue('');
129
                });
130
            }
168✔
131
            //等待组件渲染好再聚焦
132
            setTimeout(() => {
133
                if (this.panelOpened) {
2✔
134
                    this.inputElement.nativeElement.focus();
135
                }
136
            }, 200);
143✔
137
        }
1✔
138
    }
139

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

1,711✔
146
    set thyDisabled(value: boolean) {
147
        this.disabled = value;
148
        this.setSelectControlClass();
149
    }
257✔
150

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

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

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

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

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

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

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

181
    @Input() @InputBoolean() thyBorderless = false;
1,725✔
182

1,725✔
183
    @Output()
184
    thyOnSearch = new EventEmitter<string>();
185

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

189
    @Output()
190
    public thyOnClear = new EventEmitter<Event>();
21✔
191

18✔
192
    @Output()
18✔
193
    public thyOnBlur = new EventEmitter<Event>();
18✔
194

195
    @ViewChild('inputElement')
196
    inputElement: ElementRef;
197

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

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

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

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

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

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

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

252
    constructor(private renderer: Renderer2) {}
253

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

258
    setSelectControlClass() {
259
        const modeType = this.isMultiple ? 'multiple' : 'single';
260
        const selectControlClass = {
1✔
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,
1✔
266
            [`select-control-show-search`]: this.showSearch,
267
            [`panel-is-opened`]: this.panelOpened,
268
            [`disabled`]: this.disabled
269
        };
270
        this.hostRenderer.updateClassByMap(selectControlClass);
1✔
271
        this.searchInputControlClass = {
272
            [`form-control`]: true,
273
            [`form-control-${this.thySize}`]: !!this.thySize,
274
            [`search-input-field`]: true,
1✔
275
            [`hidden`]: !this.thyShowSearch
276
        };
277
    }
278

1✔
279
    setInputValue(value: string) {
280
        if (value !== this.inputValue) {
281
            this.inputValue = value;
282
            this.updateWidth();
1✔
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 === 0 && 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

© 2025 Coveralls, Inc