@@ -58,54 +58,50 @@ if (languages.default?.default["en"]?.weekdays) {
58
58
<div
59
59
class="cds--date-picker"
60
60
[ngClass]="{
61
- 'cds--date-picker--range' : range,
62
- 'cds--date-picker--single' : !range,
61
+ 'cds--date-picker--simple' : datePickerType === 'simple',
62
+ 'cds--date-picker--range' : datePickerType === 'range',
63
+ 'cds--date-picker--single' : datePickerType === 'single',
63
64
'cds--date-picker--light' : theme === 'light',
64
65
'cds--skeleton' : skeleton
65
66
}">
66
- <div class="cds--date-picker-container">
67
- <cds-date-picker-input
68
- #input
69
- [label]="label"
70
- [placeholder]="placeholder"
71
- [pattern]="inputPattern"
72
- [id]="id + '-input'"
73
- [size]="size"
74
- [type]="(range ? 'range' : 'single')"
75
- [hasIcon]="(range ? false : true)"
76
- [disabled]="disabled"
77
- [invalid]="invalid"
78
- [invalidText]="invalidText"
79
- [warn]="warn"
80
- [warnText]="warnText"
81
- [skeleton]="skeleton"
82
- [helperText]="helperText"
83
- (valueChange)="onValueChange($event)"
84
- (click)="openCalendar(input)">
85
- </cds-date-picker-input>
86
- </div>
87
-
88
- <div *ngIf="range" class="cds--date-picker-container">
89
- <cds-date-picker-input
90
- #rangeInput
91
- [label]="rangeLabel"
92
- [placeholder]="placeholder"
93
- [pattern]="inputPattern"
94
- [id]="id + '-rangeInput'"
95
- [size]="size"
96
- [type]="(range ? 'range' : 'single')"
97
- [hasIcon]="(range ? true : null)"
98
- [disabled]="disabled"
99
- [invalid]="rangeInvalid"
100
- [invalidText]="rangeInvalidText"
101
- [warn]="rangeWarn"
102
- [warnText]="rangeWarnText"
103
- [skeleton]="skeleton"
104
- [helperText]="rangeHelperText"
105
- (valueChange)="onRangeValueChange($event)"
106
- (click)="openCalendar(rangeInput)">
107
- </cds-date-picker-input>
108
- </div>
67
+ <cds-date-picker-input
68
+ #input
69
+ [label]="label"
70
+ [placeholder]="placeholder"
71
+ [pattern]="inputPattern"
72
+ [id]="id + '-input'"
73
+ [size]="size"
74
+ [type]="datePickerType"
75
+ [disabled]="disabled"
76
+ [invalid]="invalid"
77
+ [invalidText]="invalidText"
78
+ [warn]="warn"
79
+ [warnText]="warnText"
80
+ [skeleton]="skeleton"
81
+ [helperText]="helperText"
82
+ (valueChange)="onValueChange($event)"
83
+ (click)="openCalendar(input)">
84
+ </cds-date-picker-input>
85
+
86
+ <cds-date-picker-input
87
+ *ngIf="datePickerType === 'range'"
88
+ #rangeInput
89
+ [label]="rangeLabel"
90
+ [placeholder]="placeholder"
91
+ [pattern]="inputPattern"
92
+ [id]="id + '-rangeInput'"
93
+ [size]="size"
94
+ type="range"
95
+ [disabled]="disabled"
96
+ [invalid]="rangeInvalid"
97
+ [invalidText]="rangeInvalidText"
98
+ [warn]="rangeWarn"
99
+ [warnText]="rangeWarnText"
100
+ [skeleton]="skeleton"
101
+ [helperText]="rangeHelperText"
102
+ (valueChange)="onRangeValueChange($event)"
103
+ (click)="openCalendar(rangeInput)">
104
+ </cds-date-picker-input>
109
105
</div>
110
106
</div>
111
107
` ,
@@ -126,10 +122,7 @@ export class DatePicker implements
126
122
AfterViewInit {
127
123
private static datePickerCount = 0 ;
128
124
129
- /**
130
- * Select calendar range mode
131
- */
132
- @Input ( ) range = false ;
125
+ @Input ( ) datePickerType : "simple" | "single" | "range" = "simple" ;
133
126
134
127
/**
135
128
* Format of date
@@ -228,11 +221,11 @@ export class DatePicker implements
228
221
}
229
222
get flatpickrOptions ( ) : Partial < Options > {
230
223
const plugins = [ ...this . plugins , carbonFlatpickrMonthSelectPlugin ] ;
231
- if ( this . range ) {
224
+ if ( this . datePickerType === " range" ) {
232
225
plugins . push ( rangePlugin ( { input : `#${ this . id } -rangeInput` , position : "left" } ) ) ;
233
226
}
234
227
return Object . assign ( { } , this . _flatpickrOptions , this . flatpickrBaseOptions , {
235
- mode : this . range ? "range" : "single" ,
228
+ mode : this . datePickerType === " range" ? "range" : "single" ,
236
229
plugins,
237
230
dateFormat : this . dateFormat ,
238
231
locale : languages . default ?. default [ this . language ] || languages . default [ this . language ]
@@ -268,7 +261,7 @@ export class DatePicker implements
268
261
onClose : ( date ) => {
269
262
// This makes sure that the `flatpickrInstance selectedDates` are in sync with the values of
270
263
// the inputs when the calendar closes.
271
- if ( this . range && this . flatpickrInstance ) {
264
+ if ( this . datePickerType === " range" && this . flatpickrInstance ) {
272
265
const inputValue = this . input . input . nativeElement . value ;
273
266
const rangeInputValue = this . rangeInput . input . nativeElement . value ;
274
267
if ( inputValue || rangeInputValue ) {
@@ -332,7 +325,7 @@ export class DatePicker implements
332
325
// and because we rely on a library that operates outside the Angular view of the world
333
326
// we need to keep trying to load the library, until the relevant DOM is actually live
334
327
ngAfterViewChecked ( ) {
335
- if ( ! this . isFlatpickrLoaded ( ) ) {
328
+ if ( this . datePickerType !== "simple" && ! this . isFlatpickrLoaded ( ) ) {
336
329
// @ts -ignore ts is unhappy with the below call to `flatpickr`
337
330
this . flatpickrInstance = flatpickr ( `#${ this . id } -input` , this . flatpickrOptions ) ;
338
331
// if (and only if) the initialization succeeded, we can set the date values
@@ -348,7 +341,7 @@ export class DatePicker implements
348
341
onFocus ( ) {
349
342
// Updates the month manually when calendar mode is range because month
350
343
// will not update properly without manually updating them on focus.
351
- if ( this . range ) {
344
+ if ( this . datePickerType === " range" ) {
352
345
if ( this . rangeInput . input . nativeElement === document . activeElement && this . flatpickrInstance . selectedDates [ 1 ] ) {
353
346
const currentMonth = this . flatpickrInstance . selectedDates [ 1 ] . getMonth ( ) ;
354
347
this . flatpickrInstance . changeMonth ( currentMonth , false ) ;
@@ -414,7 +407,7 @@ export class DatePicker implements
414
407
onValueChange ( event : string ) {
415
408
if ( this . isFlatpickrLoaded ( ) ) {
416
409
const date = this . flatpickrInstance . parseDate ( event , this . dateFormat ) ;
417
- if ( this . range ) {
410
+ if ( this . datePickerType === " range" ) {
418
411
this . setDateValues ( [ date , this . flatpickrInstance . selectedDates [ 1 ] ] ) ;
419
412
} else {
420
413
this . setDateValues ( [ date ] ) ;
@@ -438,7 +431,7 @@ export class DatePicker implements
438
431
* Handles opening the calendar "properly" when the calendar icon is clicked.
439
432
*/
440
433
openCalendar ( datepickerInput : DatePickerInput ) {
441
- if ( this . range ) {
434
+ if ( this . datePickerType === " range" ) {
442
435
datepickerInput . input . nativeElement . click ( ) ;
443
436
444
437
// If the first input's calendar icon is clicked when calendar is in range mode, then
@@ -469,7 +462,7 @@ export class DatePicker implements
469
462
* Handles the initialization of event listeners for the datepicker input and range input fields.
470
463
*/
471
464
protected addInputListeners ( ) {
472
- if ( ! this . isFlatpickrLoaded ( ) ) {
465
+ if ( this . datePickerType === "simple" || ! this . isFlatpickrLoaded ( ) ) {
473
466
return ;
474
467
}
475
468
@@ -520,7 +513,7 @@ export class DatePicker implements
520
513
* @param newDates An optional SimpleChange of date values
521
514
*/
522
515
protected resetFlatpickrInstance ( newDates ?: SimpleChange ) {
523
- if ( this . isFlatpickrLoaded ( ) ) {
516
+ if ( this . datePickerType !== "simple" && this . isFlatpickrLoaded ( ) ) {
524
517
let dates = this . flatpickrInstance . selectedDates ;
525
518
if ( newDates && this . didDateValueChange ( newDates . currentValue , newDates . previousValue ) ) {
526
519
dates = newDates . currentValue ;
@@ -646,7 +639,7 @@ export class DatePicker implements
646
639
// In range mode, if a date is selected from the first calendar that is from the previous month,
647
640
// the month will not be updated on the calendar until the calendar is re-opened.
648
641
// This will make sure the calendar is updated with the correct month.
649
- if ( this . range && this . flatpickrInstance . selectedDates [ 0 ] ) {
642
+ if ( this . datePickerType === " range" && this . flatpickrInstance . selectedDates [ 0 ] ) {
650
643
const currentMonth = this . flatpickrInstance . selectedDates [ 0 ] . getMonth ( ) ;
651
644
652
645
// `flatpickrInstance.changeMonth` removes the focus on the selected date element and will
0 commit comments