UNPKG

@mui/x-date-pickers-pro

Version:

The Pro plan edition of the MUI X Date and Time Picker components.

129 lines (128 loc) 6.32 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.rangeValueManager = exports.getRangeFieldValueManager = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose")); var _internals = require("@mui/x-date-pickers/internals"); var _dateFieldsUtils = require("./date-fields-utils"); const _excluded = ["value", "referenceDate"]; const rangeValueManager = exports.rangeValueManager = { emptyValue: [null, null], getTodayValue: (utils, timezone, valueType) => [(0, _internals.getTodayDate)(utils, timezone, valueType), (0, _internals.getTodayDate)(utils, timezone, valueType)], getInitialReferenceValue: _ref => { let { value, referenceDate: referenceDateProp } = _ref, params = (0, _objectWithoutPropertiesLoose2.default)(_ref, _excluded); const shouldKeepStartDate = params.adapter.isValid(value[0]); const shouldKeepEndDate = params.adapter.isValid(value[1]); if (shouldKeepStartDate && shouldKeepEndDate) { return value; } const referenceDate = referenceDateProp ?? (0, _internals.getDefaultReferenceDate)(params); const startReferenceDate = Array.isArray(referenceDate) ? referenceDate[0] : referenceDate; const endReferenceDate = Array.isArray(referenceDate) ? referenceDate[1] : referenceDate; return [shouldKeepStartDate ? value[0] : startReferenceDate, shouldKeepEndDate ? value[1] : endReferenceDate]; }, cleanValue: (utils, value) => value.map(date => (0, _internals.replaceInvalidDateByNull)(utils, date)), areValuesEqual: (utils, a, b) => (0, _internals.areDatesEqual)(utils, a[0], b[0]) && (0, _internals.areDatesEqual)(utils, a[1], b[1]), isSameError: (a, b) => b !== null && a[1] === b[1] && a[0] === b[0], hasError: error => error[0] != null || error[1] != null, defaultErrorState: [null, null], getTimezone: (adapter, value) => { const timezoneStart = adapter.isValid(value[0]) ? adapter.getTimezone(value[0]) : null; const timezoneEnd = adapter.isValid(value[1]) ? adapter.getTimezone(value[1]) : null; if (timezoneStart != null && timezoneEnd != null && timezoneStart !== timezoneEnd) { throw new Error('MUI X: The timezone of the start and the end date should be the same.'); } return timezoneStart ?? timezoneEnd; }, setTimezone: (adapter, timezone, value) => [value[0] == null ? null : adapter.setTimezone(value[0], timezone), value[1] == null ? null : adapter.setTimezone(value[1], timezone)] }; const getRangeFieldValueManager = ({ dateSeparator = '–' }) => ({ updateReferenceValue: (adapter, value, prevReferenceValue) => { const shouldKeepStartDate = adapter.isValid(value[0]); const shouldKeepEndDate = adapter.isValid(value[1]); if (!shouldKeepStartDate && !shouldKeepEndDate) { return prevReferenceValue; } if (shouldKeepStartDate && shouldKeepEndDate) { return value; } if (shouldKeepStartDate) { return [value[0], prevReferenceValue[0]]; } return [prevReferenceValue[1], value[1]]; }, getSectionsFromValue: ([start, end], getSectionsFromDate) => { const getSections = (newDate, position) => { const sections = getSectionsFromDate(newDate); return sections.map((section, sectionIndex) => { if (sectionIndex === sections.length - 1 && position === 'start') { return (0, _extends2.default)({}, section, { dateName: position, // TODO: Check if RTL still works endSeparator: `${section.endSeparator} ${dateSeparator} ` }); } return (0, _extends2.default)({}, section, { dateName: position }); }); }; return [...getSections(start, 'start'), ...getSections(end, 'end')]; }, getV7HiddenInputValueFromSections: sections => { const dateRangeSections = (0, _dateFieldsUtils.splitDateRangeSections)(sections); return (0, _internals.createDateStrForV7HiddenInputFromSections)([...dateRangeSections.startDate, ...dateRangeSections.endDate]); }, getV6InputValueFromSections: (sections, localizedDigits, isRtl) => { const dateRangeSections = (0, _dateFieldsUtils.splitDateRangeSections)(sections); return (0, _internals.createDateStrForV6InputFromSections)([...dateRangeSections.startDate, ...dateRangeSections.endDate], localizedDigits, isRtl); }, parseValueStr: (valueStr, referenceValue, parseDate) => { // TODO: Improve because it would not work if some section have the same separator as the dateSeparator. const [startStr, endStr] = valueStr.split(dateSeparator); return [startStr, endStr].map((dateStr, index) => { if (dateStr == null) { return null; } return parseDate(dateStr.trim(), referenceValue[index]); }); }, getDateFromSection: (value, activeSection) => value[getActiveDateIndex(activeSection)], getDateSectionsFromValue: (sections, activeSection) => { const dateRangeSections = (0, _dateFieldsUtils.splitDateRangeSections)(sections); if (getActiveDateIndex(activeSection) === 0) { return (0, _dateFieldsUtils.removeLastSeparator)(dateRangeSections.startDate); } return dateRangeSections.endDate; }, updateDateInValue: (value, activeSection, activeDate) => { if (getActiveDateIndex(activeSection) === 0) { return [activeDate, value[1]]; } return [value[0], activeDate]; }, clearDateSections: (sections, activeSection) => { const dateRangeSections = (0, _dateFieldsUtils.splitDateRangeSections)(sections); if (getActiveDateIndex(activeSection) === 0) { return [...dateRangeSections.startDate.map(section => (0, _extends2.default)({}, section, { value: '' })), ...dateRangeSections.endDate]; } return [...dateRangeSections.startDate, ...dateRangeSections.endDate.map(section => (0, _extends2.default)({}, section, { value: '' }))]; } }); exports.getRangeFieldValueManager = getRangeFieldValueManager; function getActiveDateIndex(activeSection) { return activeSection == null || activeSection.dateName === 'start' ? 0 : 1; }