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

atinc / ngx-tethys / 82f575a8-2f12-4689-80e6-398f6f1685a3

15 Aug 2024 08:19AM UTC coverage: 90.473%. Remained the same
82f575a8-2f12-4689-80e6-398f6f1685a3

push

circleci

web-flow
build: bump prettier to 3.3.3 and other deps, refactor notify use @if (#3152)

5502 of 6726 branches covered (81.8%)

Branch coverage included in aggregate %.

112 of 116 new or added lines in 57 files covered. (96.55%)

59 existing lines in 15 files now uncovered.

13254 of 14005 relevant lines covered (94.64%)

997.41 hits per line

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

95.29
/src/date-picker/picker.component.ts
1
import { getFlexiblePositions, ThyPlacement } from 'ngx-tethys/core';
2
import { coerceBooleanProperty, TinyDate } from 'ngx-tethys/util';
3

4
import { CdkConnectedOverlay, CdkOverlayOrigin, ConnectedOverlayPositionChange } from '@angular/cdk/overlay';
5
import {
6
    AfterViewInit,
7
    ChangeDetectionStrategy,
8
    ChangeDetectorRef,
9
    Component,
10
    ElementRef,
11
    EventEmitter,
12
    Input,
13
    OnChanges,
14
    Output,
15
    SimpleChanges,
16
    ViewChild
17
} from '@angular/core';
1✔
18

19
import { AsyncPipe, NgClass, NgIf, NgTemplateOutlet } from '@angular/common';
×
20
import { ThyIcon } from 'ngx-tethys/icon';
21
import { ThyInputDirective } from 'ngx-tethys/input';
22
import { DateHelperService } from './date-helper.service';
159✔
23
import { CompatibleValue, RangePartType } from './inner-types';
159✔
24
import { getFlexibleAdvancedReadableValue } from './picker.util';
25
import { ThyDateGranularity } from './standard-types';
26
import { ThyEnterDirective } from 'ngx-tethys/shared';
×
27
import { scaleMotion, scaleXMotion, scaleYMotion } from 'ngx-tethys/core';
28

29
/**
163✔
30
 * @private
163✔
31
 */
32
@Component({
33
    selector: 'thy-picker',
642✔
34
    exportAs: 'thyPicker',
35
    templateUrl: './picker.component.html',
36
    changeDetection: ChangeDetectionStrategy.OnPush,
314✔
37
    standalone: true,
314✔
38
    imports: [
306✔
39
        CdkOverlayOrigin,
40
        ThyInputDirective,
41
        ThyEnterDirective,
42
        AsyncPipe,
43
        NgTemplateOutlet,
885✔
44
        NgIf,
45
        ThyIcon,
46
        NgClass,
651✔
47
        CdkConnectedOverlay
48
    ],
49
    animations: [scaleXMotion, scaleYMotion, scaleMotion]
157✔
50
})
157✔
51
export class ThyPicker implements OnChanges, AfterViewInit {
157✔
52
    @Input() isRange = false;
157✔
53
    @Input() open: boolean | undefined = undefined;
157✔
54
    @Input() disabled: boolean;
157✔
55
    @Input() placeholder: string | string[];
157✔
56
    @Input() readonly: boolean;
157✔
57
    @Input() allowClear: boolean;
157✔
58
    @Input() autoFocus: boolean;
157✔
59
    @Input() className: string;
157✔
60
    @Input() size: 'sm' | 'xs' | 'lg' | 'md' | 'default';
157✔
61
    @Input() suffixIcon: string;
157✔
62
    @Input() placement: ThyPlacement = 'bottomLeft';
157✔
63
    @Input() flexible: boolean = false;
157✔
64
    @Input() mode: string;
65
    @Input({ transform: coerceBooleanProperty }) hasBackdrop: boolean;
66
    @Output() blur = new EventEmitter<Event>();
67
    @Output() readonly valueChange = new EventEmitter<TinyDate | TinyDate[] | null>();
329✔
68
    @Output() readonly openChange = new EventEmitter<boolean>(); // Emitted when overlay's open state change
8✔
69
    @Output() readonly inputChange = new EventEmitter<string>();
5✔
70

71
    @ViewChild('origin', { static: true }) origin: CdkOverlayOrigin;
72
    @ViewChild(CdkConnectedOverlay, { static: true }) cdkConnectedOverlay: CdkConnectedOverlay;
3✔
73
    @ViewChild('pickerInput', { static: true }) pickerInput: ElementRef;
74
    @ViewChild('overlayContainer', { static: false }) overlayContainer: ElementRef<HTMLElement>;
75

76
    @Input()
77
    get format() {
157✔
78
        return this.innerFormat;
157✔
79
    }
1✔
80

81
    set format(value: string) {
82
        this.innerFormat = value;
83
        this.updateReadableDate(this.innerValue);
57✔
84
    }
85

86
    @Input()
5✔
87
    get flexibleDateGranularity() {
5!
88
        return this.innerflexibleDateGranularity;
5✔
89
    }
90

5✔
91
    set flexibleDateGranularity(granularity: ThyDateGranularity) {
92
        this.innerflexibleDateGranularity = granularity;
93
        this.updateReadableDate(this.innerValue);
17✔
94
    }
17✔
95

17✔
96
    @Input()
97
    get value() {
98
        return this.innerValue;
8!
99
    }
×
100

101
    set value(value: TinyDate | TinyDate[] | null) {
8!
102
        this.innerValue = value;
8✔
103
        if (!this.entering) {
104
            this.updateReadableDate(this.innerValue);
105
        }
123✔
106
    }
122✔
107

122✔
108
    private innerflexibleDateGranularity: ThyDateGranularity;
122✔
109

122✔
110
    private innerFormat: string;
122!
111

122✔
112
    private innerValue: TinyDate | TinyDate[] | null;
113

114
    entering = false;
115

116
    prefixCls = 'thy-calendar';
117

107✔
118
    isShowDatePopup = false;
55✔
119

55✔
120
    overlayOpen = false; // Available when "open"=undefined
55✔
121

55✔
122
    overlayPositions = getFlexiblePositions(this.placement, 4);
123

124
    get realOpenState(): boolean {
125
        // The value that really decide the open state of overlay
127✔
126
        return this.isOpenHandledByUser() ? !!this.open : this.overlayOpen;
127✔
127
    }
128

129
    get readonlyState(): boolean {
130
        return this.isRange || this.readonly || this.mode !== 'date';
58✔
131
    }
58✔
132

58✔
133
    constructor(
134
        private changeDetector: ChangeDetectorRef,
135
        private dateHelper: DateHelperService
136
    ) {}
126✔
137

123✔
138
    ngOnChanges(changes: SimpleChanges): void {
139
        // open by user
140
        if (changes.open && changes.open.currentValue !== undefined) {
141
            if (changes.open.currentValue) {
12✔
142
                this.showDatePopup();
143
            } else {
144
                this.closeDatePopup();
50✔
145
            }
146
        }
147
    }
126✔
148

149
    ngAfterViewInit(): void {
150
        this.overlayPositions = getFlexiblePositions(this.placement, 4);
6✔
151
        if (this.autoFocus) {
6✔
152
            this.focus();
6✔
153
        }
6✔
154
    }
155

UNCOV
156
    focus(): void {
×
157
        this.pickerInput.nativeElement.focus();
158
    }
159

642✔
160
    onBlur(event: FocusEvent) {
259✔
161
        this.blur.emit(event);
162
        if (this.entering) {
383✔
163
            this.valueChange.emit(this.pickerInput.nativeElement.value);
235✔
164
        }
165
        this.entering = false;
166
    }
148✔
167

168
    onInput(event: InputEvent) {
169
        this.entering = true;
170
        const inputValue = (event.target as HTMLElement)['value'];
171
        this.inputChange.emit(inputValue);
1,011✔
172
    }
173

174
    onEnter() {
175
        if (this.readonlyState) {
675✔
176
            return;
252✔
177
        }
46✔
178
        this.valueChange.emit(this.pickerInput.nativeElement.value || this.getReadableValue(new TinyDate(new Date())));
179
        this.entering = false;
180
    }
206✔
181

206✔
182
    showOverlay(): void {
206✔
183
        if (!this.realOpenState) {
184
            this.overlayOpen = true;
185
            this.showDatePopup();
186

423✔
187
            this.openChange.emit(this.overlayOpen);
423✔
188
            setTimeout(() => {
189
                if (this.cdkConnectedOverlay && this.cdkConnectedOverlay.overlayRef) {
190
                    this.cdkConnectedOverlay.overlayRef.updatePosition();
191
                }
192
            });
193
        }
197✔
194
    }
13✔
195

196
    hideOverlay(): void {
197
        if (this.realOpenState) {
184✔
198
            this.overlayOpen = false;
199
            this.closeDatePopup();
200

201
            this.openChange.emit(this.overlayOpen);
642✔
202
            this.focus();
203
        }
204
    }
205

206
    showDatePopup() {
628✔
207
        this.isShowDatePopup = true;
628✔
208
        this.changeDetector.markForCheck();
40✔
209
    }
210

588✔
211
    closeDatePopup() {
212
        // Delay 200ms before destroying the date-popup, otherwise you will not see the closing animation.
1✔
213
        setTimeout(() => {
214
            this.isShowDatePopup = false;
215
            this.changeDetector.markForCheck();
216
        }, 200);
1✔
217
    }
218

219
    onClickInputBox(): void {
220
        if (!this.disabled && !this.readonly && !this.isOpenHandledByUser()) {
221
            this.showOverlay();
222
        }
223
    }
224

225
    onClickBackdrop(): void {
226
        this.hideOverlay();
227
    }
228

229
    onOverlayDetach(): void {
230
        this.hideOverlay();
231
    }
232

233
    onPositionChange(position: ConnectedOverlayPositionChange): void {
234
        this.changeDetector.detectChanges();
235
    }
236

237
    onClickClear(event: MouseEvent): void {
238
        event.preventDefault();
239
        event.stopPropagation();
240

241
        this.innerValue = this.isRange ? [] : null;
242
        this.valueChange.emit(this.innerValue);
243
    }
244

1✔
245
    getPartTypeIndex(partType: RangePartType): number {
246
        return { left: 0, right: 1 }[partType];
247
    }
248

249
    isEmptyValue(value: CompatibleValue | null): boolean {
250
        if (value === null) {
251
            return true;
252
        } else if (this.isRange) {
253
            return !value || !Array.isArray(value) || value.every(val => !val);
254
        } else {
255
            return !value;
256
        }
257
    }
258

259
    // Whether open state is permanently controlled by user himself
260
    isOpenHandledByUser(): boolean {
261
        return this.open !== undefined;
262
    }
263

264
    getReadableValue(tinyDate: TinyDate | TinyDate[]): string | null {
265
        let value: TinyDate;
266
        if (this.isRange) {
267
            if (this.flexible && this.innerflexibleDateGranularity !== 'day') {
268
                return getFlexibleAdvancedReadableValue(tinyDate as TinyDate[], this.innerflexibleDateGranularity);
269
            } else {
270
                const start = tinyDate[0] ? this.formatDate(tinyDate[0]) : '';
271
                const end = tinyDate[1] ? this.formatDate(tinyDate[1]) : '';
272
                return start && end ? `${start} ~ ${end}` : null;
273
            }
274
        } else {
275
            value = tinyDate as TinyDate;
276
            return value ? this.formatDate(value) : null;
277
        }
278
    }
279

280
    formatDate(value: TinyDate) {
281
        // dateHelper.format() 使用的是 angular 的 format,不支持季度,修改的话,改动比较大。
282
        // 此处通过对 innerFormat 做下判断,如果是季度的 format,使用 date-fns 的 format()
283
        if (this.innerFormat && (this.innerFormat.includes('q') || this.innerFormat.includes('Q'))) {
284
            return value.format(this.innerFormat);
285
        } else {
286
            return this.dateHelper.format(value.nativeDate, this.innerFormat);
287
        }
288
    }
289

290
    getPlaceholder(): string {
291
        return this.isRange && this.placeholder && Array.isArray(this.placeholder)
292
            ? (this.placeholder as string[]).join(' ~ ')
293
            : (this.placeholder as string);
294
    }
295

296
    private updateReadableDate(setValue: TinyDate | TinyDate[] | null) {
297
        const readableValue = this.getReadableValue(setValue);
298
        if (readableValue === this.pickerInput.nativeElement['value']) {
299
            return;
300
        }
301

302
        this.pickerInput.nativeElement.value = readableValue;
303
    }
304
}
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