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

atinc / ngx-tethys / 5b99ba5b-d098-425e-bce7-d706396c8f83

16 Nov 2023 01:29PM UTC coverage: 90.187% (-0.01%) from 90.201%
5b99ba5b-d098-425e-bce7-d706396c8f83

Pull #2900

circleci

su4g
fix(date-picker): adjust updateReadableDate function
Pull Request #2900: fix(date-picker): adjust updateReadableDate function INFR-10167

5266 of 6502 branches covered (0.0%)

Branch coverage included in aggregate %.

13171 of 13941 relevant lines covered (94.48%)

978.15 hits per line

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

94.08
/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

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

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

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

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

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

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

5!
86
    set flexibleDateGranularity(granularity: ThyDateGranularity) {
×
87
        this.innerflexibleDateGranularity = granularity;
88
        this.updateReadableDate(this.innerValue);
5!
89
    }
5✔
90

91
    @Input()
92
    get value() {
111✔
93
        return this.innerValue;
110✔
94
    }
110!
95

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

103
    private innerflexibleDateGranularity: ThyDateGranularity;
104

105
    private innerFormat: string;
106

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

52✔
109
    entering = false;
46✔
110

111
    prefixCls = 'thy-calendar';
52✔
112

52✔
113
    animationOpenState = false;
114

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

114✔
117
    overlayPositions = getFlexiblePositions(this.placement, 4);
111✔
118

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

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

214✔
128
    constructor(private changeDetector: ChangeDetectorRef, private dateHelper: DateHelperService) {}
129

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

×
137
    focus(): void {
138
        this.pickerInput.nativeElement.focus();
139
    }
641✔
140

255✔
141
    onBlur(event: FocusEvent) {
142
        if (this.entering) {
386✔
143
            this.valueChange.emit(this.pickerInput.nativeElement.value);
264✔
144
        }
145
        this.entering = false;
146
    }
122✔
147

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

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

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

571✔
177
    hideOverlay(): void {
571✔
178
        if (this.realOpenState) {
39✔
179
            this.overlayOpen = false;
180
            if (!this.realOpenState) {
532✔
181
                this.animationOpenState = false;
182
            }
1✔
183
            this.openChange.emit(this.overlayOpen);
184
            this.focus();
185
        }
186
    }
1✔
187

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

194
    onClickBackdrop(): void {
195
        this.hideOverlay();
196
    }
197

198
    onOverlayDetach(): void {
199
        this.hideOverlay();
200
    }
201

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

206
    onClickClear(event: MouseEvent): void {
207
        event.preventDefault();
208
        event.stopPropagation();
209

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

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

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

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

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

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

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

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