Last active
October 17, 2016 13:10
-
-
Save Thanood/aaad89e595e55ba8a4e551e316a69710 to your computer and use it in GitHub Desktop.
Flatpicker custom attribute
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
// https://github.com/Microsoft/TypeScript/issues/5073 | |
declare module 'flatpickr' { | |
class Flatpickr { | |
constructor(element: Element, options?: any); | |
destroy(); | |
} | |
namespace Flatpickr {} | |
export = Flatpickr; | |
} |
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
import {autoinject, bindable, bindingMode, customAttribute, LogManager} from 'aurelia-framework'; | |
import * as Flatpickr from 'flatpickr'; | |
import {fireEvent} from '../../common/events'; | |
@customAttribute('flatpickr') | |
@autoinject() | |
export class FlatpickrAttribute { | |
@bindable() value: Date = new Date(); | |
@bindable({ | |
defaultBindingMode: bindingMode.oneTime | |
}) elementOverride; | |
fpInstance; | |
log; | |
constructor(private element: Element) { | |
this.log = LogManager.getLogger('flatpickr'); | |
} | |
bind() { | |
if (!this.elementOverride && this.element.tagName !== 'INPUT') { | |
this.elementOverride = this.element.querySelector('input'); | |
} | |
this.fpInstance = new Flatpickr(this.elementOverride || this.element, { | |
defaultDate: this.value, | |
dateFormat: 'd.m.Y', | |
onChange: this.handlePickerChange.bind(this) | |
}); | |
} | |
attached() { } | |
detached() { | |
this.fpInstance.destroy(); | |
} | |
handlePickerChange(dateObj, dateStr, instance) { | |
fireEvent(this.element, 'date-change', { date: dateObj, dateString: dateStr }); | |
} | |
valueChanged(newValue) { | |
if (this.fpInstance) { | |
this.fpInstance.setDate(newValue); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment