UNPKG

@mui/x-date-pickers

Version:

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

94 lines (84 loc) 2.88 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.AdapterDayjsBuddhist = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _dayjs = _interopRequireDefault(require("dayjs")); var _buddhistEra = _interopRequireDefault(require("dayjs/plugin/buddhistEra.js")); var _AdapterDayjs = require("../AdapterDayjs"); /* v8 ignore start */ // dayjs has no exports field defined // See https://github.com/iamkun/dayjs/issues/2562 /* eslint-disable import/extensions */ /* v8 ignore stop */ /* eslint-enable import/extensions */ _dayjs.default.extend(_buddhistEra.default); const BUDDHIST_YEAR_OFFSET = 543; const buddhistTokens = { BB: 'year', BBBB: { sectionType: 'year', contentType: 'digit', maxLength: 4 } }; const buddhistFormats = { year: 'BBBB', fullDate: 'D MMM BBBB', keyboardDate: 'DD/MM/BBBB', keyboardDateTime12h: 'DD/MM/BBBB hh:mm A', keyboardDateTime24h: 'DD/MM/BBBB HH:mm' }; /** * Adapter for dayjs with Buddhist Era calendar support. * Buddhist Era is used in Thailand and other Southeast Asian countries. * It uses the same months and days as the Gregorian calendar but with a year offset of 543. */ class AdapterDayjsBuddhist extends _AdapterDayjs.AdapterDayjs { constructor({ locale, formats } = {}) { super({ locale, formats: (0, _extends2.default)({}, buddhistFormats, formats) }); this.lib = 'dayjs-buddhist'; this.formatTokenMap = (0, _extends2.default)({}, this.formatTokenMap, buddhistTokens); } /** * Returns the Buddhist year (Gregorian year + 543) */ getYear = value => { return value.year() + BUDDHIST_YEAR_OFFSET; }; /** * Sets the Buddhist year (converts to Gregorian year internally) */ setYear = (value, year) => { return this.adjustOffset(value.set('year', year - BUDDHIST_YEAR_OFFSET)); }; /** * Parses a date string with Buddhist year support. * The buddhistEra plugin only supports formatting, not parsing, * so we convert BBBB/BB to YYYY/YY and adjust the year after parsing. */ parse = (value, format) => { if (value === '') { return null; } // Check if format contains Buddhist year tokens const hasBuddhistYear = /BBBB|BB/.test(format); // dayjs can't parse BBBB/BB tokens, replace with YYYY/YY const parseFormat = hasBuddhistYear ? format.replace(/BBBB/g, 'YYYY').replace(/BB/g, 'YY') : format; const parsed = (0, _dayjs.default)(value, parseFormat, this.locale, true); if (!parsed.isValid() || !hasBuddhistYear) { return parsed; } // Convert Buddhist year input to Gregorian return parsed.subtract(BUDDHIST_YEAR_OFFSET, 'year'); }; } exports.AdapterDayjsBuddhist = AdapterDayjsBuddhist;