Skip to content
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

fix(datepicker): make it valid in case of empty input #1247

Merged
merged 6 commits into from
Feb 21, 2019
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
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);
});
});