mapcomplete/UI/Input/SimpleDatePicker.ts

45 lines
1.1 KiB
TypeScript
Raw Normal View History

import {InputElement} from "./InputElement";
import {UIEventSource} from "../../Logic/UIEventSource";
export default class SimpleDatePicker extends InputElement<string> {
IsSelected: UIEventSource<boolean> = new UIEventSource<boolean>(false);
private readonly value: UIEventSource<string>
2021-06-10 01:36:20 +02:00
private readonly _element: HTMLElement;
constructor(
value?: UIEventSource<string>
) {
super();
this.value = value ?? new UIEventSource<string>(undefined);
const self = this;
2021-06-10 01:36:20 +02:00
const el = document.createElement("input")
this._element = el;
el.type = "date"
el.oninput = () => {
// Already in YYYY-MM-DD value!
self.value.setData(el.value);
}
this.value.addCallbackAndRunD(v => {
2021-06-10 01:36:20 +02:00
el.value = v;
});
}
GetValue(): UIEventSource<string> {
return this.value;
}
IsValid(t: string): boolean {
2022-01-08 22:11:24 +01:00
return !isNaN(new Date(t).getTime());
}
protected InnerConstructElement(): HTMLElement {
return this._element
}
}