Skip to content

Commit

Permalink
fix(datepicker): make it valid in case of empty input (#1247)
Browse files Browse the repository at this point in the history
Closes #1182
  • Loading branch information
tibing-old-email authored and nnixaa committed Feb 21, 2019
1 parent 96018b1 commit 799b8b8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,14 @@ export class NbDatepickerDirective<D> implements OnDestroy, ControlValueAccessor
* Validates that we can parse value correctly.
* */
protected parseValidator(): ValidationErrors | null {
/**
* Date services treat empty string as invalid date.
* That's why we're getting invalid formControl in case of empty input which is not required.
* */
if (this.inputValue === '') {
return null;
}

const isValid = this.datepickerAdapter.isValid(this.inputValue, this.picker.format);
return isValid ? null : { nbDatepickerParse: { value: this.inputValue } };
}
Expand Down
16 changes: 16 additions & 0 deletions src/framework/theme/components/datepicker/datepicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { ApplicationRef, Component, ViewChild } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { NbDatepickerDirective } from '@nebular/theme';

import { NbDatepickerModule } from './datepicker.module';
import { NbThemeModule } from '../../theme.module';
Expand All @@ -27,12 +28,14 @@ import { NbDatepickerComponent } from './datepicker.component';
})
export class NbDatepickerTestComponent {
@ViewChild(NbDatepickerComponent) datepicker: NbDatepickerComponent<Date>;
@ViewChild(NbDatepickerDirective) datepickerDirective: NbDatepickerDirective<Date>;
}

describe('nb-datepicker', () => {
let fixture: ComponentFixture<NbDatepickerTestComponent>;
let appRef: ApplicationRef;
let datepicker: NbDatepickerComponent<Date>;
let datepickerDirective: NbDatepickerDirective<Date>;
let overlay: HTMLElement;
let input: HTMLInputElement;

Expand Down Expand Up @@ -60,6 +63,7 @@ describe('nb-datepicker', () => {

beforeEach(() => {
datepicker = fixture.componentInstance.datepicker;
datepickerDirective = fixture.componentInstance.datepickerDirective;
overlay = fixture.nativeElement.querySelector('nb-layout');
input = fixture.nativeElement.querySelector('input');
});
Expand Down Expand Up @@ -108,4 +112,16 @@ describe('nb-datepicker', () => {

expect(cell.textContent).toContain('17');
});

it('should be valid if empty', () => {
expect(datepickerDirective.validate()).toBe(null);
});

it('should not be valid if empty contain incorrectly formatted date', () => {
input.value = 'somecurruptedinput';
input.dispatchEvent(new Event('input'));
showDatepicker();

expect(datepickerDirective.validate()).not.toBe(null);
});
});

0 comments on commit 799b8b8

Please sign in to comment.