UNPKG

@octopusdeploy/design-system-components

Version:
149 lines (148 loc) 9.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Textarea = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); const css_1 = require("@emotion/css"); const design_system_tokens_1 = require("@octopusdeploy/design-system-tokens"); const react_1 = require("react"); const FormDescription_1 = require("../Primitives/FormDescription"); const InputLabel_1 = require("../Primitives/InputLabel"); const InputValidationMessage_1 = require("../Primitives/InputValidationMessage"); const SharedFormStyling_styles_1 = require("../Primitives/SharedFormStyling.styles"); const formFieldMaxWidth_1 = require("../formFieldMaxWidth"); /** * Textarea component used for multi-line text input * * @remarks Only use in pre-approved areas, as in #project-form-uplift if unsure before using. * / /** * @param props - TextareaProps * @param props.label - The label for the textarea field * @param props.value - The current value of the textarea * @param props.placeholder - The placeholder text to display when textarea is empty * @param props.description - The description text to display below the textarea * @param props.validationMessage - Validation message to display * @param props.validationState - The validation state ('error' | 'success'). Only applied when a `validationMessage` is provided; defaults to 'error' in that case. * @param props.disabled - Whether the field is disabled * @param props.hasRequiredMarker - Whether the field is required * @param props.hasOptionalMarker - Whether to show optional marker * @param props.monospace - Whether to use a monospace font for the textarea * @param props.popover - PopoverBasicHelp component to display additional help information * @param props.autoFocus - Whether to autofocus the textarea * @param props.readOnly - Whether the textarea is readonly * @param props.name - The name attribute for the textarea * @param props.rows - The number of visible text lines for the textarea (Default is 4) * @param props.maxLength - The maximum number of characters allowed * @param props.minLength - The minimum number of characters required * @param props.autoResize - Whether the textarea should automatically resize * @param props.toolbar - Content rendered inside the field's border, above the textarea input (e.g. a formatting toolbar) * @param props.onChange - The action to perform when the form control field is changed. * @param ref - Forwarded to the underlying textarea element */ exports.Textarea = (0, react_1.forwardRef)(function Textarea({ label, value, placeholder, description, validationMessage, validationState, disabled = false, hasRequiredMarker = false, hasOptionalMarker = false, monospace = false, popover, autoFocus = false, readOnly = false, name, rows = 4, maxLength, minLength, autoResize = false, toolbar, onChange, }, ref) { const handleChange = (0, react_1.useCallback)((event) => { if (onChange) { onChange(event.target.value); } }, [onChange]); const baseId = (0, react_1.useId)(); const textareaId = `textarea-${name ? name + "-" : ""}${baseId}`; const validationDisplayState = validationMessage ? (validationState ?? "error") : "none"; const hasToolbar = Boolean(toolbar); const internalTextareaRef = (0, react_1.useRef)(null); const setTextareaElement = (0, react_1.useCallback)((element) => { internalTextareaRef.current = element; if (typeof ref === "function") { ref(element); } else if (ref) { ref.current = element; } }, [ref]); // Auto-resize functionality // NOTE: 29/10/25 This Javascript can eventually be replaced by field-sizing:content // when it's supported in Firefox and fully in Safari (0, react_1.useLayoutEffect)(() => { if (!autoResize || !internalTextareaRef.current) return; const textarea = internalTextareaRef.current; const adjustHeight = () => { textarea.style.height = "auto"; const borderHeight = textarea.offsetHeight - textarea.clientHeight; textarea.style.height = `${textarea.scrollHeight + borderHeight}px`; }; adjustHeight(); // Styles may not be fully applied when the effect first runs (e.g. on initial page load), // which skews the scrollHeight measurement — re-measure on the next frame. const rafId = requestAnimationFrame(adjustHeight); textarea.addEventListener("input", adjustHeight); return () => { cancelAnimationFrame(rafId); textarea.removeEventListener("input", adjustHeight); }; // hasToolbar affects the field's layout (border ownership and any consumer styling keyed on // the toolbar's presence), so re-measure when it changes. }, [autoResize, value, rows, hasToolbar]); return ((0, jsx_runtime_1.jsxs)("div", { className: containerStyles, children: [(0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)(InputLabel_1.InputLabel, { label: label, htmlFor: textareaId, isDisabled: disabled, hasRequiredMarker: hasRequiredMarker, hasOptionalMarker: hasOptionalMarker, popover: popover }), description && (0, jsx_runtime_1.jsx)(FormDescription_1.FormDescription, { id: `${textareaId}-description`, description: description, isDisabled: disabled })] }), (0, jsx_runtime_1.jsxs)("div", { className: (0, css_1.cx)(textareaContainerStyles, { [(0, css_1.cx)(SharedFormStyling_styles_1.fieldContainerStyles, toolbarClipStyles)]: hasToolbar, [SharedFormStyling_styles_1.fieldContainerErrorStyles]: hasToolbar && validationDisplayState === "error", [SharedFormStyling_styles_1.fieldContainerSuccessStyles]: hasToolbar && validationDisplayState === "success", [SharedFormStyling_styles_1.fieldContainerDisabledStyles]: hasToolbar && disabled, [SharedFormStyling_styles_1.fieldContainerReadOnlyStyles]: hasToolbar && readOnly, }), children: [toolbar, (0, jsx_runtime_1.jsx)("textarea", { ref: setTextareaElement, id: textareaId, name: name, value: value, placeholder: placeholder, disabled: disabled, readOnly: readOnly, "aria-readonly": readOnly, "aria-required": hasRequiredMarker, autoFocus: autoFocus, rows: rows, maxLength: maxLength, minLength: minLength, className: (0, css_1.cx)(SharedFormStyling_styles_1.inputStyles, textareaStyles, { [SharedFormStyling_styles_1.inputErrorStyles]: validationDisplayState === "error", [SharedFormStyling_styles_1.inputSuccessStyles]: validationDisplayState === "success", [SharedFormStyling_styles_1.inputDisabledStyles]: disabled, [SharedFormStyling_styles_1.inputReadOnlyStyles]: readOnly, [textareaAutoResizeStyles]: autoResize, [monospaceStyles]: monospace, }, hasToolbar && textareaInsideFieldStyles), onChange: handleChange, "aria-invalid": validationDisplayState === "error", "aria-describedby": [description ? `${textareaId}-description` : null, validationMessage ? `${textareaId}-validation` : null].filter(Boolean).join(" ") || undefined })] }), (0, jsx_runtime_1.jsx)(InputValidationMessage_1.InputValidationMessage, { message: validationMessage, displayState: validationDisplayState, id: `${textareaId}-validation` })] })); }); exports.default = exports.Textarea; const containerStyles = (0, css_1.css)({ display: "grid", gap: design_system_tokens_1.space[8], width: "100%", maxWidth: formFieldMaxWidth_1.formFieldMaxWidth, }); const textareaContainerStyles = (0, css_1.css)({ position: "relative", display: "grid", }); // When a toolbar is slotted in, the field container takes over the border, focus, and state // treatment (via the shared fieldContainer* styles); this clips the toolbar background to the // container's rounded corners. const toolbarClipStyles = (0, css_1.css)({ overflow: "hidden", }); // The field container draws the border when a toolbar is present, so the textarea itself goes borderless. const textareaInsideFieldStyles = (0, css_1.css)({ border: "none", borderRadius: 0, "&:focus, &:focus-visible": { outline: "none", }, }); const textareaStyles = (0, css_1.css)({ boxSizing: "border-box", padding: design_system_tokens_1.space[8], border: `${design_system_tokens_1.borderWidth[1]} solid ${design_system_tokens_1.themeTokens.color.border.bold}`, borderRadius: design_system_tokens_1.borderRadius.small, backgroundColor: design_system_tokens_1.themeTokens.color.background.primary.default, color: design_system_tokens_1.themeTokens.color.text.primary, width: "100%", resize: "vertical", height: "auto", minHeight: "2.625rem", // Height of a single row of text at the defined font size // Focus treatment comes from inputStyles' :focus-visible rule, matching TextField and Select. "&::placeholder": { color: design_system_tokens_1.themeTokens.color.text.tertiary, }, }); const monospaceStyles = (0, css_1.css)({ font: design_system_tokens_1.text.code.regular.medium, }); const textareaAutoResizeStyles = (0, css_1.css)({ resize: "none", overflow: "hidden", });