Skip to content

Commit

Permalink
fix: Merge pull request #272 from criticalbh/262-datepicker-binding
Browse files Browse the repository at this point in the history
262-datepicker-binding fix
  • Loading branch information
rubyboy authored May 21, 2017
2 parents 4e9b922 + cf8963c commit 275fc45
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
22 changes: 16 additions & 6 deletions sample/src/app/components/datepicker.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import {MaterializeDirective} from "angular2-materialize";
import {Component} from "@angular/core"
import {Component} from '@angular/core';
declare var Materialize: any;

@Component({
selector: "datePicker",
template: `
<form materialize class="col s12">
<div class="row">
<div class="col s6">
<label for="birthdate">Birthdate</label>
<input id="birthdate"
<label for="birthdate">Birthdate {{birthdate}}</label>
<input id="birthdate" [(ngModel)]="birthdate" name="birthdate" (ngModelChange)="modelChanged($event)"
materialize="pickadate"
[materializeParams]="[{selectMonths: true, selectYears: 15}]"
[materializeParams]="[{format: 'dd/mm/yyyy'}]"
type="text" />
</div>
</div>
</form>
`
})
export class DatePicker {}
export class DatePicker {
birthdate;

constructor() {
this.birthdate = new Date('03/12/2017');
}

modelChanged($event) {
console.log($event);
}
}
28 changes: 13 additions & 15 deletions src/materialize-directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ export class MaterializeDirective implements AfterViewInit,DoCheck,OnChanges,OnD
// this is here to trigger change detection for select elements
@Input() public set materializeSelectOptions(options:any) { }

//used for the datepicker at the moment
@Input() ngModel;

public ngAfterViewInit() {
this.performElementUpdates();
}
Expand Down Expand Up @@ -153,22 +156,17 @@ export class MaterializeDirective implements AfterViewInit,DoCheck,OnChanges,OnD
jQueryElement.on("change", e => nativeElement.dispatchEvent((<any>CustomEvent("input"))));
}

if (this.isDatePicker()) {
const nativeElement = this._el.nativeElement;
const jQueryElement = $(nativeElement);
const enablebtns = this.enableDPButtons;
if (this.isDatePicker()) {
const nativeElement = this._el.nativeElement;
const jqueryPickerElement = $(nativeElement);

jQueryElement.on("change", e => nativeElement.dispatchEvent((<any>CustomEvent("input"))));
const datePicker = jQueryElement[this._functionName](...this._params);
const picker = datePicker.pickadate('picker');
jQueryElement.mousedown(() => {
if (!jQueryElement.val()) {
return;
}

return picker.set('select', jQueryElement.val(), ...this._params)
});
}
const datePicker = jqueryPickerElement[this._functionName](...this._params);
const picker = datePicker.pickadate('picker');
setTimeout(() => {
picker.set('select', this.ngModel);
jqueryPickerElement.on('change', e => nativeElement.dispatchEvent(new Event('input')));
});
}

if (this.isTimePicker()) {
const nativeElement = this._el.nativeElement;
Expand Down

0 comments on commit 275fc45

Please sign in to comment.