UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

90 lines (89 loc) 3.45 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _index = _interopRequireDefault(require("../String/index.js")); var _useTranslation = _interopRequireDefault(require("../../hooks/useTranslation.js")); var _Context = _interopRequireDefault(require("../../../../shared/Context.js")); var _index2 = require("../../Field/Date/index.js"); var _DateFormatUtils = require("../../../../components/date-format/DateFormatUtils.js"); var _DateOfBirth = require("../../Field/DateOfBirth/DateOfBirth.js"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } function DateComponent(props) { var _props$locale, _props$label; const translations = (0, _useTranslation.default)().Date; const { locale: contextLocale } = (0, _react.useContext)(_Context.default); const locale = (_props$locale = props.locale) !== null && _props$locale !== void 0 ? _props$locale : contextLocale; const { dateFormat, variant = 'long' } = props; const options = convertVariantToDateStyle(variant); const toInput = (0, _react.useCallback)(value => { if (!value) { return undefined; } const isRange = /\|/.test(value); if (isRange) { const [startDate, endDate] = (0, _index2.parseRangeValue)(value); return (0, _DateFormatUtils.formatDateRange)({ startDate, endDate }, { locale, options }); } if (dateFormat && dateFormat !== _DateOfBirth.DEFAULT_DATE_FORMAT) { return formatCustomDate(value, dateFormat); } return (0, _DateFormatUtils.formatDate)(value, { locale, options }); }, [locale, options, dateFormat]); const stringProps = { ...props, label: (_props$label = props.label) !== null && _props$label !== void 0 ? _props$label : translations.label, toInput }; return _react.default.createElement(_index.default, stringProps); } function convertVariantToDateStyle(variant) { if (variant === 'long') { return { dateStyle: 'long' }; } if (variant === 'short') { return { dateStyle: 'medium' }; } return { dateStyle: 'short' }; } function formatCustomDate(value, dateFormat) { try { const date = new Date(value); if (isNaN(date.getTime())) { return value; } const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); return dateFormat.replace('yyyy', year.toString()).replace('MM', month).replace('dd', day); } catch (error) { return value; } } DateComponent._supportsSpacingProps = true; var _default = exports.default = DateComponent; //# sourceMappingURL=Date.js.map