@navikt/ds-react
Version:
React components from the Norwegian Labour and Welfare Administration.
67 lines • 4.95 kB
JavaScript
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import React, { forwardRef, useRef } from "react";
import { CalendarIcon } from "@navikt/aksel-icons";
import { ReadOnlyIcon } from "../form/ReadOnlyIcon.js";
import { useFormField } from "../form/useFormField.js";
import { useRenameCSS } from "../theme/Theme.js";
import { BodyShort, ErrorMessage, Label } from "../typography/index.js";
import { omit } from "../util/index.js";
import { createContext } from "../util/create-context.js";
import { useDateTranslationContext } from "./Date.locale.js";
export const [DateInputContextProvider, useDateInputContext] = createContext({
errorMessage: "useDateInputContext must be used with DateInputContext",
});
const DateInput = forwardRef((props, ref) => {
const { className, hideLabel = false, label, description, variant = "datepicker", setAnchorRef } = props, rest = __rest(props, ["className", "hideLabel", "label", "description", "variant", "setAnchorRef"]);
const buttonRef = useRef(null);
const translate = useDateTranslationContext().translate;
const { cn } = useRenameCSS();
const isDatepickerVariant = variant === "datepicker";
const conditionalVariables = {
prefix: isDatepickerVariant ? "datepicker-input" : "monthpicker-input",
iconTitle: {
open: isDatepickerVariant ? "openDatePicker" : "openMonthPicker",
close: isDatepickerVariant ? "closeDatePicker" : "closeMonthPicker",
},
};
const context = useDateInputContext();
const { inputProps, size = "medium", inputDescriptionId, errorId, showErrorMsg, hasError, readOnly, } = useFormField(props, conditionalVariables.prefix);
return (React.createElement("div", { className: cn(className, "navds-form-field", `navds-form-field--${size}`, "navds-date__field", {
"navds-text-field--error": hasError,
"navds-date__field--error": hasError,
"navds-form-field--disabled": !!inputProps.disabled,
"navds-text-field--disabled": !!inputProps.disabled,
"navds-form-field--readonly": readOnly,
"navds-text-field--readonly": readOnly,
"navds-date__field--readonly": readOnly,
}) },
React.createElement(Label, { htmlFor: inputProps.id, size: size, className: cn("navds-form-field__label", {
"navds-sr-only": hideLabel,
}) },
readOnly && React.createElement(ReadOnlyIcon, null),
label),
!!description && (React.createElement(BodyShort, { as: "div", className: cn("navds-form-field__description", {
"navds-sr-only": hideLabel,
}), id: inputDescriptionId, size: size }, description)),
React.createElement("div", { className: cn("navds-date__field-wrapper") },
React.createElement("input", Object.assign({ ref: ref }, omit(rest, ["error", "errorId", "size"]), inputProps, { autoComplete: "off", "aria-controls": (context === null || context === void 0 ? void 0 : context.open) ? context.ariaId : undefined, readOnly: readOnly, className: cn("navds-date__field-input", "navds-text-field__input", "navds-body-short", `navds-body-short--${size}`), size: isDatepickerVariant ? 11 : 14 })),
React.createElement("button", { disabled: inputProps.disabled || readOnly, tabIndex: readOnly ? -1 : (context === null || context === void 0 ? void 0 : context.open) ? -1 : 0, onClick: () => {
context === null || context === void 0 ? void 0 : context.onOpen();
setAnchorRef === null || setAnchorRef === void 0 ? void 0 : setAnchorRef(buttonRef.current);
}, type: "button", className: cn("navds-date__field-button"), ref: buttonRef },
React.createElement(CalendarIcon, { title: translate(conditionalVariables.iconTitle[(context === null || context === void 0 ? void 0 : context.open) ? "close" : "open"]) }))),
React.createElement("div", { className: cn("navds-form-field__error"), id: errorId, "aria-relevant": "additions removals", "aria-live": "polite" }, showErrorMsg && (React.createElement(ErrorMessage, { size: size, showIcon: true }, props.error)))));
});
export const DatePickerInput = forwardRef((props, ref) => React.createElement(DateInput, Object.assign({}, props, { ref: ref })));
export const MonthPickerInput = forwardRef((props, ref) => React.createElement(DateInput, Object.assign({}, props, { variant: "monthpicker", ref: ref })));
//# sourceMappingURL=Date.Input.js.map