-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(datepicker): Add directive for inline datepicker (#3956)
* feat(datepicker): Add directive for inline datepicker Closes #3955 * feat(datepicker): add support to set custom classes on specific dates Closes #3958 * Revert "feat(datepicker): add support to set custom classes on specific dates" This reverts commit 47a1a11. * feat(datepicker): resolve conflicts * fix(datepicker): fix tslint * fix(datepicker): clean up * fix(datepicker): add base spec for inline datepicker * fix(common): fix imports for system js
- Loading branch information
Showing
14 changed files
with
354 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,3 @@ | |
bsDaterangepicker> | ||
</div> | ||
</div> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
demo/src/app/components/+datepicker/demos/inline-datepicker/inline-datepicker.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<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> |
9 changes: 9 additions & 0 deletions
9
demo/src/app/components/+datepicker/demos/inline-datepicker/inline-datepicker.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Component } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'demo-datepicker-inline', | ||
templateUrl: './inline-datepicker.component.html' | ||
}) | ||
export class DemoDatepickerInlineComponent { | ||
bsInlineValue = new Date(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { | ||
ComponentRef, Directive, ElementRef, EventEmitter, Input, OnChanges, | ||
OnDestroy, OnInit, Output, Renderer2, SimpleChanges, ViewContainerRef | ||
} from '@angular/core'; | ||
import { ComponentLoader, ComponentLoaderFactory } from 'ngx-bootstrap/component-loader'; | ||
import { BsDatepickerInlineContainerComponent } from './themes/bs/bs-datepicker-inline-container.component'; | ||
import { Subscription } from 'rxjs'; | ||
import { BsDatepickerInlineConfig } from './bs-datepicker-inline.config'; | ||
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(): void { | ||
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { BsDatepickerConfig } from './bs-datepicker.config'; | ||
|
||
@Injectable() | ||
export class BsDatepickerInlineConfig extends BsDatepickerConfig { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.