@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
52 lines (51 loc) • 1.69 kB
JavaScript
"use client";
import { useCallback, useContext } from 'react';
import StringValue from "../String/index.js";
import useTranslation from "../../hooks/useTranslation.js";
import SharedContext from "../../../../shared/Context.js";
import withComponentMarkers from "../../../../shared/helpers/withComponentMarkers.js";
import { jsx as _jsx } from "react/jsx-runtime";
function TimeComponent(props) {
var _props$locale, _props$label;
const translations = useTranslation().Time;
const {
locale: contextLocale
} = useContext(SharedContext);
const locale = (_props$locale = props.locale) !== null && _props$locale !== void 0 ? _props$locale : contextLocale;
const toInput = useCallback(value => {
if (!value) {
return undefined;
}
const parts = value.split(':');
if (parts.length < 2) {
return value;
}
const [hours, minutes, seconds] = parts;
if (hours === '24') {
return seconds ? `${hours}:${minutes}:${seconds}` : `${hours}:${minutes}`;
}
const date = new Date(1970, 0, 1);
date.setHours(Number(hours), Number(minutes), Number(seconds) || 0);
const options = {
hour: '2-digit',
minute: '2-digit',
...(seconds ? {
second: '2-digit'
} : {})
};
return new Intl.DateTimeFormat(locale, options).format(date);
}, [locale]);
const stringProps = {
...props,
label: (_props$label = props.label) !== null && _props$label !== void 0 ? _props$label : translations.label,
toInput
};
return _jsx(StringValue, {
...stringProps
});
}
withComponentMarkers(TimeComponent, {
_supportsSpacingProps: true
});
export default TimeComponent;
//# sourceMappingURL=Time.js.map