Skip to content

Commit

Permalink
feat(datepicker): Add directive for inline datepicker
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Troedsson committed Mar 8, 2018
1 parent 65d0475 commit afcf540
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ export const demoComponentContent: ContentSection[] = [
anchor: 'bs-daterangepicker',
outlet: NgApiDocComponent
},
{
title: 'BsDatepickerInlineDirective',
anchor: 'bs-datepicker-inline',
outlet: NgApiDocComponent
},
{
title: 'BsDatepickerConfig',
anchor: 'bs-datepicker-config',
Expand Down
6 changes: 6 additions & 0 deletions demo/src/app/components/+datepicker/demos/basic/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@
</div>
</div>

<div class="row">
<div class="col-xs-12 col-12 col-md-4 form-group">
<p>Inline datepicker:</p>
<bs-datepicker-inline></bs-datepicker-inline>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@
<button class="btn btn-success" (click)="drp.toggle()">Date Range Picker</button>
</div>
</div>

<p>bsInlineValue property sets initial state in this example</p>
<div class="row">
<div class="col-xs-12 col-12 col-md-4 form-group">
<bs-datepicker-inline [bsValue]="bsInlineValue"></bs-datepicker-inline>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Component } from '@angular/core';
export class DemoDatepickerDateInitialStateComponent {
bsValue = new Date();
bsRangeValue: Date[];
bsInlineValue = new Date();
maxDate = new Date();
constructor() {
this.maxDate.setDate(this.maxDate.getDate() + 7);
Expand Down
77 changes: 77 additions & 0 deletions demo/src/ng-api-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,62 @@ export const ngdoc: any = {
"methods": [],
"properties": []
},
"BsDatepickerInlineDirective": {
"fileName": "src/datepicker/bs-datepicker-inline.component.ts",
"className": "BsDatepickerInlineDirective",
"description": "",
"selector": "bs-datepicker-inline",
"exportAs": "bsDatepickerInline",
"inputs": [
{
"name": "bsConfig",
"type": "Partial<BsDatepickerInlineConfig>",
"description": "<p>Config object for datepicker</p>\n"
},
{
"name": "bsValue",
"type": "Date",
"description": "<p>Initial value of datepicker</p>\n"
},
{
"name": "isDisabled",
"type": "boolean",
"description": "<p>Indicates whether datepicker is enabled or not</p>\n"
},
{
"name": "maxDate",
"type": "Date",
"description": "<p>Maximum date which is available for selection</p>\n"
},
{
"name": "minDate",
"type": "Date",
"description": "<p>Minimum date which is available for selection</p>\n"
}
],
"outputs": [
{
"name": "bsValueChange",
"description": "<p>Emits when datepicker value has been changed</p>\n"
}
],
"properties": [],
"methods": [
{
"name": "setConfig",
"description": "<p>Set config for datepicker</p>\n",
"args": [],
"returnType": "void"
}
]
},
"BsDatepickerInlineConfig": {
"fileName": "src/datepicker/bs-datepicker-inline.config.ts",
"className": "BsDatepickerInlineConfig",
"description": "",
"methods": [],
"properties": []
},
"BsDatepickerInputDirective": {
"fileName": "src/datepicker/bs-datepicker-input.directive.ts",
"className": "BsDatepickerInputDirective",
Expand Down Expand Up @@ -1500,6 +1556,16 @@ export const ngdoc: any = {
"properties": [],
"methods": []
},
"BsDatepickerInlineContainerComponent": {
"fileName": "src/datepicker/themes/bs/bs-datepicker-inline-container.component.ts",
"className": "BsDatepickerInlineContainerComponent",
"description": "",
"selector": "bs-datepicker-inline-container",
"inputs": [],
"outputs": [],
"properties": [],
"methods": []
},
"BsDatepickerNavigationViewComponent": {
"fileName": "src/datepicker/themes/bs/bs-datepicker-navigation-view.component.ts",
"className": "BsDatepickerNavigationViewComponent",
Expand Down Expand Up @@ -2747,6 +2813,11 @@ export const ngdoc: any = {
"type": "boolean",
"description": "<p>if true up/down arrowkeys inside hours and minutes inputs will change time </p>\n"
},
{
"name": "disabled",
"type": "boolean",
"description": "<p>if true hours and minutes fields will be disabled </p>\n"
},
{
"name": "hourStep",
"type": "number",
Expand Down Expand Up @@ -2829,6 +2900,12 @@ export const ngdoc: any = {
"type": "boolean",
"description": "<p>if true up/down arrowkeys inside hours and minutes inputs will change time </p>\n"
},
{
"name": "disabled",
"defaultValue": "false",
"type": "boolean",
"description": "<p>if true hours and minutes fields will be disabled </p>\n"
},
{
"name": "hourStep",
"defaultValue": "1",
Expand Down
2 changes: 1 addition & 1 deletion src/component-loader/component-loader.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class ComponentLoader<T> {
}

// todo: add behaviour: to target element, `body`, custom element
to(container?: string): ComponentLoader<T> {
to(container?: string | ElementRef): ComponentLoader<T> {
this.container = container || this.container;

return this;
Expand Down
129 changes: 129 additions & 0 deletions src/datepicker/bs-datepicker-inline.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import {
ComponentRef, Directive, ElementRef, EventEmitter, Input, OnChanges,
OnDestroy, OnInit, Output, Renderer2, SimpleChanges, ViewContainerRef
} from '@angular/core';
import { ComponentLoader } from '../component-loader/component-loader.class';
import { ComponentLoaderFactory } from '../component-loader/component-loader.factory';
import { BsDatepickerInlineContainerComponent } from './themes/bs/bs-datepicker-inline-container.component';
import { Subscription } from 'rxjs/Subscription';
import 'rxjs/add/operator/filter';
import { BsDatepickerInlineConfig } from './bs-datepicker-inline.config';
import { BsLocaleService } from './bs-locale.service';
import { BsDatepickerConfig } from './bs-datepicker.config';

@Directive({
selector: 'bs-datepicker-inline',
exportAs: 'bsDatepickerInline'
})
export class BsDatepickerInlineDirective implements OnInit, OnDestroy, OnChanges {
_bsValue: Date;
/**
* Initial value of datepicker
*/
@Input()
set bsValue(value: Date) {
if (this._bsValue === value) {
return;
}
this._bsValue = value;
this.bsValueChange.emit(value);
}

/**
* Config object for datepicker
*/
@Input() bsConfig: Partial<BsDatepickerInlineConfig>;
/**
* Indicates whether datepicker is enabled or not
*/
@Input() isDisabled: boolean;
/**
* Minimum date which is available for selection
*/
@Input() minDate: Date;
/**
* Maximum date which is available for selection
*/
@Input() maxDate: Date;
/**
* Emits when datepicker value has been changed
*/
@Output() bsValueChange: EventEmitter<Date> = new EventEmitter();

protected _subs: Subscription[] = [];

private _datepicker: ComponentLoader<BsDatepickerInlineContainerComponent>;
private _datepickerRef: ComponentRef<BsDatepickerInlineContainerComponent>;

constructor(public _config: BsDatepickerInlineConfig,
private _elementRef: ElementRef,
_renderer: Renderer2,
_viewContainerRef: ViewContainerRef,
cis: ComponentLoaderFactory) {
// todo: assign only subset of fields
Object.assign(this, this._config);
this._datepicker = cis.createLoader<BsDatepickerInlineContainerComponent>(
_elementRef,
_viewContainerRef,
_renderer
);
}

ngOnInit(): any {
this.setConfig();

this._datepickerRef = this._datepicker
.provide({provide: BsDatepickerConfig, useValue: this._config})
.attach(BsDatepickerInlineContainerComponent)
.to(this._elementRef)
.show();

// if date changes from external source (model -> view)
this._subs.push(
this.bsValueChange.subscribe((value: Date) => {
this._datepickerRef.instance.value = value;
})
);

// if date changes from picker (view -> model)
this._subs.push(
this._datepickerRef.instance.valueChange.subscribe((value: Date) => {
this.bsValue = value;
})
);
}

ngOnChanges(changes: SimpleChanges): void {
if (!this._datepickerRef || !this._datepickerRef.instance) {
return;
}

if (changes.minDate) {
this._datepickerRef.instance.minDate = this.minDate;
}

if (changes.maxDate) {
this._datepickerRef.instance.maxDate = this.maxDate;
}

if (changes.isDisabled) {
this._datepickerRef.instance.isDisabled = this.isDisabled;
}
}

/**
* Set config for datepicker
*/
setConfig(): void {
this._config = Object.assign({}, this._config, this.bsConfig, {
value: this._bsValue,
isDisabled: this.isDisabled,
minDate: this.minDate || this.bsConfig && this.bsConfig.minDate,
maxDate: this.maxDate || this.bsConfig && this.bsConfig.maxDate
});
}

ngOnDestroy(): any {
this._datepicker.dispose();
}
}
7 changes: 7 additions & 0 deletions src/datepicker/bs-datepicker-inline.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Injectable } from '@angular/core';
import { BsDatepickerConfig } from './bs-datepicker.config';

@Injectable()
export class BsDatepickerInlineConfig extends BsDatepickerConfig {

}
12 changes: 10 additions & 2 deletions src/datepicker/bs-datepicker.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { BsDatepickerConfig } from './bs-datepicker.config';
import { BsDaterangepickerInputDirective } from './bs-daterangepicker-input.directive';
import { BsDaterangepickerDirective } from './bs-daterangepicker.component';
import { BsDaterangepickerConfig } from './bs-daterangepicker.config';
import { BsDatepickerInlineDirective } from './bs-datepicker-inline.component';
import { BsDatepickerInlineConfig } from './bs-datepicker-inline.config';

import { BsLocaleService } from './bs-locale.service';
import { BsDatepickerActions } from './reducer/bs-datepicker.actions';
Expand All @@ -26,16 +28,20 @@ import { BsDaysCalendarViewComponent } from './themes/bs/bs-days-calendar-view.c
import { BsMonthCalendarViewComponent } from './themes/bs/bs-months-calendar-view.component';
import { BsTimepickerViewComponent } from './themes/bs/bs-timepicker-view.component';
import { BsYearsCalendarViewComponent } from './themes/bs/bs-years-calendar-view.component';
import { BsDatepickerInlineContainerComponent } from './themes/bs/bs-datepicker-inline-container.component';

const _exports = [
BsDatepickerContainerComponent,
BsDaterangepickerContainerComponent,
BsDatepickerInlineContainerComponent,

BsDatepickerDirective,
BsDatepickerInputDirective,

BsDaterangepickerInputDirective,
BsDaterangepickerDirective
BsDaterangepickerDirective,

BsDatepickerInlineDirective
];

@NgModule({
Expand All @@ -57,7 +63,8 @@ const _exports = [
],
entryComponents: [
BsDatepickerContainerComponent,
BsDaterangepickerContainerComponent
BsDaterangepickerContainerComponent,
BsDatepickerInlineContainerComponent
],
exports: _exports
})
Expand All @@ -78,6 +85,7 @@ export class BsDatepickerModule {
BsDatepickerActions,
BsDatepickerConfig,
BsDaterangepickerConfig,
BsDatepickerInlineConfig,
BsDatepickerEffects,
BsLocaleService
]
Expand Down
2 changes: 2 additions & 0 deletions src/datepicker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export { DatepickerConfig } from './datepicker.config';
export { BsDatepickerModule } from './bs-datepicker.module';
export { BsDatepickerDirective } from './bs-datepicker.component';
export { BsDaterangepickerDirective } from './bs-daterangepicker.component';
export { BsDatepickerInlineDirective } from './bs-datepicker-inline.component';
export { BsDatepickerConfig } from './bs-datepicker.config';
export { BsDaterangepickerConfig } from './bs-daterangepicker.config';
export { BsDatepickerInlineConfig } from './bs-datepicker-inline.config';
export { BsLocaleService } from './bs-locale.service';
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Component, OnDestroy, OnInit } from '@angular/core';
import { BsDatepickerContainerComponent } from './bs-datepicker-container.component';

import { BsDatepickerConfig } from '../../bs-datepicker.config';
import { BsDatepickerActions } from '../../reducer/bs-datepicker.actions';
import { BsDatepickerEffects } from '../../reducer/bs-datepicker.effects';
import { BsDatepickerStore } from '../../reducer/bs-datepicker.store';

@Component({
selector: 'bs-datepicker-inline-container',
providers: [BsDatepickerStore, BsDatepickerEffects],
templateUrl: './bs-datepicker-view.html',
host: {
'(click)': '_stopPropagation($event)',
style: 'display: inline-block;'
}
})
export class BsDatepickerInlineContainerComponent extends BsDatepickerContainerComponent
implements OnInit, OnDestroy {
constructor(
_config: BsDatepickerConfig,
_store: BsDatepickerStore,
_actions: BsDatepickerActions,
_effects: BsDatepickerEffects
) {
super(_config, _store, _actions, _effects);
}
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ButtonsModule } from './buttons/buttons.module';
import { CarouselModule } from './carousel/carousel.module';
import { CollapseModule } from './collapse/collapse.module';
import { BsDaterangepickerConfig } from './datepicker/bs-daterangepicker.config';
import { BsDatepickerInlineConfig } from './datepicker/bs-datepicker-inline.config';
import { DatepickerModule } from './datepicker/datepicker.module';
import { BsDropdownModule } from './dropdown/bs-dropdown.module';
import { ModalModule } from './modal/modal.module';
Expand Down Expand Up @@ -56,6 +57,7 @@ export {
BsDatepickerModule,
BsDatepickerConfig,
BsDaterangepickerConfig,
BsDatepickerInlineConfig,
BsLocaleService,
BsDaterangepickerDirective,
BsDatepickerDirective
Expand Down

0 comments on commit afcf540

Please sign in to comment.