@@ -21,10 +21,8 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnIn
21
21
@Input ( )
22
22
public value : Date ;
23
23
24
- // TODO
25
- // locale date formats should be the same sa igxdatepicker's - shortDate, longDate, etc
26
24
@Input ( )
27
- public locale : string ;
25
+ public locale = 'en' ;
28
26
29
27
@Input ( )
30
28
public minValue : string | Date ;
@@ -44,14 +42,17 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnIn
44
42
@Output ( )
45
43
public validationFailed = new EventEmitter < IgxDateTimeEditorEventArgs > ( ) ;
46
44
47
- public get format ( ) : string {
45
+ @Input ( )
46
+ public displayFormat : string ;
47
+
48
+ public get inputFormat ( ) : string {
48
49
return this . _format ;
49
50
}
50
51
51
52
@Input ( `igxDateTimeEditor` )
52
- public set format ( value : string ) {
53
+ public set inputFormat ( value : string ) {
53
54
this . _format = value ;
54
- const mask = this . buildMask ( this . format ) ;
55
+ const mask = this . buildMask ( this . inputFormat ) ;
55
56
this . mask = value . indexOf ( 'tt' ) !== - 1 ? mask . substring ( 0 , mask . length - 2 ) + 'LL' : mask ;
56
57
}
57
58
@@ -71,6 +72,10 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnIn
71
72
return literals ;
72
73
}
73
74
75
+ private get emptyInput ( ) {
76
+ return this . maskParser . applyMask ( '' , this . maskOptions ) ;
77
+ }
78
+
74
79
private get targetDatePart ( ) : DatePart {
75
80
if ( this . _document . activeElement === this . nativeElement ) {
76
81
return this . _dateTimeFormatParts . find ( p => p . start <= this . selectionStart && this . selectionStart <= p . end ) . type ;
@@ -94,8 +99,8 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnIn
94
99
95
100
/** @hidden */
96
101
public ngOnInit ( ) : void {
97
- this . _dateTimeFormatParts = DatePickerUtil . parseDateTimeFormat ( DatePickerUtil . setInputFormat ( this . format ) ) ;
98
- this . renderer . setAttribute ( this . nativeElement , 'placeholder' , this . format ) ;
102
+ this . _dateTimeFormatParts = DatePickerUtil . parseDateTimeFormat ( this . inputFormat ) ;
103
+ this . renderer . setAttribute ( this . nativeElement , 'placeholder' , this . inputFormat ) ;
99
104
}
100
105
101
106
public clear ( ) : void {
@@ -105,15 +110,15 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnIn
105
110
106
111
public increment ( datePart ?: DatePart ) : void {
107
112
const newValue = datePart ? this . calculateValueOnSpin ( datePart , 1 ) : this . calculateValueOnSpin ( this . targetDatePart , 1 ) ;
108
- if ( newValue && this . value && newValue !== this . value ) {
113
+ if ( newValue ) {
109
114
this . updateValue ( newValue ) ;
110
115
this . updateMask ( ) ;
111
116
}
112
117
}
113
118
114
119
public decrement ( datePart ?: DatePart ) : void {
115
120
const newValue = datePart ? this . calculateValueOnSpin ( datePart , - 1 ) : this . calculateValueOnSpin ( this . targetDatePart , - 1 ) ;
116
- if ( newValue && this . value && newValue !== this . value ) {
121
+ if ( newValue ) {
117
122
this . updateValue ( newValue ) ;
118
123
this . updateMask ( ) ;
119
124
}
@@ -152,6 +157,7 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnIn
152
157
153
158
/** @hidden */
154
159
public onFocus ( ) : void {
160
+ // TODO: apply mask on focus & focused flag
155
161
this . onTouchCallback ( ) ;
156
162
super . onFocus ( ) ;
157
163
}
@@ -161,19 +167,21 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnIn
161
167
// if inputted string does not fit in the editor, show as many chars as possible followed by "..." ?
162
168
if ( ! this . valueInRange ( this . value ) ) {
163
169
this . validationFailed . emit ( { oldValue : this . _oldValue , newValue : this . value } ) ;
164
- // this.updateMask(); TODO: set empty mask
170
+ this . inputValue = this . emptyInput ;
171
+ this . updateValue ( null ) ;
172
+ return ;
165
173
}
166
174
167
- // TODO: display value pipe
168
- // this.updateMask( );
175
+ const format = this . displayFormat ? this . displayFormat : this . inputFormat ;
176
+ this . inputValue = formatDate ( this . value , format , this . locale ) ;
169
177
this . onTouchCallback ( ) ;
170
178
super . onBlur ( event ) ;
171
179
}
172
180
173
181
/** @hidden */
174
- protected handleInputChanged ( ) : void {
182
+ protected onInputChanged ( ) : void {
175
183
// the mask must be updated before any date operations
176
- super . handleInputChanged ( ) ;
184
+ super . onInputChanged ( ) ;
177
185
if ( this . inputValue === this . maskParser . applyMask ( '' , this . maskOptions ) ) {
178
186
this . updateValue ( null ) ;
179
187
return ;
@@ -187,8 +195,6 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnIn
187
195
this . validationFailed . emit ( { oldValue : this . value , newValue : parsedDate . value } ) ;
188
196
}
189
197
}
190
-
191
- super . afterInput ( ) ;
192
198
}
193
199
194
200
private buildMask ( format : string ) : string {
@@ -213,7 +219,7 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnIn
213
219
}
214
220
215
221
private calculateValueOnSpin ( datePart : DatePart , delta : number ) : Date {
216
- if ( ! this . value ) { return ; }
222
+ if ( ! this . value ) { return null ; }
217
223
const currentDate = this . value as Date ;
218
224
const newDate = new Date ( currentDate . getFullYear ( ) , currentDate . getMonth ( ) , currentDate . getDate ( ) ,
219
225
currentDate . getHours ( ) , currentDate . getMinutes ( ) , currentDate . getSeconds ( ) ) ;
@@ -232,7 +238,9 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnIn
232
238
case DatePart . Seconds :
233
239
return DatePickerUtil . calculateSecondsOnSpin ( delta , newDate , currentDate , this . isSpinLoop ) ;
234
240
case DatePart . AmPm :
235
- return DatePickerUtil . calculateAmPmOnSpin ( delta , newDate , currentDate ) ;
241
+ const formatPart = this . _dateTimeFormatParts . find ( dp => dp . type === DatePart . AmPm ) ;
242
+ const amPmFromMask = this . inputValue . substring ( formatPart . start , formatPart . end ) ;
243
+ return DatePickerUtil . calculateAmPmOnSpin ( newDate , currentDate , amPmFromMask ) ;
236
244
}
237
245
}
238
246
@@ -253,15 +261,34 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnIn
253
261
let targetValue : string = this . getMaskedValue ( p . type , partLength ) ;
254
262
255
263
if ( p . type === DatePart . Month ) {
256
- targetValue = this . prependPromptChars (
257
- parseInt ( targetValue . replace ( new RegExp ( this . promptChar , 'g' ) , '0' ) , 10 ) + 1 , partLength ) ;
264
+ targetValue = this . prependValue (
265
+ parseInt ( targetValue . replace ( new RegExp ( this . promptChar , 'g' ) , '0' ) , 10 ) + 1 , partLength , '0' ) ;
266
+ }
267
+
268
+ if ( p . type === DatePart . Hours && p . format . indexOf ( 'h' ) !== - 1 ) {
269
+ targetValue = this . prependValue ( this . toTwelveHourFormat ( targetValue ) , partLength , '0' ) ;
270
+ }
271
+
272
+ if ( p . type === DatePart . Year && p . format . length === 2 ) {
273
+ targetValue = this . prependValue ( parseInt ( targetValue . slice ( - 2 ) , 10 ) , partLength , '0' ) ;
258
274
}
259
275
260
276
this . inputValue = this . maskParser . replaceInMask ( this . inputValue , targetValue , this . maskOptions , p . start , p . end ) . value ;
261
277
} ) ;
262
278
this . setSelectionRange ( cursor ) ;
263
279
}
264
280
281
+ private toTwelveHourFormat ( value : string ) : number {
282
+ let hour = parseInt ( value . replace ( new RegExp ( this . promptChar , 'g' ) , '0' ) , 10 ) ;
283
+ if ( hour > 12 ) {
284
+ hour -= 12 ;
285
+ } else if ( hour === 0 ) {
286
+ hour = 12 ;
287
+ }
288
+
289
+ return hour ;
290
+ }
291
+
265
292
private getMaskedValue ( datePart : DatePart , partLength : number ) : string {
266
293
let maskedValue ;
267
294
switch ( datePart ) {
@@ -289,14 +316,14 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnIn
289
316
}
290
317
291
318
if ( datePart !== DatePart . AmPm ) {
292
- return this . prependPromptChars ( maskedValue , partLength ) ;
319
+ return this . prependValue ( maskedValue , partLength , '0' ) ;
293
320
}
294
321
295
322
return maskedValue ;
296
323
}
297
324
298
- private prependPromptChars ( value : number , partLength : number ) : string {
299
- return ( this . promptChar + value . toString ( ) ) . slice ( - partLength ) ;
325
+ private prependValue ( value : number , partLength : number , prependChar : string ) : string {
326
+ return ( prependChar + value . toString ( ) ) . slice ( - partLength ) ;
300
327
}
301
328
302
329
private spin ( event : KeyboardEvent ) : void {
0 commit comments