• 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

8.82
/src/switch/switch.component.ts
1
import { NgClass } from '@angular/common';
2
import {
3
    ChangeDetectionStrategy,
4
    ChangeDetectorRef,
5
    Component,
6
    ElementRef,
7
    forwardRef,
8
    inject,
9
    viewChild,
1✔
10
    input,
1✔
11
    computed,
12
    signal,
13
    output,
14
    effect
15
} from '@angular/core';
16
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
1✔
17
import { TabIndexDisabledControlValueAccessorMixin } from 'ngx-tethys/core';
18
import { coerceBooleanProperty } from 'ngx-tethys/util';
×
19

20
const supportedTypes: string[] = ['primary', 'info', 'warning', 'danger'];
21

×
22
const supportedSizes: string[] = ['', 'sm', 'xs'];
×
23

×
24
/**
×
25
 * 开关组件
×
26
 * @name thy-switch
×
27
 * @order 10
×
28
 */
×
29
@Component({
×
30
    selector: 'thy-switch',
×
31
    templateUrl: './switch.component.html',
×
32
    changeDetection: ChangeDetectionStrategy.OnPush,
33
    providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => ThySwitch), multi: true }],
34
    imports: [NgClass],
×
35
    host: { class: 'thy-switch', '[class.thy-switch-xs]': 'size() === "xs"', '[class.thy-switch-sm]': 'size() === "sm"' }
36
})
37
export class ThySwitch extends TabIndexDisabledControlValueAccessorMixin implements ControlValueAccessor {
×
38
    /**
×
39
     * 类型,目前分为: 'primary' |'info' | 'warning' | 'danger'
×
40
     */
41
    readonly thyType = input<string>('primary');
42

×
43
    /**
44
     * 大小
45
     * @type xs | sm | md
×
46
     * @default md
×
47
     */
×
48
    readonly thySize = input<string>('');
×
49

50
    /**
×
51
     * 是否属于禁用状态
×
52
     */
×
53
    readonly inputDisabled = input(false, { transform: coerceBooleanProperty, alias: `thyDisabled` });
×
54

55
    /**
56
     * 是否加载中
×
57
     */
58
    readonly thyLoading = input(false, { transform: coerceBooleanProperty });
×
59

×
60
    /**
×
61
     * 数据变化的回调事件,即将被弃用,请使用 ngModelChange
×
62
     * @deprecated
×
63
     */
×
64
    thyChange = output<Event>();
65

66
    model = signal<boolean>(false);
67

68
    disabled = signal(false);
69

70
    get thyDisabled() {
71
        return this.disabled() as boolean;
×
72
    }
×
73

×
74
    readonly type = computed(() => {
×
75
        if (!supportedTypes.includes(this.thyType())) {
×
76
            return 'primary';
×
77
        } else {
78
            return this.thyType();
79
        }
80
    });
×
81

×
82
    readonly size = computed(() => {
83
        if (!supportedSizes.includes(this.thySize())) {
84
            return '';
×
85
        } else {
86
            return this.thySize();
87
        }
×
88
    });
89

90
    readonly classNames = computed(() => {
×
91
        const classList = [`thy-switch-${this.type()}`];
92
        if (this.size()) {
93
            classList.push(`thy-switch-${this.size()}`);
×
94
        }
×
95
        if (this.disabled() || this.thyLoading()) {
×
96
            classList.push(`thy-switch-disabled`);
×
97
            if (this.model()) {
98
                classList.push(`thy-switch-disabled-true`);
1✔
99
            }
1✔
100
        }
101
        return classList;
102
    });
103

104
    readonly loadingCircle = computed(() => {
105
        const svgSize: Record<string, number> = { xs: 12, sm: 16 };
106
        const circleSize = svgSize[this.size()] ?? 20;
107
        const centerPoint = circleSize / 2;
108
        const r = circleSize / 4;
1✔
109
        return {
110
            viewBox: `0 0 ${circleSize} ${circleSize}`,
111
            cx: centerPoint,
112
            cy: centerPoint,
113
            r: r,
×
114
            dasharray: `${2 * Math.PI * r * 0.75} ${2 * Math.PI * r * 0.25}`
115
        };
116
    });
117

118
    onModelChange: Function = () => {};
119

120
    onModelTouched: Function = () => {};
121

122
    readonly switchElementRef = viewChild<string, ElementRef<HTMLElement>>('switch', { read: ElementRef<HTMLElement> });
123

124
    private cdr = inject(ChangeDetectorRef);
125

126
    constructor() {
127
        super();
128

129
        effect(() => {
130
            this.disabled.set(this.inputDisabled());
131
        });
132
    }
133

134
    writeValue(value: boolean) {
135
        this.model.set(value);
136
        this.cdr.markForCheck();
137
    }
138

139
    registerOnChange(fn: Function): void {
140
        this.onModelChange = fn;
141
    }
142

143
    registerOnTouched(fn: Function): void {
144
        this.onModelTouched = fn;
145
    }
146

147
    setDisabledState(isDisabled: boolean): void {
148
        this.disabled.set(isDisabled);
149
    }
150

151
    toggle(event: Event) {
152
        this.model.set(!this.model());
153
        this.onModelChange(this.model());
154
        this.onModelTouched();
155
        this.thyChange?.emit(event);
156
    }
157
}
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