@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
48 lines (47 loc) • 2.36 kB
JavaScript
"use client";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import React, { useCallback, useContext } from 'react';
import StringValue from '../String';
import useTranslation from '../../hooks/useTranslation';
import SharedContext from '../../../../shared/Context';
import { formatDate, formatDateRange } from '../../../../components/date-picker/DatePickerCalc';
import { parseRangeValue } from '../../Field/Date';
function DateComponent(props) {
var _props$locale, _props$variant, _props$label;
const translations = useTranslation().Date;
const {
locale: contextLocale
} = useContext(SharedContext);
const locale = (_props$locale = props.locale) !== null && _props$locale !== void 0 ? _props$locale : contextLocale;
const variant = (_props$variant = props.variant) !== null && _props$variant !== void 0 ? _props$variant : 'long';
const toInput = useCallback(value => {
if (!value) {
return undefined;
}
const isRange = /\|/.test(value);
if (isRange) {
const [startDate, endDate] = parseRangeValue(value);
return formatDateRange({
startDate,
endDate
}, {
locale,
variant
});
}
return formatDate(value, {
locale,
variant
});
}, [locale, variant]);
const stringProps = _objectSpread(_objectSpread({}, props), {}, {
label: (_props$label = props.label) !== null && _props$label !== void 0 ? _props$label : translations.label,
toInput
});
return React.createElement(StringValue, stringProps);
}
DateComponent._supportsSpacingProps = true;
export default DateComponent;
//# sourceMappingURL=Date.js.map