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

atinc / ngx-tethys / e62d3b10-1466-49c3-aabd-707148681fc8

14 Jun 2024 08:24AM UTC coverage: 90.422%. Remained the same
e62d3b10-1466-49c3-aabd-707148681fc8

push

circleci

minlovehua
feat: use the ngx-tethys/util's coerceBooleanProperty instead of booleanAttribute #INFR-12648

5467 of 6692 branches covered (81.69%)

Branch coverage included in aggregate %.

117 of 120 new or added lines in 66 files covered. (97.5%)

183 existing lines in 46 files now uncovered.

13216 of 13970 relevant lines covered (94.6%)

985.91 hits per line

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

90.36
/src/date-picker/base-picker.component.ts
1
import { ThyClickDispatcher, ThyPlacement } from 'ngx-tethys/core';
2
import { elementMatchClosest, FunctionProp, TinyDate } from 'ngx-tethys/util';
3

4
import {
5
    ChangeDetectorRef,
6
    Component,
7
    ElementRef,
8
    EventEmitter,
9
    Input,
10
    NgZone,
11
    OnChanges,
12
    OnInit,
13
    Output,
14
    PLATFORM_ID,
1✔
15
    TemplateRef,
16
    ViewChild,
153✔
17
    inject
153✔
18
} from '@angular/core';
6✔
19

20
import { AbstractPickerComponent } from './abstract-picker.component';
21
import { CompatibleValue, RangeAdvancedValue } from './inner-types';
22
import { CompatibleDate, ThyPanelMode } from './standard-types';
844✔
23
import { ThyPicker } from './picker.component';
24
import { hasTimeInStringDate, isValidStringDate, parseStringDate, transformDateValue } from './picker.util';
25
import { isPlatformBrowser } from '@angular/common';
559✔
26
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
27
import { coerceBooleanProperty } from '@angular/cdk/coercion';
28

67!
29
/**
30
 * @private
31
 */
