@3mo/date-time-fields
Version:
Date time fields let people select dates, date-ranges, and times.
63 lines (62 loc) • 1.95 kB
JavaScript
import { __decorate } from "tslib";
import { component, property } from '@a11d/lit';
import { Localizer } from '@3mo/localization';
import { FieldDateTimeBase } from './FieldDateTimeBase.js';
import { Memoize as memoize } from 'typescript-memoize';
Localizer.dictionaries.add('de', {
'Date & Time': 'Datum & Uhrzeit',
});
/**
* @element mo-field-date-time
*
* @i18n "Date & Time"
*/
let FieldDateTime = class FieldDateTime extends FieldDateTimeBase {
constructor() {
super(...arguments);
this.label = t('Date & Time');
}
get selectedDate() {
if (!this.value) {
return undefined;
}
if (this.value instanceof Date) {
return new DateTime(this.value);
}
return this.value;
}
get placeholder() {
return new DateTime().format(this.formatOptions);
}
resetNavigationDate() {
this.navigationDate = this.selectedDate ?? new DateTime();
}
get calendarValue() {
return new DateTimeRange(this.selectedDate, this.selectedDate);
}
handleSelectedDateChange(date, precision) {
const { hour, minute, second } = this.navigationDate;
date = date.with({ hour, minute, second });
this.value = !date ? undefined : this.precision.getRange(date).start;
super.handleSelectedDateChange(date, precision);
}
valueToInputValue(value) {
return value?.format(this.formatOptions) ?? '';
}
inputValueToValue(value) {
return value ? DateTime.parseAsDateTime(value, this.shortcutReferenceDate) : undefined;
}
};
__decorate([
property()
], FieldDateTime.prototype, "label", void 0);
__decorate([
property({ type: Object })
], FieldDateTime.prototype, "value", void 0);
__decorate([
memoize()
], FieldDateTime.prototype, "placeholder", null);
FieldDateTime = __decorate([
component('mo-field-date-time')
], FieldDateTime);
export { FieldDateTime };