@@ -100,27 +100,22 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnIn
100
100
101
101
public clear ( ) : void {
102
102
this . showMask ( '' ) ;
103
- this . value = null ;
104
- this . valueChanged . emit ( { oldValue : this . _oldValue , newValue : this . value } ) ;
103
+ this . updateValue ( null ) ;
105
104
}
106
105
107
106
public increment ( datePart ?: DatePart ) : void {
108
107
const newValue = datePart ? this . calculateValueOnSpin ( datePart , 1 ) : this . calculateValueOnSpin ( this . targetDatePart , 1 ) ;
109
108
if ( newValue && this . value && newValue !== this . value ) {
110
109
this . updateValue ( newValue ) ;
110
+ this . updateMask ( ) ;
111
111
}
112
-
113
- // TODO: update mask
114
- // this.updateMask();
115
112
}
116
113
117
114
public decrement ( datePart ?: DatePart ) : void {
118
115
const newValue = datePart ? this . calculateValueOnSpin ( datePart , - 1 ) : this . calculateValueOnSpin ( this . targetDatePart , - 1 ) ;
119
116
if ( newValue && this . value && newValue !== this . value ) {
120
117
this . updateValue ( newValue ) ;
121
-
122
- // TODO: update mask
123
- // this.updateMask(true);
118
+ this . updateMask ( ) ;
124
119
}
125
120
}
126
121
@@ -148,9 +143,7 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnIn
148
143
}
149
144
150
145
if ( event . ctrlKey && event . key === KEYS . SEMICOLON ) {
151
- // TODO: emit success & update mask?
152
- this . value = new Date ( ) ;
153
- this . valueChanged . emit ( { oldValue : this . _oldValue , newValue : this . value } ) ;
146
+ this . updateValue ( new Date ( ) ) ;
154
147
this . updateMask ( ) ;
155
148
}
156
149
@@ -172,7 +165,7 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnIn
172
165
}
173
166
174
167
// TODO: display value pipe
175
- // this.updateMask(); TODO: fill in any empty date parts
168
+ // this.updateMask();
176
169
this . onTouchCallback ( ) ;
177
170
super . onBlur ( event ) ;
178
171
}
@@ -181,9 +174,18 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnIn
181
174
protected handleInputChanged ( ) : void {
182
175
// the mask must be updated before any date operations
183
176
super . handleInputChanged ( ) ;
177
+ if ( this . inputValue === this . maskParser . applyMask ( '' , this . maskOptions ) ) {
178
+ this . updateValue ( null ) ;
179
+ return ;
180
+ }
181
+
184
182
const parsedDate = this . parseDate ( this . inputValue ) ;
185
- if ( parsedDate . state === DateState . Valid && this . inputValue . indexOf ( this . promptChar ) === - 1 ) {
186
- this . updateValue ( parsedDate . value ) ;
183
+ if ( this . inputValue . indexOf ( this . promptChar ) === - 1 ) { // better way to check for filled input?
184
+ if ( parsedDate . state === DateState . Valid ) {
185
+ this . updateValue ( parsedDate . value ) ;
186
+ } else {
187
+ this . validationFailed . emit ( { oldValue : this . value , newValue : parsedDate . value } ) ;
188
+ }
187
189
}
188
190
189
191
super . afterInput ( ) ;
@@ -198,6 +200,7 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnIn
198
200
}
199
201
200
202
private valueInRange ( value : Date ) : boolean {
203
+ if ( ! value ) { return ; }
201
204
const maxValueAsDate = this . isDate ( this . maxValue ) ? this . maxValue : this . parseDate ( this . maxValue ) . value ;
202
205
const minValueAsDate = this . isDate ( this . minValue ) ? this . minValue : this . parseDate ( this . minValue ) . value ;
203
206
if ( maxValueAsDate && minValueAsDate ) {
@@ -228,6 +231,8 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnIn
228
231
return DatePickerUtil . calculateMinutesOnSpin ( delta , newDate , currentDate , this . isSpinLoop ) ;
229
232
case DatePart . Seconds :
230
233
return DatePickerUtil . calculateSecondsOnSpin ( delta , newDate , currentDate , this . isSpinLoop ) ;
234
+ case DatePart . AmPm :
235
+ return DatePickerUtil . calculateAmPmOnSpin ( delta , newDate , currentDate ) ;
231
236
}
232
237
}
233
238
@@ -244,47 +249,54 @@ export class IgxDateTimeEditorDirective extends IgxMaskDirective implements OnIn
244
249
private updateMask ( ) {
245
250
const cursor = this . selectionEnd ;
246
251
this . _dateTimeFormatParts . forEach ( p => {
247
- // TODO: cycle through the parts and update the mask based on their indices
248
- let value : number = this . breakUpDate ( p . type ) ;
249
- // TODO: append all date parts from the date object to one another
250
- // if a part of that date object's length is less than the expected length (taken from the format) -> prepend prompt chars
251
- value = p . type === DatePart . Month ? value + 1 : value ;
252
- this . inputValue = this . maskParser . replaceInMask ( this . inputValue , `${ value } ` , this . maskOptions , p . start , p . end ) . value ;
252
+ const partLength = p . end - p . start ;
253
+ let targetValue : string = this . getMaskedValue ( p . type , partLength ) ;
254
+
255
+ if ( p . type === DatePart . Month ) {
256
+ targetValue = this . prependPromptChars (
257
+ parseInt ( targetValue . replace ( new RegExp ( this . promptChar , 'g' ) , '0' ) , 10 ) + 1 , partLength ) ;
258
+ }
259
+
260
+ this . inputValue = this . maskParser . replaceInMask ( this . inputValue , targetValue , this . maskOptions , p . start , p . end ) . value ;
253
261
} ) ;
254
262
this . setSelectionRange ( cursor ) ;
255
263
}
256
264
257
- private breakUpDate ( datePart : DatePart ) : number {
258
- const valueAsDate = this . value as Date ;
265
+ private getMaskedValue ( datePart : DatePart , partLength : number ) : string {
266
+ let maskedValue ;
259
267
switch ( datePart ) {
260
268
case DatePart . Date :
261
- return valueAsDate . getDate ( ) ;
269
+ maskedValue = this . value . getDate ( ) ;
270
+ break ;
262
271
case DatePart . Month :
263
- return valueAsDate . getMonth ( ) ;
272
+ maskedValue = this . value . getMonth ( ) ;
273
+ break ;
264
274
case DatePart . Year :
265
- return valueAsDate . getFullYear ( ) ;
275
+ maskedValue = this . value . getFullYear ( ) ;
276
+ break ;
266
277
case DatePart . Hours :
267
- return valueAsDate . getHours ( ) ;
278
+ maskedValue = this . value . getHours ( ) ;
279
+ break ;
268
280
case DatePart . Minutes :
269
- return valueAsDate . getMinutes ( ) ;
281
+ maskedValue = this . value . getMinutes ( ) ;
282
+ break ;
270
283
case DatePart . Seconds :
271
- return valueAsDate . getSeconds ( ) ;
284
+ maskedValue = this . value . getSeconds ( ) ;
285
+ break ;
286
+ case DatePart . AmPm :
287
+ maskedValue = this . value . getHours ( ) >= 12 ? 'PM' : 'AM' ;
288
+ break ;
272
289
}
273
- }
274
290
275
- private updateMaskOnSpin ( parsedValue : number , editedParts : DatePartInfo [ ] , addPromptChar ?: boolean ) {
276
- let start = editedParts [ 0 ] . start ;
277
- const end = editedParts [ editedParts . length - 1 ] . start ;
278
- if ( parsedValue . toString ( ) . length < editedParts . length ) {
279
- start += editedParts . length - parsedValue . toString ( ) . length ;
280
- if ( addPromptChar ) {
281
- this . inputValue = this . maskParser . replaceCharAt ( this . inputValue , start - 1 , this . promptChar ) ;
282
- }
291
+ if ( datePart !== DatePart . AmPm ) {
292
+ return this . prependPromptChars ( maskedValue , partLength ) ;
283
293
}
284
294
285
- this . inputValue = this . maskParser . replaceInMask (
286
- this . inputValue , `${ parsedValue } ` , this . maskOptions , start , end ) . value ;
287
- this . setSelectionRange ( this . end ) ;
295
+ return maskedValue ;
296
+ }
297
+
298
+ private prependPromptChars ( value : number , partLength : number ) : string {
299
+ return ( this . promptChar + value . toString ( ) ) . slice ( - partLength ) ;
288
300
}
289
301
290
302
private spin ( event : KeyboardEvent ) : void {
0 commit comments