Skip to content

Commit

Permalink
feat(datepicker): throw error when setting format for native date (#986)
Browse files Browse the repository at this point in the history
  • Loading branch information
yggg authored and tibing-old-email committed Nov 15, 2018
1 parent 5d5a874 commit 3e9df5d
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/framework/theme/components/datepicker/datepicker.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Component,
ComponentFactoryResolver,
ComponentRef,
OnChanges,
ElementRef,
EventEmitter,
Inject,
Expand Down Expand Up @@ -36,16 +37,23 @@ import { NbDatepickerContainerComponent } from './datepicker-container.component
import { NB_DOCUMENT } from '../../theme.options';
import { NbCalendarRange, NbCalendarRangeComponent } from '../calendar/calendar-range.component'
import { NbCalendarComponent } from '../calendar/calendar.component';
import { NbCalendarCell, NbCalendarSize, NbCalendarViewMode } from '../calendar-kit';
import {
NbCalendarCell,
NbCalendarSize,
NbCalendarViewMode,
NbDateService,
NbNativeDateService,
} from '../calendar-kit';
import { NbDatepicker, NbPickerValidatorConfig } from './datepicker.directive';


/**
* The `NbBasePicker` component concentrates overlay manipulation logic.
* */
export abstract class NbBasePicker<D, T, P> extends NbDatepicker<T> implements OnDestroy {
export abstract class NbBasePicker<D, T, P> extends NbDatepicker<T> implements OnChanges, OnDestroy {
/**
* Datepicker date format.
* Datepicker date format. Can be used only with date adapters (moment, date-fns) since native date
* object doesn't support formatting.
* */
@Input() format: string;

Expand Down Expand Up @@ -156,7 +164,9 @@ export abstract class NbBasePicker<D, T, P> extends NbDatepicker<T> implements O
constructor(@Inject(NB_DOCUMENT) protected document,
protected positionBuilder: NbPositionBuilderService,
protected overlay: NbOverlayService,
protected cfr: ComponentFactoryResolver) {
protected cfr: ComponentFactoryResolver,
protected dateService: NbDateService<D>,
) {
super();
}

Expand Down Expand Up @@ -187,6 +197,15 @@ export abstract class NbBasePicker<D, T, P> extends NbDatepicker<T> implements O

protected abstract get pickerValueChange(): Observable<T>;

ngOnChanges() {
if (this.dateService instanceof NbNativeDateService && this.format) {
throw new Error('Can\'t format native date. To use custom formatting you have to install @nebular/moment or ' +
'@nebular/date-fns package and import NbMomentDateModule or NbDateFnsDateModule accordingly.' +
'More information at "Formatting issue" ' +
'https://akveo.github.io/nebular/docs/components/datepicker/overview#nbdatepickercomponent');
}
}

ngOnDestroy() {
this.alive = false;
this.hide();
Expand Down

0 comments on commit 3e9df5d

Please sign in to comment.