Last active
February 24, 2018 12:28
-
-
Save ihadeed/5f73e703897318e86521d5e5008347d8 to your computer and use it in GitHub Desktop.
Angular2 Component: Semantic-UI Calendar Component (date/time picker)
This file contains hidden or 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 {Component, ElementRef, AfterViewInit, Output, EventEmitter, Input, Self} from '@angular/core'; | |
import {ControlValueAccessor, NgModel} from '@angular/common'; | |
declare var $: any; | |
@Component({ | |
selector: 'calendar', | |
template: ` | |
<div class="ui calendar"> | |
<div class="ui input left icon"> | |
<i class="calendar icon"></i> | |
<input type="text" placeholder="Click to select Date/Time" [value]="initialDate"> | |
</div> | |
</div> | |
`, | |
providers: [NgModel] | |
}) | |
export class CalendarComponent implements AfterViewInit, ControlValueAccessor { | |
@Output() change: EventEmitter<Date> = new EventEmitter<Date>(); | |
@Output() htmlElement: EventEmitter<HTMLElement> = new EventEmitter<HTMLElement>(); | |
@Input() settings: CalendarOptions = {}; | |
@Input() initialDate: Date; | |
public onChange: any = Function.prototype; | |
public onTouched: any = Function.prototype; | |
private selectedDate: Date; | |
constructor(private parentElement: ElementRef, @Self() private self: NgModel){ | |
this.self.valueAccessor = this; | |
} | |
ngAfterViewInit(): void { | |
this.settings.onChange = (date: Date) => { | |
this.writeValue(date); | |
}; | |
let calandarElement: HTMLElement = this.parentElement.nativeElement.children[0]; | |
this.htmlElement.emit(calandarElement); | |
$(calandarElement).calendar(this.settings); | |
} | |
writeValue (value: Date): void { | |
if (value === this.selectedDate) { | |
return; | |
} | |
this.self.viewToModelUpdate(value); | |
this.change.emit(value); | |
this.selectedDate = value; | |
} | |
registerOnChange(fn: (_: any) => void): void { this.onChange = fn; } | |
registerOnTouched(fn: () => void): void { this.onTouched = fn; } | |
} | |
export interface CalendarOptions { | |
type?: string; | |
startCalendar?: HTMLElement; | |
endCalendar?: HTMLElement; | |
startMode?: string; | |
ampm?: boolean; | |
on?: string; | |
minDate?: Date; | |
maxDate?: Date; | |
formatter?: Function; | |
monthFirst?: boolean; | |
inline?: boolean; | |
onChange?: Function; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, thank you for writing this component, I think it is very usefull!
I'm not able to correctly use the formatter setting in my component. I'd like to use a custom dateformat declaring it in the component as a CalendarOptions property. Could you provide an example?