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

atinc / ngx-tethys / 0bbb2cec-209e-4d8a-b1b3-6bc54e05daa6

04 Sep 2023 08:40AM UTC coverage: 15.616% (-74.6%) from 90.2%
0bbb2cec-209e-4d8a-b1b3-6bc54e05daa6

Pull #2829

circleci

cmm-va
fix: add test
Pull Request #2829: fix: add tabIndex

300 of 6386 branches covered (0.0%)

Branch coverage included in aggregate %.

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

2849 of 13779 relevant lines covered (20.68%)

83.41 hits per line

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

6.45
/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';
×
17
import { TabIndexDisabledControlValueAccessorMixin } from 'ngx-tethys/core';
×
18

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

×
46
    public type?: String = 'primary';
×
47

×
48
    public size?: String = '';
×
49

×
50
    public disabled?: boolean = false;
×
51

×
52
    public classNames: string[];
53

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

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

58
    private initialized = false;
×
59

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

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

×
76
    /**
×
77
     * 大小
78
     * @type xs | sm | md
79
     * @default md
×
80
     */
×
81
    @Input()
×
82
    set thySize(value: string) {
83
        if (!this.sizeArray.includes(value)) {
×
84
            value = '';
×
85
        }
×
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>();
×
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