|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {Directive, ElementRef, Optional, Self, InjectionToken, Inject} from '@angular/core'; |
| 10 | +import { |
| 11 | + NG_VALUE_ACCESSOR, |
| 12 | + NG_VALIDATORS, |
| 13 | + ControlValueAccessor, |
| 14 | + Validator, |
| 15 | + AbstractControl, |
| 16 | + ValidationErrors, |
| 17 | + NgForm, |
| 18 | + FormGroupDirective, |
| 19 | + NgControl, |
| 20 | +} from '@angular/forms'; |
| 21 | +import { |
| 22 | + CanUpdateErrorState, |
| 23 | + CanDisable, |
| 24 | + ErrorStateMatcher, |
| 25 | + CanDisableCtor, |
| 26 | + CanUpdateErrorStateCtor, |
| 27 | + mixinErrorState, |
| 28 | + mixinDisabled, |
| 29 | +} from '@angular/material/core'; |
| 30 | +import {BooleanInput} from '@angular/cdk/coercion'; |
| 31 | + |
| 32 | +/** Parent component that should be wrapped around `MatStartDate` and `MatEndDate`. */ |
| 33 | +export interface MatDateRangeInputParent { |
| 34 | + id: string; |
| 35 | + _ariaDescribedBy: string | null; |
| 36 | + _ariaLabelledBy: string | null; |
| 37 | + _handleChildValueChange: () => void; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Used to provide the date range input wrapper component |
| 42 | + * to the parts without circular dependencies. |
| 43 | + */ |
| 44 | +export const MAT_DATE_RANGE_INPUT_PARENT = |
| 45 | + new InjectionToken<MatDateRangeInputParent>('MAT_DATE_RANGE_INPUT_PARENT'); |
| 46 | + |
| 47 | +// Boilerplate for applying mixins to MatDateRangeInput. |
| 48 | +/** @docs-private */ |
| 49 | +class MatDateRangeInputPartMixinBase { |
| 50 | + constructor(public _defaultErrorStateMatcher: ErrorStateMatcher, |
| 51 | + public _parentForm: NgForm, |
| 52 | + public _parentFormGroup: FormGroupDirective, |
| 53 | + /** @docs-private */ |
| 54 | + public ngControl: NgControl) {} |
| 55 | +} |
| 56 | +const _MatDateRangeInputMixinBase: CanDisableCtor & |
| 57 | + CanUpdateErrorStateCtor & typeof MatDateRangeInputPartMixinBase = |
| 58 | + mixinErrorState(mixinDisabled(MatDateRangeInputPartMixinBase)); |
| 59 | + |
| 60 | +@Directive() |
| 61 | +abstract class MatDateRangeInputPartBase<D> extends _MatDateRangeInputMixinBase implements |
| 62 | + ControlValueAccessor, Validator, CanUpdateErrorState, CanDisable, CanUpdateErrorState { |
| 63 | + |
| 64 | + private _onTouched = () => {}; |
| 65 | + |
| 66 | + constructor( |
| 67 | + protected _elementRef: ElementRef<HTMLInputElement>, |
| 68 | + @Inject(MAT_DATE_RANGE_INPUT_PARENT) public _rangeInput: MatDateRangeInputParent, |
| 69 | + defaultErrorStateMatcher: ErrorStateMatcher, |
| 70 | + @Optional() parentForm: NgForm, |
| 71 | + @Optional() parentFormGroup: FormGroupDirective, |
| 72 | + @Optional() @Self() ngControl: NgControl) { |
| 73 | + super(defaultErrorStateMatcher, parentForm, parentFormGroup, ngControl); |
| 74 | + } |
| 75 | + |
| 76 | + /** @docs-private */ |
| 77 | + writeValue(_value: D | null): void { |
| 78 | + // TODO(crisbeto): implement |
| 79 | + } |
| 80 | + |
| 81 | + /** @docs-private */ |
| 82 | + registerOnChange(_fn: () => void): void { |
| 83 | + // TODO(crisbeto): implement |
| 84 | + } |
| 85 | + |
| 86 | + /** @docs-private */ |
| 87 | + registerOnTouched(fn: () => void): void { |
| 88 | + this._onTouched = fn; |
| 89 | + } |
| 90 | + |
| 91 | + /** @docs-private */ |
| 92 | + setDisabledState(isDisabled: boolean): void { |
| 93 | + this.disabled = isDisabled; |
| 94 | + } |
| 95 | + |
| 96 | + /** @docs-private */ |
| 97 | + validate(_control: AbstractControl): ValidationErrors | null { |
| 98 | + // TODO(crisbeto): implement |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + /** @docs-private */ |
| 103 | + registerOnValidatorChange(_fn: () => void): void { |
| 104 | + // TODO(crisbeto): implement |
| 105 | + } |
| 106 | + |
| 107 | + isEmpty(): boolean { |
| 108 | + // TODO(crisbeto): should look at the CVA value. |
| 109 | + return this._elementRef.nativeElement.value.length === 0; |
| 110 | + } |
| 111 | + |
| 112 | + focus(): void { |
| 113 | + this._elementRef.nativeElement.focus(); |
| 114 | + } |
| 115 | + |
| 116 | + _handleBlur() { |
| 117 | + this._onTouched(); |
| 118 | + } |
| 119 | + |
| 120 | + static ngAcceptInputType_disabled: BooleanInput; |
| 121 | +} |
| 122 | + |
| 123 | + |
| 124 | +/** Input for entering the start date in a `mat-date-range-input`. */ |
| 125 | +@Directive({ |
| 126 | + selector: 'input[matStartDate]', |
| 127 | + inputs: ['disabled'], |
| 128 | + host: { |
| 129 | + '[id]': '_rangeInput.id', |
| 130 | + '[attr.aria-labelledby]': '_rangeInput._ariaLabelledBy', |
| 131 | + '[attr.aria-describedby]': '_rangeInput._ariaDescribedBy', |
| 132 | + 'class': 'mat-date-range-input-inner', |
| 133 | + 'type': 'text', |
| 134 | + '(blur)': '_handleBlur()', |
| 135 | + '(input)': '_rangeInput._handleChildValueChange()' |
| 136 | + }, |
| 137 | + providers: [ |
| 138 | + {provide: NG_VALUE_ACCESSOR, useExisting: MatStartDate, multi: true}, |
| 139 | + {provide: NG_VALIDATORS, useExisting: MatStartDate, multi: true} |
| 140 | + ] |
| 141 | +}) |
| 142 | +export class MatStartDate<D> extends MatDateRangeInputPartBase<D> { |
| 143 | + getMirrorValue(): string { |
| 144 | + const element = this._elementRef.nativeElement; |
| 145 | + const value = element.value; |
| 146 | + return value.length > 0 ? value : element.placeholder; |
| 147 | + } |
| 148 | + |
| 149 | + static ngAcceptInputType_disabled: BooleanInput; |
| 150 | +} |
| 151 | + |
| 152 | + |
| 153 | +/** Input for entering the end date in a `mat-date-range-input`. */ |
| 154 | +@Directive({ |
| 155 | + selector: 'input[matEndDate]', |
| 156 | + inputs: ['disabled'], |
| 157 | + host: { |
| 158 | + 'class': 'mat-date-range-input-inner', |
| 159 | + '[attr.aria-labelledby]': '_rangeInput._ariaLabelledBy', |
| 160 | + '[attr.aria-describedby]': '_rangeInput._ariaDescribedBy', |
| 161 | + 'type': 'text', |
| 162 | + '(blur)': '_handleBlur()' |
| 163 | + }, |
| 164 | + providers: [ |
| 165 | + {provide: NG_VALUE_ACCESSOR, useExisting: MatEndDate, multi: true}, |
| 166 | + {provide: NG_VALIDATORS, useExisting: MatEndDate, multi: true} |
| 167 | + ] |
| 168 | +}) |
| 169 | +export class MatEndDate<D> extends MatDateRangeInputPartBase<D> { |
| 170 | + static ngAcceptInputType_disabled: BooleanInput; |
| 171 | +} |
0 commit comments