Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
Fix datepicker input clear (#1729) (#1730)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackbaud-conorwright authored and Blackbaud-SteveBrush committed Jun 11, 2018
1 parent e333e82 commit 380bdae
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/demos/datepicker/datepicker-demo.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,9 @@
</div>

<div>
Selected date is {{selectedDate}}
Selected date is {{ selectedDate }}
</div>

<p>
<button class="sky-btn sky-btn-secondary" (click)="clearSelectedDate()">Clear selected date</button>
</p>
4 changes: 4 additions & 0 deletions src/demos/datepicker/datepicker-demo.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ export class SkyDatepickerDemoComponent {
public minDate: Date;
public maxDate: Date;
public selectedDate: Date;

public clearSelectedDate(): void {
this.selectedDate = undefined;
}
}
10 changes: 6 additions & 4 deletions src/modules/datepicker/datepicker-input.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,19 @@ export class SkyDatepickerInputDirective implements
public registerOnValidatorChange(fn: () => void): void { this._validatorChange = fn; }

public writeValue(value: any) {

if (value && this.dateFormatter.dateIsValid(value)) {
if (this.dateFormatter.dateIsValid(value)) {
this.modelValue = value;
} else if (value) {
this.modelValue = this.dateFormatter.getDateFromString(value, this.dateFormat);
if (value !== this.modelValue && this.dateFormatter.dateIsValid(this.modelValue)) {
this._onChange(this.modelValue);
}
} else {
this.modelValue = value;
this._onChange(this.modelValue);
}

if (this.dateFormatter.dateIsValid(this.modelValue)) {
if (this.dateFormatter.dateIsValid(this.modelValue) || !this.modelValue) {
this.writeModelValue(this.modelValue);
} else if (value) {
this.renderer.setElementProperty(
Expand Down Expand Up @@ -213,7 +215,7 @@ export class SkyDatepickerInputDirective implements
this.renderer.setElementProperty(
this.elRef.nativeElement,
'value',
this.dateFormatter.format(model, this.dateFormat));
model ? this.dateFormatter.format(model, this.dateFormat) : '');

this.skyDatepickerInput.setSelectedDate(model);
}
Expand Down
33 changes: 31 additions & 2 deletions src/modules/datepicker/datepicker.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ describe('datepicker', () => {
nativeElement = fixture.nativeElement as HTMLElement;
component = fixture.componentInstance;

fixture.detectChanges();
tick();
fixture.detectChanges();

setInput(nativeElement, '5/12/2017', fixture);
Expand Down Expand Up @@ -284,16 +286,19 @@ describe('datepicker', () => {
});

describe('input change', () => {

it('should handle input change with a string with the expected format', fakeAsync(() => {
fixture.detectChanges();
tick();
fixture.detectChanges();

setInput(nativeElement, '5/12/2017', fixture);
expect(nativeElement.querySelector('input').value).toBe('05/12/2017');
expect(component.selectedDate).toEqual(new Date('5/12/2017'));

}));

it('should handle input change with a ISO string', fakeAsync(() => {
fixture.detectChanges();
tick();
fixture.detectChanges();
setInput(nativeElement, '2009-06-15T00:00:01', fixture);
expect(nativeElement.querySelector('input').value).toBe('06/15/2009');
Expand All @@ -302,6 +307,8 @@ describe('datepicker', () => {
}));

it('should handle input change with an ISO string with offset', fakeAsync(() => {
fixture.detectChanges();
tick();
fixture.detectChanges();
setInput(nativeElement, '1994-11-05T08:15:30-05:00', fixture);

Expand All @@ -312,6 +319,8 @@ describe('datepicker', () => {
}));

it('should handle two digit years', fakeAsync(() => {
fixture.detectChanges();
tick();
fixture.detectChanges();
setInput(nativeElement, '5/12/98', fixture);

Expand All @@ -320,7 +329,25 @@ describe('datepicker', () => {
expect(component.selectedDate).toEqual(new Date('05/12/1998'));
}));

it('should handle undefined date', fakeAsync(() => {
component.selectedDate = '5/12/17';

fixture.detectChanges();
tick();
fixture.detectChanges();

component.selectedDate = undefined;
fixture.detectChanges();
tick();
fixture.detectChanges();

expect(nativeElement.querySelector('input').value).toBe('');
expect(nativeElement.querySelector('input')).not.toHaveCssClass('ng-invalid');
}));

it('should pass date to calendar', fakeAsync(() => {
fixture.detectChanges();
tick();
fixture.detectChanges();
setInput(nativeElement, '5/12/2017', fixture);

Expand All @@ -341,6 +368,8 @@ describe('datepicker', () => {
it('should handle a dateFormat on the input different than the default', fakeAsync(() => {
component.format = 'DD/MM/YYYY';
fixture.detectChanges();
tick();
fixture.detectChanges();

setInput(nativeElement, '5/12/2017', fixture);

Expand Down

0 comments on commit 380bdae

Please sign in to comment.