@navikt/ds-react
Version:
React components from the Norwegian Labour and Welfare Administration.
52 lines • 3.05 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 } from "react";
import { useRenameCSS } from "../../theme/Theme.js";
import { BodyShort, ErrorMessage, Label } from "../../typography/index.js";
import { omit } from "../../util/index.js";
import { ReadOnlyIcon } from "../ReadOnlyIcon.js";
import { useFormField } from "../useFormField.js";
/**
* A component that displays a text input field with a label.
*
* @see [📝 Documentation](https://aksel.nav.no/komponenter/core/textfield)
* @see 🏷️ {@link TextFieldProps}
*
* @example
* ```jsx
* <TextField label="Har du noen tilbakemeldinger?" />
* ```
*/
export const TextField = forwardRef((props, ref) => {
const { cn } = useRenameCSS();
const { inputProps, errorId, showErrorMsg, hasError, size, inputDescriptionId, } = useFormField(props, "textField");
const { label, className, description, htmlSize, hideLabel = false, type = "text", readOnly } = props, rest = __rest(props, ["label", "className", "description", "htmlSize", "hideLabel", "type", "readOnly"]);
return (React.createElement("div", { className: cn(className, "navds-form-field", `navds-form-field--${size}`, {
"navds-text-field--error": hasError,
"navds-text-field--disabled": !!inputProps.disabled,
"navds-form-field--disabled": !!inputProps.disabled,
"navds-form-field--readonly": readOnly,
"navds-text-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, { className: cn("navds-form-field__description", {
"navds-sr-only": hideLabel,
}), id: inputDescriptionId, size: size, as: "div" }, description)),
React.createElement("input", Object.assign({}, omit(rest, ["error", "errorId", "size"]), inputProps, { ref: ref, type: type, readOnly: readOnly, className: cn("navds-text-field__input", "navds-body-short", `navds-body-short--${size !== null && size !== void 0 ? size : "medium"}`), size: htmlSize })),
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 default TextField;
//# sourceMappingURL=TextField.js.map