UNPKG

@kelvininc/ui-components

Version:
105 lines (101 loc) 4.22 kB
import { H as dayjs } from './p-BS8xGxWJ.js'; import { l as EAbsoluteTimeError, B as EValidationState } from './p-BP5CxQcH.js'; const DEFAULT_HEADER_TITLE = 'Custom Interval'; const DATE_INPUT_PLACEHOLDER = 'dd-mm-yyyy 00:00:00'; const DATETIME_INPUT_MASK = 'DD-MM-YYYY HH:mm:ss'; const DATE_INPUT_MASK = 'DD-MM-YYYY'; const CALENDAR_MASK = 'YYYY-MM-DD'; const CALENDAR_DATE_TIME_MASK = 'YYYY-MM-DD HH:mm:ss'; const CALENDAR_INPUT_MIN_DATE = '01-01-2018 00:00:00'; /** * The max date bellow must be setted manually due to the component tests. * If we use something like: dayjs().add(50, 'years'), the test will break on the following days */ const CALENDAR_INPUT_MAX_DATE = '31-12-3000 23:59:59'; const buildSelectedDatesEventPayload = (dateA, dateB) => { if (!dateA && !dateB) { return []; } if (!dateB) { return [dateA.format(CALENDAR_DATE_TIME_MASK)]; } return [dateA.format(CALENDAR_DATE_TIME_MASK), dateB.format(CALENDAR_DATE_TIME_MASK)]; }; const isEndDateAtStartOfDay = (date) => { return date.isValid() && date.hour() === 0 && date.minute() === 0 && date.second() === 0; }; const getFirstCalendarInitialDate = (displayedMonth) => { const initialDate = displayedMonth; return initialDate.isValid() ? initialDate.format(CALENDAR_MASK) : ''; }; const getSecondCalendarInitialDate = (displayedMonth) => { const initialDate = displayedMonth; return initialDate.isValid() ? initialDate.add(1, 'month').format(CALENDAR_MASK) : ''; }; const getMinimumDateFromDayClick = (clickedDate, minimumDate) => { const parsedMinDate = dayjs(minimumDate, DATETIME_INPUT_MASK); if (clickedDate.startOf('day').isBefore(parsedMinDate)) { return parsedMinDate; } return clickedDate.startOf('day'); }; const getMaximumDateFromDayClick = (clickedDate, maximumDate) => { const parsedMaxDate = dayjs(maximumDate, DATETIME_INPUT_MASK); if (clickedDate.endOf('day').isAfter(parsedMaxDate)) { return parsedMaxDate; } return clickedDate.endOf('day'); }; const getFromDateInputState = (error, { minDate }) => { if (!error) { return; } if (error === EAbsoluteTimeError.StartDateBeforeMinimumDate && minDate) { const min = dayjs(minDate).format(DATETIME_INPUT_MASK); return { state: EValidationState.Invalid, helpText: `The 'FROM' date must be after ${min}` }; } return; }; const getToDateTimeInputState = (error, { maxDate }) => { if (!error) { return; } if (error === EAbsoluteTimeError.EndDateAfterMaximumDate && maxDate) { const max = dayjs(maxDate).format(DATETIME_INPUT_MASK); return { state: EValidationState.Invalid, helpText: `The 'TO' date must be before ${max}` }; } if (error === EAbsoluteTimeError.EndDateBeforeStartDate) { return { state: EValidationState.Invalid, helpText: `The 'TO' date must be after 'FROM' date` }; } return; }; const getSingleDateTimeInputState = (error, { minDate, maxDate }) => { if (!error) { return; } if (error === EAbsoluteTimeError.StartDateBeforeMinimumDate && minDate) { const min = dayjs(minDate).format(DATETIME_INPUT_MASK); return { state: EValidationState.Invalid, helpText: `The date must be after ${min}` }; } if (error === EAbsoluteTimeError.EndDateAfterMaximumDate && maxDate) { const max = dayjs(maxDate).format(DATETIME_INPUT_MASK); return { state: EValidationState.Invalid, helpText: `The date must be before ${max}` }; } return; }; export { CALENDAR_DATE_TIME_MASK as C, DATETIME_INPUT_MASK as D, CALENDAR_MASK as a, getSecondCalendarInitialDate as b, DEFAULT_HEADER_TITLE as c, CALENDAR_INPUT_MIN_DATE as d, CALENDAR_INPUT_MAX_DATE as e, buildSelectedDatesEventPayload as f, getFirstCalendarInitialDate as g, getMinimumDateFromDayClick as h, getMaximumDateFromDayClick as i, DATE_INPUT_MASK as j, isEndDateAtStartOfDay as k, DATE_INPUT_PLACEHOLDER as l, getFromDateInputState as m, getToDateTimeInputState as n, getSingleDateTimeInputState as o };