Skip to content

fix(material/datepicker): add ability to have numeric zero value in input #24813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/material/datepicker/datepicker-input-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,8 @@ export abstract class MatDatepickerInputBase<S, D = ExtractDateTypeFromSelection

/** 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)
: '';
this._elementRef.nativeElement.value =
value != null ? this._dateAdapter.format(value, this._dateFormats.display.dateInput) : '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't think also try to format other falsy values like an empty string?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it's already there. '!= null' checks only for 'null' and 'undefined'. 0, "", false, etc. will be formatted

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @crisbeto is saying that maybe '' should be treated like null. I think it is technically correct to try to parse it though. We don't know what the generic type D will be, but it could be string. If its not string people shouldn't really be passing '' anyways. I think we can keep it like this and try to presubmit it in Google. If we find a lot of people using '' to mean null we may want to change it

}

/** Assigns a value to the model. */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {HarnessLoader, parallel} from '@angular/cdk/testing';
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
import {Component} from '@angular/core';
import {MatNativeDateModule} from '@angular/material/core';
import {DateAdapter, MatNativeDateModule} from '@angular/material/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {FormsModule} from '@angular/forms';
import {MatDatepickerModule} from '@angular/material/datepicker';
Expand Down Expand Up @@ -86,6 +86,31 @@ export function runDatepickerInputHarnessTests(
expect(await input.getValue()).toBe('1/1/2020');
});

it('should set the input value based on date adapter validation and formatting', async () => {
const adapter = fixture.debugElement.injector.get(DateAdapter);
const input = await loader.getHarness(datepickerInputHarness.with({selector: '#basic'}));
const validValues: any[] = [new Date(0), '', 0, false];
const invalidValues: any[] = [null, undefined];
spyOn(adapter, 'format').and.returnValue('FORMATTED_VALUE');
spyOn(adapter, 'isValid').and.callFake(value => validValues.includes(value));
spyOn(adapter, 'deserialize').and.callFake(value =>
validValues.includes(value) ? value : null,
);
spyOn(adapter, 'getValidDateOrNull').and.callFake(value =>
adapter.isValid(value) ? value : null,
);

for (let value of validValues) {
fixture.componentInstance.date = value;
expect(await input.getValue()).toBe('FORMATTED_VALUE');
}

for (let value of invalidValues) {
fixture.componentInstance.date = value;
expect(await input.getValue()).toBe('');
}
});

it('should get the input placeholder', async () => {
const inputs = await loader.getAllHarnesses(datepickerInputHarness);
expect(
Expand Down