Skip to content

Commit 5f4d7e2

Browse files
authored
fix(material/datepicker): add ability to have numeric zero value in input (#24813)
* fix(material/datepicker): add ability to have numeric zero value in input Shows valid formatted value for 'falsy' values (exp 0) Custom DateAdapter (exp TimestampDateAdapter) can have valid numeric 0 value (for timestamp it is 1/1/1970), but datepicker input doesn't show formatted value * fix(material/datepicker): add ability to have numeric zero value in input Add tests
1 parent a47869c commit 5f4d7e2

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/material/datepicker/datepicker-input-base.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,8 @@ export abstract class MatDatepickerInputBase<S, D = ExtractDateTypeFromSelection
344344

345345
/** Formats a value and sets it on the input element. */
346346
protected _formatValue(value: D | null) {
347-
this._elementRef.nativeElement.value = value
348-
? this._dateAdapter.format(value, this._dateFormats.display.dateInput)
349-
: '';
347+
this._elementRef.nativeElement.value =
348+
value != null ? this._dateAdapter.format(value, this._dateFormats.display.dateInput) : '';
350349
}
351350

352351
/** Assigns a value to the model. */

src/material/datepicker/testing/datepicker-input-harness-shared.spec.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {HarnessLoader, parallel} from '@angular/cdk/testing';
22
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
33
import {Component} from '@angular/core';
4-
import {MatNativeDateModule} from '@angular/material/core';
4+
import {DateAdapter, MatNativeDateModule} from '@angular/material/core';
55
import {ComponentFixture, TestBed} from '@angular/core/testing';
66
import {FormsModule} from '@angular/forms';
77
import {MatDatepickerModule} from '@angular/material/datepicker';
@@ -86,6 +86,31 @@ export function runDatepickerInputHarnessTests(
8686
expect(await input.getValue()).toBe('1/1/2020');
8787
});
8888

89+
it('should set the input value based on date adapter validation and formatting', async () => {
90+
const adapter = fixture.debugElement.injector.get(DateAdapter);
91+
const input = await loader.getHarness(datepickerInputHarness.with({selector: '#basic'}));
92+
const validValues: any[] = [new Date(0), '', 0, false];
93+
const invalidValues: any[] = [null, undefined];
94+
spyOn(adapter, 'format').and.returnValue('FORMATTED_VALUE');
95+
spyOn(adapter, 'isValid').and.callFake(value => validValues.includes(value));
96+
spyOn(adapter, 'deserialize').and.callFake(value =>
97+
validValues.includes(value) ? value : null,
98+
);
99+
spyOn(adapter, 'getValidDateOrNull').and.callFake(value =>
100+
adapter.isValid(value) ? value : null,
101+
);
102+
103+
for (let value of validValues) {
104+
fixture.componentInstance.date = value;
105+
expect(await input.getValue()).toBe('FORMATTED_VALUE');
106+
}
107+
108+
for (let value of invalidValues) {
109+
fixture.componentInstance.date = value;
110+
expect(await input.getValue()).toBe('');
111+
}
112+
});
113+
89114
it('should get the input placeholder', async () => {
90115
const inputs = await loader.getAllHarnesses(datepickerInputHarness);
91116
expect(

0 commit comments

Comments
 (0)