@kelvininc/ui-components
Version:
Kelvin UI Components
347 lines (343 loc) • 21.7 kB
JavaScript
import { r as registerInstance, c as createEvent, h, H as Host } from './index-D-JVwta2.js';
import { a as EComponentSize } from './components-DzZLIMy0.js';
import './lib-config-DwRzddFC.js';
import './action-button.types-DVds6a5Z.js';
import { i as EAbsoluteTimePickerMode, j as ERelativeTimeInputMode, k as EInputSource } from './absolute-time-picker-dropdown.types-CojoW2Y2.js';
import { E as EIconName } from './icon.types-SVedE_O8.js';
import './summary-card.types-BcMhjKoS.js';
import './toaster.types-vhHhaF4Q.js';
import './tree-item.types-CBuzk8fR.js';
import './tag-alarm.types-DHk26cGe.js';
import './wizard.types-7ioMFMb5.js';
import { m as fromDateInput, h as fromISO, o as newDate, p as isDateSame, q as isDateBefore, a as dayjs } from './date.helper-D0D1COyq.js';
import { b as DEFAULT_HEADER_TITLE, c as CALENDAR_INPUT_MIN_DATE, d as CALENDAR_INPUT_MAX_DATE, D as DATETIME_INPUT_MASK, a as CALENDAR_MASK, e as DATE_INPUT_MASK, C as CALENDAR_DATE_TIME_MASK, f as DATE_INPUT_PLACEHOLDER } from './absolute-time-picker.config-jTjLvKa0.js';
import { b as buildSelectedDatesEventPayload, c as getMinimumDateFromDayClick, d as getMaximumDateFromDayClick, i as isEndDateAtStartOfDay, g as getFirstCalendarInitialDate, a as getSecondCalendarInitialDate, e as getFromDateInputState, f as getToDateTimeInputState, h as getSingleDateTimeInputState } from './absolute-time-picker.helper-B7XUlfpI.js';
import { D as DATE_FORMAT } from './calendar.config-BY-Rxx_4.js';
import { i as isEmpty } from './isEmpty-BAGi1PqI.js';
import './_commonjsHelpers-BFTU3MAI.js';
import './_Set-B7Zkvu4X.js';
import './_Map-B6Xd0L4K.js';
import './isObject-Dkd2PDJ-.js';
const absoluteTimePickerCss = "@property --rotation{syntax:\"<angle>\";initial-value:0deg;inherits:false}@keyframes rotate-border{to{--rotation:360deg}}.absolute-time-picker-container{display:flex;flex-direction:column;gap:var(--kv-spacing-3x, 12px);background-color:var(--kv-neutral-7, #2a2a2a);padding:var(--kv-spacing-4x, 16px);border-radius:4px;width:fit-content}.absolute-time-picker-container .navigate-back{display:flex;gap:var(--kv-spacing, 4px);align-items:center;cursor:pointer;width:fit-content}.absolute-time-picker-container .navigate-back kv-icon{--icon-color:var(--kv-neutral-4, #bebebe)}.absolute-time-picker-container .navigate-back .back-text{font-family:var(--kv-primary-font, \"proxima-nova\", sans-serif, \"Arial\");font-size:12px;font-weight:400;font-stretch:normal;font-style:normal;line-height:18px;letter-spacing:normal;text-transform:none;color:var(--kv-neutral-4, #bebebe);user-select:none}.absolute-time-picker-container .header .title{font-family:var(--kv-primary-font, \"proxima-nova\", sans-serif, \"Arial\");font-size:16px;font-weight:600;font-stretch:normal;font-style:normal;line-height:24px;letter-spacing:normal;text-transform:none;color:var(--kv-neutral-2, #e5e5e5)}.absolute-time-picker-container .absolute-range-input{display:flex;gap:var(--kv-spacing-6x, 24px)}.absolute-time-picker-container .absolute-range-input kv-date-time-input{width:196px}.absolute-time-picker-container .absolute-point-input{width:100%}.absolute-time-picker-container .calendars{display:flex;gap:var(--kv-spacing-6x, 24px)}";
const KvAbsoluteTimePicker = class {
constructor(hostRef) {
registerInstance(this, hostRef);
this.selectedDatesChange = createEvent(this, "selectedDatesChange", 7);
this.backButtonClicked = createEvent(this, "backButtonClicked", 7);
this.relativeTimeConfigReset = createEvent(this, "relativeTimeConfigReset", 7);
this.relativeTimeConfigChange = createEvent(this, "relativeTimeConfigChange", 7);
var _a;
/** @inheritdoc */
this.headerTitle = DEFAULT_HEADER_TITLE;
/** @inheritdoc */
this.displayBackButton = false;
/** @inheritdoc */
this.mode = EAbsoluteTimePickerMode.Range;
/** @inheritdoc */
this.selectedDates = [];
/** @inheritdoc */
this.disabledDates = [];
/** @inheritdoc */
this.calendarInputMinDate = CALENDAR_INPUT_MIN_DATE;
/** @inheritdoc */
this.calendarInputMaxDate = CALENDAR_INPUT_MAX_DATE;
/** Input values inserted by the user on the date and time input */
this.fromInputValue = '';
this.toInputValue = '';
this.singleInputValue = '';
/** Shared hovered date between calendars for range mode */
this.hoveredDate = '';
/** First calendar displayed month (Second calendar is displayedMonth + 1) */
this.displayedMonth = (_a = fromDateInput(this.initialDate)) !== null && _a !== void 0 ? _a : fromDateInput(new Date());
/** Used to force focus the from and to input */
this.fromInputFocused = true;
this.toInputFocused = false;
/**
* Used to define when the input dates came from the relative time picker where the input
* can be an absolute date or a text (ex: last 24 hours)
*/
this.inputMode = ERelativeTimeInputMode.Date;
/** Calendars max and minimum dates */
this.minDate = '';
this.maxDate = '';
this.setDateLimits = (min, max) => {
this.minDate = min;
this.maxDate = max;
};
this.setInputValues = (from, to) => {
this.fromInputValue = from;
this.toInputValue = to;
};
this.resetInputValues = () => {
this.fromInputValue = '';
this.toInputValue = '';
this.singleInputValue = '';
};
this.resetDateLimits = () => {
this.minDate = '';
this.maxDate = '';
};
this.emitSelectRangeDatesChangeEvent = (dateA, dateB) => {
const range = buildSelectedDatesEventPayload(dateA, dateB);
this.selectedDatesChange.emit({
range
});
};
this.handleBackClick = (event) => {
this.backButtonClicked.emit(event);
};
this.onClickDate = ({ detail }) => {
var _a;
const date = detail.date;
const clickedDate = fromISO(date);
const inputDate = newDate(date);
const fromDate = getMinimumDateFromDayClick(clickedDate, this.calendarInputMinDate);
const toDate = getMaximumDateFromDayClick(clickedDate, this.calendarInputMaxDate);
if (this.mode === EAbsoluteTimePickerMode.Range) {
const [selectedStartDate, selectedEndDate] = (_a = this.selectedDates) !== null && _a !== void 0 ? _a : [];
if (!selectedStartDate) {
this.displayedMonth = clickedDate.startOf('day');
this.setInputValues(fromDate.format(DATETIME_INPUT_MASK), '');
this.setDateLimits(clickedDate.format(CALENDAR_MASK), '');
this.emitSelectRangeDatesChangeEvent(fromDate);
return;
}
if (isDateSame(clickedDate, selectedStartDate)) {
if (!selectedEndDate) {
this.setInputValues(fromDate.format(DATETIME_INPUT_MASK), toDate.format(DATETIME_INPUT_MASK));
this.resetDateLimits();
this.emitSelectRangeDatesChangeEvent(fromDate, toDate);
return;
}
this.resetInputValues();
this.resetDateLimits();
this.emitSelectRangeDatesChangeEvent();
return;
}
if (selectedEndDate !== undefined) {
this.displayedMonth = inputDate;
this.setInputValues(fromDate.format(DATETIME_INPUT_MASK), '');
this.setDateLimits(inputDate.format(CALENDAR_MASK), '');
this.emitSelectRangeDatesChangeEvent(fromDate);
return;
}
if (isDateBefore(clickedDate, selectedStartDate)) {
this.displayedMonth = clickedDate;
this.setInputValues(fromDate.format(DATETIME_INPUT_MASK), '');
this.setDateLimits(fromDate.format(CALENDAR_MASK), '');
this.emitSelectRangeDatesChangeEvent(fromDate);
return;
}
this.setInputValues(newDate(selectedStartDate).format(DATETIME_INPUT_MASK), toDate.format(DATETIME_INPUT_MASK));
this.resetDateLimits();
this.emitSelectRangeDatesChangeEvent(fromISO(selectedStartDate), toDate);
return;
}
else {
this.displayedMonth = clickedDate;
this.singleInputValue = fromDate.format(DATETIME_INPUT_MASK);
this.emitSelectRangeDatesChangeEvent(fromDate);
}
};
this.handleClickBackMonth = () => {
this.displayedMonth = this.displayedMonth.subtract(1, 'month');
if (!isEmpty(this.relativeTimeConfig)) {
this.relativeTimeConfigReset.emit();
}
};
this.handleClickForwardMonth = () => {
this.displayedMonth = this.displayedMonth.add(1, 'month');
if (!isEmpty(this.relativeTimeConfig)) {
this.relativeTimeConfigReset.emit();
}
};
this.handleHoveredDateChange = (event) => {
if (this.mode === EAbsoluteTimePickerMode.Range)
this.hoveredDate = event.detail;
};
this.handleInputReset = () => {
this.resetInputValues();
this.inputMode = ERelativeTimeInputMode.Date;
this.relativeTimeConfigReset.emit();
};
/** User date-time-input I/O event handlers */
this.handleOnFocusFromInput = () => {
this.toInputFocused = false;
this.fromInputFocused = true;
const parsedFromInputDate = dayjs(this.fromInputValue, DATETIME_INPUT_MASK);
const parsedToInputDate = dayjs(this.toInputValue, DATETIME_INPUT_MASK);
if (this.relativeTimeConfig) {
if (this.relativeTimeConfig.mode === ERelativeTimeInputMode.Text) {
this.handleInputReset();
}
else {
if (!isEmpty(this.fromInputValue)) {
this.displayedMonth = parsedFromInputDate;
}
this.emitSelectRangeDatesChangeEvent(dayjs(this.fromInputValue, DATETIME_INPUT_MASK), dayjs(this.toInputValue, DATETIME_INPUT_MASK));
}
}
else {
if (!isEmpty(this.fromInputValue) && (parsedToInputDate.diff(parsedFromInputDate, 'month') > 0 || this.displayedMonth.diff(parsedFromInputDate, 'month') > 0)) {
this.displayedMonth = parsedFromInputDate;
}
}
};
this.handleOnFocusToInput = () => {
this.toInputFocused = true;
this.fromInputFocused = false;
const parsedToInputDate = dayjs(this.toInputValue, DATETIME_INPUT_MASK);
const parsedFromInputDate = dayjs(this.fromInputValue, DATETIME_INPUT_MASK);
if (this.relativeTimeConfig) {
if (this.relativeTimeConfig.mode === ERelativeTimeInputMode.Text) {
this.handleInputReset();
}
else {
if (!isEmpty(this.toInputValue) && parsedFromInputDate.diff(parsedToInputDate, 'month') > 0) {
this.displayedMonth = parsedToInputDate.subtract(1, 'month');
}
this.emitSelectRangeDatesChangeEvent(dayjs(this.fromInputValue, DATETIME_INPUT_MASK), dayjs(this.toInputValue, DATETIME_INPUT_MASK));
}
}
else {
if (!isEmpty(this.toInputValue) && (parsedToInputDate.diff(parsedFromInputDate, 'month') > 0 || this.displayedMonth.diff(parsedToInputDate, 'month') > 0)) {
this.displayedMonth = parsedToInputDate.subtract(1, 'month');
}
}
};
this.handleDateChange = (event, inputSource) => {
const date = event.detail;
const parsedDate = dayjs(date, DATE_INPUT_MASK);
const parsedDateTime = dayjs(date, DATETIME_INPUT_MASK);
if (inputSource === EInputSource.Single) {
if (parsedDate.isValid() && parsedDateTime.isValid()) {
this.singleInputValue = parsedDateTime.format(DATETIME_INPUT_MASK);
this.displayedMonth = parsedDate;
this.emitSelectRangeDatesChangeEvent(parsedDateTime);
}
}
else {
if (parsedDateTime.isValid()) {
if (inputSource === EInputSource.From) {
this.handleFromInputDateChange(parsedDate, parsedDateTime);
}
else {
this.handleToInputDateChange(parsedDate, parsedDateTime);
}
}
else {
if (isEmpty(this.fromInputValue)) {
this.minDate = '';
}
if (isEmpty(this.toInputValue)) {
this.maxDate = '';
}
}
}
};
this.handleFromInputDateChange = (parsedDate, parsedDateTime) => {
this.fromInputValue = parsedDateTime.format(DATETIME_INPUT_MASK);
this.displayedMonth = parsedDate;
if (!isEmpty(this.toInputValue)) {
this.emitSelectRangeDatesChangeEvent(parsedDateTime, dayjs(this.toInputValue, DATETIME_INPUT_MASK));
this.resetDateLimits();
}
else {
this.emitSelectRangeDatesChangeEvent(parsedDateTime);
this.toInputFocused = true;
this.minDate = parsedDate.format(CALENDAR_MASK);
}
};
this.handleToInputDateChange = (parsedDate, parsedDateTime) => {
this.toInputValue = parsedDateTime.format(DATETIME_INPUT_MASK);
this.displayedMonth = parsedDate.subtract(1, 'month');
if (!isEmpty(this.fromInputValue)) {
this.emitSelectRangeDatesChangeEvent(dayjs(this.fromInputValue, DATETIME_INPUT_MASK), parsedDateTime);
this.resetDateLimits();
}
else {
this.maxDate = parsedDate.format(CALENDAR_MASK);
this.emitSelectRangeDatesChangeEvent(parsedDateTime);
this.fromInputFocused = true;
}
};
this.handleEndDateLostFocus = (event) => {
const date = event.detail;
const parsedDate = dayjs(date, DATE_INPUT_MASK);
const parsedDateTime = dayjs(date, DATETIME_INPUT_MASK);
if (parsedDate.isValid() && isEndDateAtStartOfDay(parsedDateTime)) {
const parsedDateFormatted = parsedDate.endOf('day').format(DATETIME_INPUT_MASK);
this.toInputValue = parsedDateFormatted;
this.displayedMonth = parsedDate.subtract(1, 'month');
if (!isEmpty(this.fromInputValue)) {
this.emitSelectRangeDatesChangeEvent(dayjs(this.fromInputValue, DATETIME_INPUT_MASK), parsedDate.endOf('day'));
}
else {
this.emitSelectRangeDatesChangeEvent(parsedDate.endOf('day'));
this.fromInputFocused = true;
}
}
};
/** Components config methods */
this.useInputMask = () => {
return this.inputMode === ERelativeTimeInputMode.Date;
};
this.getCalendarStringLimits = () => ({
minDate: !isEmpty(this.minDate) ? this.minDate : dayjs(this.calendarInputMinDate, DATETIME_INPUT_MASK).format(DATE_FORMAT),
maxDate: !isEmpty(this.maxDate) ? this.maxDate : dayjs(this.calendarInputMaxDate, DATETIME_INPUT_MASK).format(DATE_FORMAT)
});
this.getCalendarTimestampLimits = () => ({
minDate: !isEmpty(this.minDate) ? undefined : dayjs(this.calendarInputMinDate, DATETIME_INPUT_MASK).valueOf(),
maxDate: !isEmpty(this.maxDate) ? undefined : dayjs(this.calendarInputMaxDate, DATETIME_INPUT_MASK).valueOf()
});
}
handleSelectedRangeDatesChange(value = []) {
if (value.length === 0) {
this.resetInputValues();
this.resetDateLimits();
return;
}
if (this.mode === EAbsoluteTimePickerMode.Range) {
if (isEmpty(this.relativeTimeConfig)) {
const [from, to] = value;
const parsedFromDate = dayjs(from, CALENDAR_DATE_TIME_MASK).format(DATETIME_INPUT_MASK);
const parsedToDate = dayjs(to, CALENDAR_DATE_TIME_MASK).format(DATETIME_INPUT_MASK);
this.setInputValues(parsedFromDate, parsedToDate);
}
}
else {
const [date] = value;
const parsedDate = dayjs(date, CALENDAR_DATE_TIME_MASK).format(DATETIME_INPUT_MASK);
this.singleInputValue = parsedDate;
}
}
handleRelativeTimeConfigInput(newValue) {
var _a;
if (isEmpty(newValue)) {
this.inputMode = ERelativeTimeInputMode.Date;
if (!this.selectedDates || this.selectedDates.length === 0) {
this.setInputValues('', '');
this.displayedMonth = fromDateInput(new Date());
}
return;
}
this.inputMode = newValue.mode;
this.setInputValues(newValue.from, newValue.to);
const [from] = (_a = this.selectedDates) !== null && _a !== void 0 ? _a : [];
const date = newDate(from);
if (date.isValid()) {
this.displayedMonth = date;
}
}
render() {
const fromCalendarInitialDate = getFirstCalendarInitialDate(this.displayedMonth);
const toCalendarInitialDate = getSecondCalendarInitialDate(this.displayedMonth);
return (h(Host, { key: '25e66a7babdc4feef5fd99969d1f135698752a44' }, h("div", { key: '6fdc8c49640ce9efa33ac740da64487648762097', class: "absolute-time-picker-container" }, this.displayBackButton && (h("div", { key: 'a47537fad2669332373350101c208c4554152f36', class: "navigate-back", onClick: this.handleBackClick }, h("kv-icon", { key: 'ca2a47f5cbdccdf6c2f782b35c62e917ec3a9c1d', name: EIconName.SlimRight, customClass: "rotate-180" }), h("div", { key: 'b9d6b9d018a4b16175d7fd97a323e2deae4fa2b9', class: "back-text" }, "Back"))), h("div", { key: '60f81ab5cd34c78da812e7414e12db89b6a7a73d', class: "header" }, h("div", { key: '7fec81a57a70cf9d21b714af99640b3c2a0103a5', class: "title" }, this.headerTitle)), this.mode === EAbsoluteTimePickerMode.Range ? (h("div", { class: "absolute-range-input" }, h("kv-date-time-input", Object.assign({ inputName: "from-input", useInputMask: this.useInputMask(), label: "From", value: this.fromInputValue, size: EComponentSize.Small, placeholder: DATE_INPUT_PLACEHOLDER, highlighted: isEmpty(this.fromInputValue) && !this.toInputFocused, onTextChange: ev => this.handleDateChange(ev, EInputSource.From), onInputFocus: this.handleOnFocusFromInput }, getFromDateInputState(this.error, this.getCalendarTimestampLimits()))), h("kv-date-time-input", Object.assign({ inputName: "to-input", useInputMask: this.useInputMask(), label: "To", value: this.toInputValue, size: EComponentSize.Small, placeholder: DATE_INPUT_PLACEHOLDER, highlighted: isEmpty(this.toInputValue) && !isEmpty(this.fromInputValue), onTextChange: ev => this.handleDateChange(ev, EInputSource.To), onDateTimeBlur: this.handleEndDateLostFocus, onInputFocus: this.handleOnFocusToInput }, getToDateTimeInputState(this.error, this.getCalendarTimestampLimits()))))) : (h("div", { class: "absolute-point-input" }, h("kv-date-time-input", Object.assign({ id: "single-date-input", useInputMask: true, label: "Day & Hour", value: this.singleInputValue, size: EComponentSize.Small, placeholder: DATE_INPUT_PLACEHOLDER, onTextChange: ev => this.handleDateChange(ev, EInputSource.Single) }, getSingleDateTimeInputState(this.error, this.getCalendarTimestampLimits()))))), h("div", { key: '8ce1318dbbe6ca8d6671940b6796d937f8012321', class: "calendars" }, h("kv-calendar", Object.assign({ key: 'c6a6567d84918b8412178a1d2e8af1915a7813c6', mode: this.mode, displayNextMonthArrow: false, displayPreviousMonthArrow: true, selectedDates: this.selectedDates, hoveredDate: this.hoveredDate, initialDate: fromCalendarInitialDate, disabledDates: this.disabledDates, onClickDate: this.onClickDate, onChangeMonth: this.handleClickBackMonth, onHoveredDateChange: this.handleHoveredDateChange }, this.getCalendarStringLimits())), h("kv-calendar", Object.assign({ key: '96576139143224ac533005be2fd94c369d7c7192', mode: this.mode, displayNextMonthArrow: true, displayPreviousMonthArrow: false, selectedDates: this.selectedDates, hoveredDate: this.hoveredDate, initialDate: toCalendarInitialDate, disabledDates: this.disabledDates, onClickDate: this.onClickDate, onChangeMonth: this.handleClickForwardMonth, onHoveredDateChange: this.handleHoveredDateChange }, this.getCalendarStringLimits()))))));
}
static get watchers() { return {
"selectedDates": ["handleSelectedRangeDatesChange"],
"relativeTimeConfig": ["handleRelativeTimeConfigInput"]
}; }
};
KvAbsoluteTimePicker.style = absoluteTimePickerCss;
export { KvAbsoluteTimePicker as kv_absolute_time_picker };