153✔
32
@Component({
153✔
33
    template: ``,
153✔
34
    standalone: true,
153✔
35
    host: {
153✔
36
        '[attr.tabindex]': `tabIndex`,
153✔
37
        '(focus)': 'onFocus($event)',
153✔
38
        '(blur)': 'onBlur($event)'
153✔
39
    }
153✔
40
})
153✔
41
export class BasePicker extends AbstractPickerComponent implements OnInit, OnChanges {
153✔
42
    showWeek = false;
153✔
43

153✔
44
    panelMode: ThyPanelMode | ThyPanelMode[];
45

46
    initialized: boolean;
153✔
47

153✔
48
    private innerPreviousDate: string;
153✔
49

153!
50
    @ViewChild('thyPicker', { static: true }) thyPicker: ThyPicker;
153✔
51

52
    @Input() thyDateRender: FunctionProp<TemplateRef<Date> | string>;
53

54
    @Input() set thyMode(value: ThyPanelMode) {
224✔
55
        this._panelMode = value ?? 'date';
56
        if (this.initialized) {
57
            this.setDefaultTimePickerState(this._panelMode);
1✔
58
        }
1✔
59
    }
1✔
60

61
    get thyMode() {
62
        return this._panelMode;
63
    }
64

65
    /**
66
     * 是否有幕布
47✔
67
     * @default true
47✔
68
     */
47✔
69
    @Input({ transform: coerceBooleanProperty }) thyHasBackdrop = true;
47✔
70

39✔
71
    /**
72
     * @type EventEmitter<ThyPanelMode | ThyPanelMode[]>
47✔
73
     */
74
    @Output() readonly thyOnPanelChange = new EventEmitter<ThyPanelMode | ThyPanelMode[]>();
75

14✔
76
    /**
6✔
77
     * @type EventEmitter<Date[]>
6✔
78
     */
6✔
79
    @Output() readonly thyOnCalendarChange = new EventEmitter<Date[]>();
6✔
80

81
    private _showTime: object | boolean;
8✔
82

8✔
83
    /**
8!
84
     * 增加时间选择功能
8✔
85
     * @default false
5✔
86
     */
87
    @Input() get thyShowTime(): object | boolean {
88
        return this._showTime;
3✔
89
    }
90
    set thyShowTime(value: object | boolean) {
8✔
91
        this._showTime = typeof value === 'object' ? value : coerceBooleanProperty(value);
8✔
92
    }
8✔
93

94
    /**
95
     * 是否展示时间(时、分)
96
     * @default false
159✔
97
     */
159✔
98
    @Input({ transform: coerceBooleanProperty }) thyMustShowTime = false;
58✔
99

100
    /**
101
     * 弹出位置
101✔
102
     * @type top | topLeft | topRight | bottom | bottomLeft | bottomRight | left | leftTop | leftBottom | right | rightTop | rightBottom
103
     */
159✔
104
    @Input() thyPlacement: ThyPlacement = 'bottomLeft';
159✔
105

121✔
106
    /**
107
     * @type EventEmitter<CompatibleDate | null>
108
     */
109
    @Output() readonly thyOnOk = new EventEmitter<CompatibleDate | null>();
110

121✔
111
    takeUntilDestroyed = takeUntilDestroyed();
112

121✔
113
    thyClickDispatcher = inject(ThyClickDispatcher);
114

115
    platformId = inject(PLATFORM_ID);
116

117
    ngZone = inject(NgZone);
61✔
118

10✔
119
    constructor(cdr: ChangeDetectorRef, protected element: ElementRef) {
120
        super(cdr);
121
    }
122

123
    ngOnInit(): void {
10!
124
        super.ngOnInit();
15✔
125
        this.setDefaultTimePickerState(this._panelMode);
10✔
126
        this.initialized = true;
127

128
        if (isPlatformBrowser(this.platformId)) {
129
            this.thyClickDispatcher
66✔
130
                .clicked(0)
131
                .pipe(this.takeUntilDestroyed)
132
                .subscribe((event: Event) => {
5!
133
                    if (
×
134
                        !this.element.nativeElement.contains(event.target) &&
×
UNCOV
135
                        !this.thyPicker?.overlayContainer?.nativeElement.contains(event.target as Node) &&
×
136
                        this.realOpenState
137
                    ) {
UNCOV
138
                        this.ngZone.run(() => {
×
139
                            this.closeOverlay();
140
                            this.cdr.markForCheck();
141
                        });
142
                    }
5!
143
                });
5✔
144
        }
145
    }
UNCOV
146

×
147
    onValueChange(value: CompatibleValue | RangeAdvancedValue): void {
148
        this.thyPicker.entering = false;
149
        this.restoreTimePickerState(value as CompatibleValue);
5✔
150
        super.onValueChange(value);
151
        if (!this.flexible) {
152
            this.closeOverlay();
177✔
153
        }
177✔
154
        this.innerPreviousDate = this.thyPicker.getReadableValue(this.thyValue);
55✔
155
    }
156

157
    onInputValueChange(formatDate: string | null | Array<null>) {
158
        if (!formatDate || !formatDate.length) {
1✔
159
            const compatibleValue = formatDate ? (formatDate as CompatibleValue) : null;
160
            this.restoreTimePickerState(compatibleValue);
161
            super.onValueChange(compatibleValue);
162
            return;
5✔
163
        }
1✔
164
        let value = formatDate as string;
165
        const valueValid = isValidStringDate(value);
4✔
166
        const valueLimitValid = valueValid ? this.isValidDateLimit(parseStringDate(value)) : false;
167
        if (valueValid && valueLimitValid) {
168
            this.innerPreviousDate = value;
13!
169
        } else {
13✔
170
            value = this.innerPreviousDate;
4✔
171
        }
172
        const tinyDate = value ? (this.thyShowTime ? parseStringDate(value) : parseStringDate(value).startOfDay()) : null;
13✔
173
        this.restoreTimePickerState(tinyDate);
174
        super.onValueChange(tinyDate);
175
    }
176

8✔
177
    // Displays the time directly when the time must be displayed by default
8!
UNCOV
178
    setDefaultTimePickerState(value: ThyPanelMode) {
×
179
        this.withTime = this.thyMustShowTime;
180
        if (this.isRange) {
8✔
181
            this.panelMode = this.flexible ? ['date', 'date'] : [value, value];
8✔
182
        } else {
8✔
183
            this.panelMode = value;
184
        }
185
        this.showWeek = value === 'week';
186
        if (!this.thyFormat) {
1✔
187
            const inputFormats: { [key in ThyPanelMode]?: string } = {
188
                year: 'yyyy',
189
                quarter: 'yyyy-qqq',
190
                month: 'yyyy-MM',
1✔
191
                week: 'yyyy-ww周',
192
                date: this.thyShowTime ? 'yyyy-MM-dd HH:mm' : 'yyyy-MM-dd'
193
            };
194
            this.thyFormat = this.flexible ? inputFormats['date'] : inputFormats[value];
195
        }
196
    }
197

198
    // Restore after clearing time to select whether the original picker time is displayed or not
199
    restoreTimePickerState(value: CompatibleValue | null) {
200
        if (!value) {
201
            this.withTime = this.thyMustShowTime || this.originWithTime;
202
        }
203
    }
1✔
204

205
    // Emit thyOnCalendarChange when select date by thy-range-picker
206
    onCalendarChange(value: TinyDate[]): void {
207
        if (this.isRange) {
208
            const rangeValue = value.map(x => x.nativeDate);
209
            this.thyOnCalendarChange.emit(rangeValue);
210
        }
211
    }
212

213
    onShowTimePickerChange(show: boolean): void {
214
        this.withTime = show;
215
    }
216

217
    onResultOk(): void {
218
        if (this.isRange) {
219
            const value = this.thyValue as TinyDate[];
220
            if (value.length) {
221
                this.thyOnOk.emit([value[0].nativeDate, value[1].nativeDate]);
222
            } else {
223
                this.thyOnOk.emit([]);
224
            }
225
        } else {
226
            if (this.thyValue) {
227
                this.thyOnOk.emit((this.thyValue as TinyDate).nativeDate);
228
            } else {
229
                this.thyOnOk.emit(null);
230
            }
231
        }
232
        this.closeOverlay();
233
    }
234

235
    onOpenChange(open: boolean): void {
236
        this.thyOpenChange.emit(open);
237
        if (!open) {
238
            this.onTouchedFn();
239
        }
240
    }
241

242
    onFocus(event: Event) {
243
        this.picker.focus();
244
    }
245

246
    onBlur(event?: FocusEvent) {
247
        // Tab 聚焦后自动聚焦到 input 输入框,此分支下直接返回,无需触发 onTouchedFn
248
        if (elementMatchClosest(event?.relatedTarget as HTMLElement, ['date-popup', 'thy-picker'])) {
249
            return;
250
        }
251
        this.onTouchedFn();
252
    }
253

254
    onInputDate(value: string) {
255
        if (value && isValidStringDate(value)) {
256
            if (this.thyShowTime) {
257
                this.withTime = hasTimeInStringDate(value);
258
            }
259
            this.thyValue = parseStringDate(value);
260
        }
261
    }
262

263
    private isValidDateLimit(date: TinyDate): boolean {
264
        let disable = false;
265
        if (this.thyDisabledDate !== undefined) {
266
            disable = this.thyDisabledDate(date.nativeDate);
267
        }
268
        const minDate = this.thyMinDate ? new TinyDate(transformDateValue(this.thyMinDate).value as Date) : null;
269
        const maxDate = this.thyMaxDate ? new TinyDate(transformDateValue(this.thyMaxDate).value as Date) : null;
270
        return (
271
            (!minDate || date.startOfDay().nativeDate >= minDate.startOfDay().nativeDate) &&
272
            (!maxDate || date.startOfDay().nativeDate <= maxDate.startOfDay().nativeDate) &&
273
            !disable
274
        );
275
    }
276
}
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

© 2026 Coveralls, Inc