UNPKG

@navikt/ds-react

Version:

React components from the Norwegian Labour and Welfare Administration.

75 lines 4.87 kB
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 cl from "clsx"; import React, { forwardRef, useState } from "react"; import { useRenameCSS } from "../../theme/Theme.js"; import { BodyShort, ErrorMessage, Label } from "../../typography/index.js"; import { omit } from "../../util/index.js"; import TextareaAutosize from "../../util/TextareaAutoSize.js"; import { composeEventHandlers } from "../../util/composeEventHandlers.js"; import { useId } from "../../util/hooks/index.js"; import { ReadOnlyIcon } from "../ReadOnlyIcon.js"; import { useFormField } from "./../useFormField.js"; import Counter from "./TextareaCounter.js"; /** * A component that displays a textarea input field with a label. * * @see [📝 Documentation](https://aksel.nav.no/komponenter/core/textarea) * @see 🏷️ {@link TextareaProps} * * @example * ```jsx * <Textarea label="Har du noen tilbakemeldinger?" /> * ``` */ export const Textarea = forwardRef((props, ref) => { var _a, _b, _c; const { inputProps, errorId, showErrorMsg, hasError, size, inputDescriptionId, } = useFormField(props, "textarea"); const { label, className, description, maxLength, hideLabel = false, resize, UNSAFE_autoScrollbar, i18n, readOnly } = props, rest = __rest(props, ["label", "className", "description", "maxLength", "hideLabel", "resize", "UNSAFE_autoScrollbar", "i18n", "readOnly"]); const { cn } = useRenameCSS(); const maxLengthId = useId(); const hasMaxLength = maxLength !== undefined && maxLength > 0; const [uncontrolledValue, setUncontrolledValue] = useState((_a = props === null || props === void 0 ? void 0 : props.defaultValue) !== null && _a !== void 0 ? _a : ""); const getMinRows = () => { let rows = (rest === null || rest === void 0 ? void 0 : rest.minRows) ? rest === null || rest === void 0 ? void 0 : rest.minRows : 3; if (size === "small") { rows = (rest === null || rest === void 0 ? void 0 : rest.minRows) ? rest === null || rest === void 0 ? void 0 : rest.minRows : 2; } return rows; }; const describedBy = cl(inputProps["aria-describedby"], { [maxLengthId !== null && maxLengthId !== void 0 ? maxLengthId : ""]: hasMaxLength, }); return (React.createElement("div", { className: cn(className, "navds-form-field", `navds-form-field--${size}`, { "navds-form-field--disabled": !!inputProps.disabled, "navds-form-field--readonly": readOnly, "navds-textarea--readonly": readOnly, "navds-textarea--error": hasError, "navds-textarea--autoscrollbar": UNSAFE_autoScrollbar, [`navds-textarea--resize-${resize === true ? "both" : resize}`]: resize, }) }, 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(TextareaAutosize, Object.assign({}, omit(rest, ["error", "errorId", "size"]), inputProps, { onChange: composeEventHandlers(props.onChange, props.value === undefined ? (e) => setUncontrolledValue(e.target.value) : undefined), minRows: getMinRows(), autoScrollbar: UNSAFE_autoScrollbar, ref: ref, readOnly: readOnly, className: cn("navds-textarea__input", "navds-body-short", `navds-body-short--${size !== null && size !== void 0 ? size : "medium"}`) }, (describedBy ? { "aria-describedby": describedBy } : {}))), hasMaxLength && !readOnly && !inputProps.disabled && (React.createElement(Counter, { maxLengthId: maxLengthId, maxLength: maxLength, currentLength: (_c = (_b = props.value) === null || _b === void 0 ? void 0 : _b.length) !== null && _c !== void 0 ? _c : uncontrolledValue.length, size: size, i18n: i18n })), 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 Textarea; //# sourceMappingURL=Textarea.js.map