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

atinc / ngx-tethys / 8a6ba229-c82f-4a21-a1ed-95461f2ad66c

04 Sep 2023 08:37AM UTC coverage: 90.196% (-0.004%) from 90.2%
8a6ba229-c82f-4a21-a1ed-95461f2ad66c

Pull #2829

circleci

cmm-va
fix: delete f
Pull Request #2829: fix: add tabIndex

5164 of 6386 branches covered (0.0%)

Branch coverage included in aggregate %.

78 of 78 new or added lines in 26 files covered. (100.0%)

13024 of 13779 relevant lines covered (94.52%)

971.69 hits per line

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

93.55
/src/switch/switch.component.ts
1
import { coerceBooleanProperty } from 'ngx-tethys/util';
2

3
import { NgClass } from '@angular/common';
4
import {
5
    ChangeDetectionStrategy,
6
    ChangeDetectorRef,
7
    Component,
8
    ElementRef,
9
    EventEmitter,
10
    forwardRef,
11
    Input,
12
    OnInit,
13
    Output,
14
    ViewChild
1✔
15
} from '@angular/core';
16
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
6✔
17
import { TabIndexDisabledControlValueAccessorMixin } from 'ngx-tethys/core';
2✔
18

19
/**
6✔
20
 * 开关组件
6✔
21
 * @name thy-switch
3✔
22
 * @order 10
23
 */
24
@Component({
25
    selector: 'thy-switch',
4!
26
    templateUrl: './switch.component.html',
×
27
    changeDetection: ChangeDetectionStrategy.OnPush,
28
    providers: [
4✔
29
        {
4✔
30
            provide: NG_VALUE_ACCESSOR,
1✔
31
            useExisting: forwardRef(() => ThySwitchComponent),
32
            multi: true
33
        }
34
    ],
373✔
35
    standalone: true,
36
    imports: [NgClass],
37
    host: {
5✔
38
        class: 'thy-switch',
5✔
39
        '[class.thy-switch-xs]': 'size === "xs"',
40
        '[class.thy-switch-sm]': 'size === "sm"'
41
    }
318✔
42
})
318✔
43
export class ThySwitchComponent extends TabIndexDisabledControlValueAccessorMixin implements OnInit, ControlValueAccessor {
318✔
44
    public model: boolean;
318✔
45

318✔
46
    public type?: String = 'primary';
318✔
47

318✔
48
    public size?: String = '';
318✔
49

318✔
50
    public disabled?: boolean = false;
318✔
51

318✔
52
    public classNames: string[];
53

54
    public typeArray: string[] = ['primary', 'info', 'warning', 'danger'];
316✔
55

316✔
56
    public sizeArray: string[] = ['', 'sm', 'xs'];
57

58
    private initialized = false;
626✔
59

626✔
60
    @ViewChild('switch', { static: true }) switchElementRef: ElementRef;
61

62
    /**
63
     * 类型,目前分为: 'primary' |'info' | 'warning' | 'danger'
313✔
64
     */
65
    @Input()
66
    set thyType(value: string) {
313✔
67
        if (!this.typeArray.includes(value)) {
68
            value = 'primary';
69
        }
313✔
70
        this.type = value;
313✔
71
        if (this.initialized) {
72
            this.setClassNames();
73
        }
1✔
74
    }
1✔
75

1✔
76
    /**
1✔
77
     * 大小
78
     * @type xs | sm | md
79
     * @default md
638✔
80
     */
638✔
81
    @Input()
3✔
82
    set thySize(value: string) {
83
        if (!this.sizeArray.includes(value)) {
638✔
84
            value = '';
1✔
85
        }
1!
86
        this.size = value;
×
87
        if (this.initialized) {
88
            this.setClassNames();
89
        }
90
    }
1✔
91

92
    /**
93
     * 是否属于禁用状态
1✔
94
     */
95
    @Input()
96
    override get thyDisabled(): boolean {
97
        return this.disabled;
98
    }
99

100
    override set thyDisabled(value: boolean) {
101
        this.disabled = coerceBooleanProperty(value);
1✔
102
        this.setClassNames();
103
    }
104

105
    /**
106
     * 数据变化的回调事件,即将被弃用,请使用 ngModelChange
107
     * @deprecated
108
     */
109
    @Output() thyChange: EventEmitter<Event> = new EventEmitter<Event>();
313✔
110

111
    constructor(public cdr: ChangeDetectorRef) {
112
        super();
113
    }
114

115
    ngOnInit() {
116
        this.setClassNames();
117
        this.initialized = true;
118
    }
119

120
    public onModelChange: Function = () => {};
121

122
    public onModelTouched: Function = () => {};
123

124
    writeValue(value: boolean) {
125
        this.model = value;
126
        this.cdr.markForCheck();
127
        // this.setClassNames();
128
    }
129

130
    registerOnChange(fn: Function): void {
131
        this.onModelChange = fn;
132
    }
133

134
    registerOnTouched(fn: Function): void {
135
        this.onModelTouched = fn;
136
    }
137

138
    setDisabledState(isDisabled: boolean) {
139
        this.disabled = isDisabled;
140
        this.setClassNames();
141
    }
142

143
    toggle(event: Event) {
144
        this.model = !this.model;
145
        this.onModelChange(this.model);
146
        this.onModelTouched();
147
        this.thyChange.emit(event);
148
    }
149

150
    setClassNames() {
151
        this.classNames = [`thy-switch-${this.type}`];
152
        if (this.size) {
153
            this.classNames.push(`thy-switch-${this.size}`);
154
        }
155
        if (this.disabled) {
156
            this.classNames.push(`thy-switch-disabled`);
157
            if (this.model) {
158
                this.classNames.push(`thy-switch-disabled-true`);
159
            }
160
        }
161
    }
162
}
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