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

atinc / ngx-tethys / 4ffbd06a-676e-4e5d-9a69-34811ff1d9ac

07 Dec 2023 12:41PM UTC coverage: 90.359% (-0.004%) from 90.363%
4ffbd06a-676e-4e5d-9a69-34811ff1d9ac

Pull #2951

circleci

minlovehua
reset(date-picker): solve the problem that the animation does not take effect when the picker is closed #INFR-10809
Pull Request #2951: feat(date-picker): support animations for date-picker, week-picker, month-picker, year-picker INFR-10809

5347 of 6578 branches covered (0.0%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

5 existing lines in 2 files now uncovered.

13322 of 14083 relevant lines covered (94.6%)

972.98 hits per line

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

94.12
/src/date-picker/picker.component.ts
1
import { getFlexiblePositions, ThyPlacement } from 'ngx-tethys/core';
2
import { 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
    Output,
14
    ViewChild
15
} from '@angular/core';
16

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

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

144✔
68
    @ViewChild('origin', { static: true }) origin: CdkOverlayOrigin;
1✔
69
    @ViewChild(CdkConnectedOverlay, { static: true }) cdkConnectedOverlay: CdkConnectedOverlay;
70
    @ViewChild('pickerInput', { static: true }) pickerInput: ElementRef;
71

72
    @Input()
54✔
73
    get format() {
74
        return this.innerFormat;
75
    }
5✔
76

5!
77
    set format(value: string) {
5✔
78
        this.innerFormat = value;
79
        this.updateReadableDate(this.innerValue);
5✔
80
    }
81

82
    @Input()
17✔
83
    get flexibleDateGranularity() {
17✔
84
        return this.innerflexibleDateGranularity;
17✔
85
    }
86

87
    set flexibleDateGranularity(granularity: ThyDateGranularity) {
8!
UNCOV
88
        this.innerflexibleDateGranularity = granularity;
×
89
        this.updateReadableDate(this.innerValue);
90
    }
8!
91

8✔
92
    @Input()
93
    get value() {
94
        return this.innerValue;
113✔
95
    }
112✔
96

112!
97
    set value(value: TinyDate | TinyDate[] | null) {
112✔
98
        this.innerValue = value;
99
        if (!this.entering) {
112✔
100
            this.updateReadableDate(this.innerValue);
112✔
101
        }
112!
102
    }
112✔
103

104
    private innerflexibleDateGranularity: ThyDateGranularity;
105

106
    private innerFormat: string;
107

108
    private innerValue: TinyDate | TinyDate[] | null;
101✔
109

52✔
110
    entering = false;
52✔
111

46✔
112
    prefixCls = 'thy-calendar';
113

52✔
114
    animationOpenState = false;
52✔
115

116
    overlayOpen = false; // Available when "open"=undefined
117

118
    overlayPositions = getFlexiblePositions(this.placement, 4);
116✔
119

113✔
120
    get realOpenState(): boolean {
121
        // The value that really decide the open state of overlay
122
        return this.isOpenHandledByUser() ? !!this.open : this.overlayOpen;
123
    }
10✔
124

125
    get readonlyState(): boolean {
126
        return this.isRange || this.readonly || this.mode !== 'date';
47✔
127
    }
128

129
    constructor(private changeDetector: ChangeDetectorRef, private dateHelper: DateHelperService) {}
218✔
130

131
    ngAfterViewInit(): void {
132
        this.overlayPositions = getFlexiblePositions(this.placement, 4);
5✔
133
        if (this.autoFocus) {
5✔
134
            this.focus();
5✔
135
        }
5✔
136
    }
137

UNCOV
138
    focus(): void {
×
139
        this.pickerInput.nativeElement.focus();
140
    }
141

653✔
142
    onBlur(event: FocusEvent) {
263✔
143
        this.blur.emit(event);
144
        if (this.entering) {
390✔
145
            this.valueChange.emit(this.pickerInput.nativeElement.value);
264✔
146
        }
147
        this.entering = false;
148
    }
126✔
149

150
    onInput(event: InputEvent) {
151
        this.entering = true;
152
        const inputValue = (event.target as HTMLElement)['value'];
153
        this.inputChange.emit(inputValue);
2,386✔
154
    }
155

156
    onEnter() {
157
        if (this.readonlyState) {
628✔
158
            return;
262✔
159
        }
46✔
160
        this.valueChange.emit(this.pickerInput.nativeElement.value || this.getReadableValue(new TinyDate(new Date())));
161
        this.entering = false;
162
    }
216✔
163

216✔
164
    showOverlay(): void {
216✔
165
        if (!this.realOpenState) {
166
            this.overlayOpen = true;
167
            if (this.realOpenState) {
168
                this.animationOpenState = true;
366✔
169
            }
366✔
170
            this.openChange.emit(this.overlayOpen);
171
            setTimeout(() => {
172
                if (this.cdkConnectedOverlay && this.cdkConnectedOverlay.overlayRef) {
173
                    this.cdkConnectedOverlay.overlayRef.updatePosition();
653✔
174
                }
175
            });
176
        }
177
    }
178

581✔
179
    hideOverlay(): void {
581✔
180
        if (this.realOpenState) {
40✔
181
            this.overlayOpen = false;
182
            if (!this.realOpenState) {
541✔
183
                this.animationOpenState = false;
184
            }
1✔
185
            this.openChange.emit(this.overlayOpen);
186
            this.focus();
187
        }
188
    }
1✔
189

190
    onClickInputBox(): void {
191
        if (!this.disabled && !this.readonly && !this.isOpenHandledByUser()) {
192
            this.showOverlay();
193
        }
194
    }
195

196
    onClickBackdrop(): void {
197
        this.hideOverlay();
198
    }
199

200
    onOverlayDetach(): void {
201
        this.hideOverlay();
202
    }
203

204
    onPositionChange(position: ConnectedOverlayPositionChange): void {
205
        this.changeDetector.detectChanges();
206
    }
207

208
    onClickClear(event: MouseEvent): void {
209
        event.preventDefault();
210
        event.stopPropagation();
211

212
        this.innerValue = this.isRange ? [] : null;
213
        this.valueChange.emit(this.innerValue);
214
    }
1✔
215

216
    getPartTypeIndex(partType: RangePartType): number {
217
        return { left: 0, right: 1 }[partType];
218
    }
219

220
    isEmptyValue(value: CompatibleValue | null): boolean {
221
        if (value === null) {
222
            return true;
223
        } else if (this.isRange) {
224
            return !value || !Array.isArray(value) || value.every(val => !val);
225
        } else {
226
            return !value;
227
        }
228
    }
229

230
    // Whether open state is permanently controlled by user himself
231
    isOpenHandledByUser(): boolean {
232
        return this.open !== undefined;
233
    }
234

235
    getReadableValue(tinyDate: TinyDate | TinyDate[]): string | null {
236
        let value: TinyDate;
237
        if (this.isRange) {
238
            if (this.flexible && this.innerflexibleDateGranularity !== 'day') {
239
                return getFlexibleAdvancedReadableValue(tinyDate as TinyDate[], this.innerflexibleDateGranularity);
240
            } else {
241
                const start = tinyDate[0] ? this.dateHelper.format(tinyDate[0].nativeDate, this.innerFormat) : '';
242
                const end = tinyDate[1] ? this.dateHelper.format(tinyDate[1].nativeDate, this.innerFormat) : '';
243
                return start && end ? `${start} ~ ${end}` : null;
244
            }
245
        } else {
246
            value = tinyDate as TinyDate;
247
            return value ? this.dateHelper.format(value.nativeDate, this.innerFormat) : null;
248
        }
249
    }
250

251
    getPlaceholder(): string {
252
        return this.isRange && this.placeholder && Array.isArray(this.placeholder)
253
            ? (this.placeholder as string[]).join(' ~ ')
254
            : (this.placeholder as string);
255
    }
256

257
    private updateReadableDate(setValue: TinyDate | TinyDate[] | null) {
258
        const readableValue = this.getReadableValue(setValue);
259
        if (readableValue === this.pickerInput.nativeElement['value']) {
260
            return;
261
        }
262

263
        this.pickerInput.nativeElement.value = readableValue;
264
    }
265
}
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