UNPKG

@octopusdeploy/design-system-components

Version:
145 lines (144 loc) 8.38 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.Textarea = Textarea; 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 = __importStar(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"); /** * Textarea component used for multi-line text input * * @remarks Rollout currently paused — do not use in new code until further notice. * Use `<Text>` instead. * @see <Text> * / /** * @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.onChange - The action to perform when the form control field is changed. */ 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, onChange, }) { const handleChange = React.useCallback((event) => { if (onChange) { onChange(event.target.value); } }, [onChange]); const baseId = React.useId(); const textareaId = `textarea-${name ? name + "-" : ""}${baseId}`; const validationDisplayState = validationMessage ? (validationState ?? "error") : "none"; const textareaRef = React.useRef(null); // 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 React.useLayoutEffect(() => { if (!autoResize || !textareaRef.current) return; const textarea = textareaRef.current; const adjustHeight = () => { textarea.style.height = "auto"; textarea.style.height = `${textarea.scrollHeight}px`; }; adjustHeight(); textarea.addEventListener("input", adjustHeight); return () => textarea.removeEventListener("input", adjustHeight); }, [autoResize, value]); 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.jsx)("div", { className: (0, css_1.cx)(textareaContainerStyles), children: (0, jsx_runtime_1.jsx)("textarea", { ref: textareaRef, 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)(textareaStyles, SharedFormStyling_styles_1.inputStyles, { [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, }), 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 = Textarea; const containerStyles = (0, css_1.css)({ display: "grid", gap: design_system_tokens_1.space[8], width: "100%", maxWidth: "600px", }); const textareaContainerStyles = (0, css_1.css)({ position: "relative", display: "grid", }); 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", minHeight: "2.625rem", // Height of a single row of text at the defined font size "&:focus": { outline: `${design_system_tokens_1.borderWidth[2]} solid ${design_system_tokens_1.themeTokens.color.border.selected}`, outlineOffset: "0", borderColor: design_system_tokens_1.themeTokens.color.border.selected, }, "&::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", });