-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
datepicker-input-base.ts
393 lines (331 loc) · 13.3 KB
/
datepicker-input-base.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {BooleanInput, coerceBooleanProperty} from '@angular/cdk/coercion';
import {DOWN_ARROW} from '@angular/cdk/keycodes';
import {
Directive,
ElementRef,
EventEmitter,
Inject,
Input,
OnDestroy,
Optional,
Output,
AfterViewInit,
OnChanges,
} from '@angular/core';
import {
AbstractControl,
ControlValueAccessor,
ValidationErrors,
Validator,
ValidatorFn,
} from '@angular/forms';
import {
DateAdapter,
MAT_DATE_FORMATS,
MatDateFormats,
} from '@angular/material/core';
import {Subscription, Subject} from 'rxjs';
import {createMissingDateImplError} from './datepicker-errors';
import {
ExtractDateTypeFromSelection,
MatDateSelectionModel,
DateSelectionModelChange,
} from './date-selection-model';
/**
* An event used for datepicker input and change events. We don't always have access to a native
* input or change event because the event may have been triggered by the user clicking on the
* calendar popup. For consistency, we always use MatDatepickerInputEvent instead.
*/
export class MatDatepickerInputEvent<D, S = unknown> {
/** The new value for the target datepicker input. */
value: D | null;
constructor(
/** Reference to the datepicker input component that emitted the event. */
public target: MatDatepickerInputBase<S, D>,
/** Reference to the native input element associated with the datepicker input. */
public targetElement: HTMLElement) {
this.value = this.target.value;
}
}
/** Function that can be used to filter out dates from a calendar. */
export type DateFilterFn<D> = (date: D | null) => boolean;
/** Base class for datepicker inputs. */
@Directive()
export abstract class MatDatepickerInputBase<S, D = ExtractDateTypeFromSelection<S>>
implements ControlValueAccessor, AfterViewInit, OnChanges, OnDestroy, Validator {
/** Whether the component has been initialized. */
private _isInitialized: boolean;
/** The value of the input. */
@Input()
get value(): D | null {
return this._model ? this._getValueFromModel(this._model.selection) : this._pendingValue;
}
set value(value: D | null) {
value = this._dateAdapter.deserialize(value);
this._lastValueValid = this._isValidValue(value);
value = this._dateAdapter.getValidDateOrNull(value);
const oldDate = this.value;
this._assignValue(value);
this._formatValue(value);
if (!this._dateAdapter.sameDate(oldDate, value)) {
this._valueChange.emit(value);
}
}
protected _model: MatDateSelectionModel<S, D> | undefined;
/** Whether the datepicker-input is disabled. */
@Input()
get disabled(): boolean { return !!this._disabled || this._parentDisabled(); }
set disabled(value: boolean) {
const newValue = coerceBooleanProperty(value);
const element = this._elementRef.nativeElement;
if (this._disabled !== newValue) {
this._disabled = newValue;
this._stateChanges.next(undefined);
}
// We need to null check the `blur` method, because it's undefined during SSR.
// In Ivy static bindings are invoked earlier, before the element is attached to the DOM.
// This can cause an error to be thrown in some browsers (IE/Edge) which assert that the
// element has been inserted.
if (newValue && this._isInitialized && element.blur) {
// Normally, native input elements automatically blur if they turn disabled. This behavior
// is problematic, because it would mean that it triggers another change detection cycle,
// which then causes a changed after checked error if the input element was focused before.
element.blur();
}
}
private _disabled: boolean;
/** Emits when a `change` event is fired on this `<input>`. */
@Output() readonly dateChange: EventEmitter<MatDatepickerInputEvent<D, S>> =
new EventEmitter<MatDatepickerInputEvent<D, S>>();
/** Emits when an `input` event is fired on this `<input>`. */
@Output() readonly dateInput: EventEmitter<MatDatepickerInputEvent<D, S>> =
new EventEmitter<MatDatepickerInputEvent<D, S>>();
/** Emits when the value changes (either due to user input or programmatic change). */
_valueChange = new EventEmitter<D | null>();
/** Emits when the internal state has changed */
_stateChanges = new Subject<void>();
_onTouched = () => {};
_validatorOnChange = () => {};
protected _cvaOnChange: (value: any) => void = () => {};
private _valueChangesSubscription = Subscription.EMPTY;
private _localeSubscription = Subscription.EMPTY;
/**
* Since the value is kept on the model which is assigned in an Input,
* we might get a value before we have a model. This property keeps track
* of the value until we have somewhere to assign it.
*/
private _pendingValue: D | null;
/** The form control validator for whether the input parses. */
private _parseValidator: ValidatorFn = (): ValidationErrors | null => {
return this._lastValueValid ?
null : {'matDatepickerParse': {'text': this._elementRef.nativeElement.value}};
}
/** The form control validator for the date filter. */
private _filterValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
const controlValue = this._dateAdapter.getValidDateOrNull(
this._dateAdapter.deserialize(control.value));
const dateFilter = this._getDateFilter();
return !dateFilter || !controlValue || dateFilter(controlValue) ?
null : {'matDatepickerFilter': true};
}
/** The form control validator for the min date. */
private _minValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
const controlValue = this._dateAdapter.getValidDateOrNull(
this._dateAdapter.deserialize(control.value));
const min = this._getMinDate();
return (!min || !controlValue ||
this._dateAdapter.compareDate(min, controlValue) <= 0) ?
null : {'matDatepickerMin': {'min': min, 'actual': controlValue}};
}
/** The form control validator for the max date. */
private _maxValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
const controlValue = this._dateAdapter.getValidDateOrNull(
this._dateAdapter.deserialize(control.value));
const max = this._getMaxDate();
return (!max || !controlValue ||
this._dateAdapter.compareDate(max, controlValue) >= 0) ?
null : {'matDatepickerMax': {'max': max, 'actual': controlValue}};
}
/** Gets the base validator functions. */
protected _getValidators(): ValidatorFn[] {
return [this._parseValidator, this._minValidator, this._maxValidator, this._filterValidator];
}
/** Gets the minimum date for the input. Used for validation. */
abstract _getMinDate(): D | null;
/** Gets the maximum date for the input. Used for validation. */
abstract _getMaxDate(): D | null;
/** Gets the date filter function. Used for validation. */
protected abstract _getDateFilter(): DateFilterFn<D> | undefined;
/** Registers a date selection model with the input. */
_registerModel(model: MatDateSelectionModel<S, D>): void {
this._model = model;
this._valueChangesSubscription.unsubscribe();
if (this._pendingValue) {
this._assignValue(this._pendingValue);
}
this._valueChangesSubscription = this._model.selectionChanged.subscribe(event => {
if (event.source !== this) {
const value = this._getValueFromModel(event.selection);
this._lastValueValid = this._isValidValue(value);
this._cvaOnChange(value);
this._onTouched();
this._formatValue(value);
// Note that we can't wrap the entire block with this logic, because for the range inputs
// we want to revalidate whenever either one of the inputs changes and we don't have a
// good way of distinguishing it at the moment.
if (this._canEmitChangeEvent(event)) {
this.dateInput.emit(new MatDatepickerInputEvent(this, this._elementRef.nativeElement));
this.dateChange.emit(new MatDatepickerInputEvent(this, this._elementRef.nativeElement));
}
if (this._outsideValueChanged) {
this._outsideValueChanged();
}
}
});
}
/** Opens the popup associated with the input. */
protected abstract _openPopup(): void;
/** Assigns a value to the input's model. */
protected abstract _assignValueToModel(model: D | null): void;
/** Converts a value from the model into a native value for the input. */
protected abstract _getValueFromModel(modelValue: S): D | null;
/** Combined form control validator for this input. */
protected abstract _validator: ValidatorFn | null;
/**
* Callback that'll be invoked when the selection model is changed
* from somewhere that's not the current datepicker input.
*/
protected abstract _outsideValueChanged?: () => void;
/** Predicate that determines whether we're allowed to emit a particular change event. */
protected abstract _canEmitChangeEvent(event: DateSelectionModelChange<S>): boolean;
/** Whether the last value set on the input was valid. */
protected _lastValueValid = false;
constructor(
protected _elementRef: ElementRef<HTMLInputElement>,
@Optional() public _dateAdapter: DateAdapter<D>,
@Optional() @Inject(MAT_DATE_FORMATS) private _dateFormats: MatDateFormats) {
if (!this._dateAdapter) {
throw createMissingDateImplError('DateAdapter');
}
if (!this._dateFormats) {
throw createMissingDateImplError('MAT_DATE_FORMATS');
}
// Update the displayed date when the locale changes.
this._localeSubscription = _dateAdapter.localeChanges.subscribe(() => {
this.value = this.value;
});
}
ngAfterViewInit() {
this._isInitialized = true;
}
ngOnChanges() {
this._stateChanges.next(undefined);
}
ngOnDestroy() {
this._valueChangesSubscription.unsubscribe();
this._localeSubscription.unsubscribe();
this._valueChange.complete();
this._stateChanges.complete();
}
/** @docs-private */
registerOnValidatorChange(fn: () => void): void {
this._validatorOnChange = fn;
}
/** @docs-private */
validate(c: AbstractControl): ValidationErrors | null {
return this._validator ? this._validator(c) : null;
}
// Implemented as part of ControlValueAccessor.
writeValue(value: D): void {
this.value = value;
}
// Implemented as part of ControlValueAccessor.
registerOnChange(fn: (value: any) => void): void {
this._cvaOnChange = fn;
}
// Implemented as part of ControlValueAccessor.
registerOnTouched(fn: () => void): void {
this._onTouched = fn;
}
// Implemented as part of ControlValueAccessor.
setDisabledState(isDisabled: boolean): void {
this.disabled = isDisabled;
}
_onKeydown(event: KeyboardEvent) {
const isAltDownArrow = event.altKey && event.keyCode === DOWN_ARROW;
if (isAltDownArrow && !this._elementRef.nativeElement.readOnly) {
this._openPopup();
event.preventDefault();
}
}
_onInput(value: string) {
const lastValueWasValid = this._lastValueValid;
let date = this._dateAdapter.parse(value, this._dateFormats.parse.dateInput);
this._lastValueValid = this._isValidValue(date);
date = this._dateAdapter.getValidDateOrNull(date);
if (!this._dateAdapter.sameDate(date, this.value)) {
this._assignValue(date);
this._cvaOnChange(date);
this._valueChange.emit(date);
this.dateInput.emit(new MatDatepickerInputEvent(this, this._elementRef.nativeElement));
} else {
// Call the CVA change handler for invalid values
// since this is what marks the control as dirty.
if (value && !this.value) {
this._cvaOnChange(date);
}
if (lastValueWasValid !== this._lastValueValid) {
this._validatorOnChange();
}
}
}
_onChange() {
this.dateChange.emit(new MatDatepickerInputEvent(this, this._elementRef.nativeElement));
}
/** Handles blur events on the input. */
_onBlur() {
// Reformat the input only if we have a valid value.
if (this.value) {
this._formatValue(this.value);
}
this._onTouched();
}
/** Formats a value and sets it on the input element. */
protected _formatValue(value: D | null) {
this._elementRef.nativeElement.value =
value ? this._dateAdapter.format(value, this._dateFormats.display.dateInput) : '';
}
/** Assigns a value to the model. */
private _assignValue(value: D | null) {
// We may get some incoming values before the model was
// assigned. Save the value so that we can assign it later.
if (this._model) {
this._assignValueToModel(value);
this._pendingValue = null;
} else {
this._pendingValue = value;
}
}
/** Whether a value is considered valid. */
private _isValidValue(value: D | null): boolean {
return !value || this._dateAdapter.isValid(value);
}
/**
* Checks whether a parent control is disabled. This is in place so that it can be overridden
* by inputs extending this one which can be placed inside of a group that can be disabled.
*/
protected _parentDisabled() {
return false;
}
// Accept `any` to avoid conflicts with other directives on `<input>` that
// may accept different types.
static ngAcceptInputType_value: any;
static ngAcceptInputType_disabled: BooleanInput;
}