Skip to content

Commit

Permalink
refactor(datepicker): inherit 'NgbInputDatepickerConfig' from 'NgbDat…
Browse files Browse the repository at this point in the history
…epickerConfig'
  • Loading branch information
maxokorokov committed Nov 15, 2019
1 parent 3134439 commit fbd6e8d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
@@ -1,6 +1,5 @@
import {Component} from '@angular/core';
import {
NgbDatepickerConfig,
NgbCalendar,
NgbDate,
NgbDateStruct,
Expand All @@ -10,28 +9,27 @@ import {
@Component({
selector: 'ngbd-datepicker-config',
templateUrl: './datepicker-config.html',
providers: [NgbDatepickerConfig] // add NgbDatepickerConfig to the component providers
providers: [NgbInputDatepickerConfig] // add config to the component providers
})
export class NgbdDatepickerConfig {

model: NgbDateStruct;

constructor(
datepickerConfig: NgbDatepickerConfig, inputDatepickerConfig: NgbInputDatepickerConfig, calendar: NgbCalendar) {
constructor(config: NgbInputDatepickerConfig, calendar: NgbCalendar) {
// customize default values of datepickers used by this component tree
datepickerConfig.minDate = {year: 1900, month: 1, day: 1};
datepickerConfig.maxDate = {year: 2099, month: 12, day: 31};
config.minDate = {year: 1900, month: 1, day: 1};
config.maxDate = {year: 2099, month: 12, day: 31};

// days that don't belong to current month are not visible
datepickerConfig.outsideDays = 'hidden';
config.outsideDays = 'hidden';

// weekends are disabled
datepickerConfig.markDisabled = (date: NgbDate) => calendar.getWeekday(date) >= 6;
config.markDisabled = (date: NgbDate) => calendar.getWeekday(date) >= 6;

// setting datepicker popup to close only on click outside
inputDatepickerConfig.autoClose = 'outside';
config.autoClose = 'outside';

// setting datepicker popup to open above the input
inputDatepickerConfig.placement = ['top-left', 'top-right'];
config.placement = ['top-left', 'top-right'];
}
}
3 changes: 2 additions & 1 deletion src/datepicker/datepicker-input-config.ts
@@ -1,5 +1,6 @@
import {Injectable} from '@angular/core';

import {NgbDatepickerConfig} from './datepicker-config';
import {PlacementArray} from '../util/positioning';

/**
Expand All @@ -9,7 +10,7 @@ import {PlacementArray} from '../util/positioning';
* order to provide default values for all the datepicker inputs used in the application.
*/
@Injectable({providedIn: 'root'})
export class NgbInputDatepickerConfig {
export class NgbInputDatepickerConfig extends NgbDatepickerConfig {
autoClose: boolean | 'inside' | 'outside' = true;
container: null | 'body';
positionTarget: string | HTMLElement;
Expand Down
6 changes: 5 additions & 1 deletion src/datepicker/datepicker-input.ts
Expand Up @@ -33,6 +33,7 @@ import {NgbDate} from './ngb-date';
import {NgbDateParserFormatter} from './ngb-date-parser-formatter';
import {NgbDateStruct} from './ngb-date-struct';
import {NgbInputDatepickerConfig} from './datepicker-input-config';
import {NgbDatepickerConfig} from './datepicker-config';

const NGB_DATEPICKER_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
Expand Down Expand Up @@ -61,7 +62,10 @@ const NGB_DATEPICKER_VALIDATOR = {
'(blur)': 'onBlur()',
'[disabled]': 'disabled'
},
providers: [NGB_DATEPICKER_VALUE_ACCESSOR, NGB_DATEPICKER_VALIDATOR, NgbDatepickerService]
providers: [
NGB_DATEPICKER_VALUE_ACCESSOR, NGB_DATEPICKER_VALIDATOR, NgbDatepickerService,
{provide: NgbDatepickerConfig, useExisting: NgbInputDatepickerConfig}
],
})
export class NgbInputDatepicker implements OnChanges,
OnDestroy, ControlValueAccessor, Validator {
Expand Down

0 comments on commit fbd6e8d

Please sign in to comment.