{"version":3,"file":"TextareaField.mjs","sources":["../../../src/components/TextareaField/TextareaField.tsx"],"sourcesContent":["import clsx from 'clsx';\nimport type { ReactNode } from 'react';\nimport React, { forwardRef, useState } from 'react';\nimport { getMinValue } from '../../util/getMinValue';\nimport type {\n  EitherInclusive,\n  ForwardedRefComponent,\n} from '../../util/utility-types';\n\nimport type { Status } from '../../util/variant-types';\nimport Counter from '../Counter';\nimport FieldLabel from '../FieldLabel';\nimport FieldNote from '../FieldNote';\nimport Text from '../Text';\n\nimport styles from './TextareaField.module.css';\n\ntype TextareaFieldProps = React.TextareaHTMLAttributes<HTMLTextAreaElement> & {\n  // Component API\n  /**\n   * Text content of the field upon instantiation\n   */\n  children?: string;\n  /**\n   * CSS class names that can be appended to the component.\n   */\n  className?: string;\n  /**\n   * Disables the field and prevents editing the contents\n   */\n  disabled?: boolean;\n  /**\n   * Text under the textarea used to provide validation hints or error message to describe the input error.\n   */\n  fieldNote?: ReactNode;\n  /**\n   * HTML id for the component. Can be used with a custom Label component\n   */\n  id?: string;\n  // Design API\n  /**\n   * Behaves similar to `maxLength` but allows the user to continue typing more text.\n   * Should not be larger than `maxLength`, if present.\n   */\n  recommendedMaxLength?: number;\n  /**\n   * Whether it should show the field hint or not\n   *\n   * **Default is `\"false\"`**.\n   */\n  showHint?: boolean;\n  /**\n   * Status for the field state\n   *\n   * **Default is `\"default\"`**.\n   */\n  status?: 'default' | Extract<Status, 'warning' | 'critical'>;\n  /**\n   * Add additional descriptive text for the field name.\n   */\n  subLabel?: ReactNode;\n} & EitherInclusive<\n    {\n      /**\n       * Visible text label for the component.\n       */\n      label: string;\n    },\n    {\n      /**\n       * Aria-label to provide an accesible name for the text input if no visible label is provided.\n       */\n      'aria-label': string;\n    }\n  >;\n\ntype TextareaFieldType = ForwardedRefComponent<\n  HTMLTextAreaElement,\n  TextareaFieldProps\n> & {\n  TextArea?: typeof TextArea;\n  Label?: typeof FieldLabel;\n};\n\ntype TextAreaProps = React.TextareaHTMLAttributes<HTMLTextAreaElement> & {\n  /**\n   * CSS class names that can be appended to the component\n   */\n  className?: string;\n  /**\n   * Text default contents of the field\n   */\n  children?: string;\n  /**\n   * Whether the disabled stat is active\n   */\n  disabled?: boolean;\n  /**\n   * Status for the field state\n   *\n   * **Default is `\"default\"`**.\n   */\n  status?: 'default' | Extract<Status, 'warning' | 'critical'>;\n};\n\n/**\n * Base component, applying styles to a <textarea> tag\n */\nconst TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(\n  ({ className, disabled, status = 'default', ...other }, ref) => {\n    const componentClassName = clsx(\n      styles['textarea'],\n      status === 'critical' && styles['error'],\n      status === 'warning' && styles['warning'],\n      className,\n    );\n\n    return (\n      <textarea\n        className={componentClassName}\n        disabled={disabled}\n        ref={ref}\n        {...other}\n      />\n    );\n  },\n);\n\n/**\n * ## Usage\n *\n * | Type/Use | Description | Example |\n * |----------|-------------|---------|\n * | Standard | Multi-line input field for entering extended text. | Comments, descriptions, notes fields. |\n * | Auto-resizing | Expands vertically as the user types. | Messaging apps, forms with flexible input length. |\n * | Fixed-size | Has a set number of rows and does not resize. | Forms with a consistent layout, limited-screen environments. |\n * | Read-only | Displays pre-filled content that cannot be edited. | Code blocks, audit logs, view-only mode. |\n * | Disabled | Grayed out and non-editable; indicates inaccessibility. | Conditional logic states, feature restrictions. |\n * | Monospaced | Uses a monospaced font, often for structured text or code. | Code snippets, JSON input, command editors. |\n *\n * **NOTE**: This component requires a `label` or `aria-label` prop to support accessibility.\n *\n * ## Interaction\n *\n * When the amount of text exceeds the available space, the text scrolls to show the end of the string.\n *\n * ## Content & Accessibility\n *\n * ### Do's\n *\n * * Always have a visible label when adding a hint.\n * * Use short, instructional labels when necessary and use sentence case.\n * * Use `fieldNote` helper text when necessary, and use examples rather than instructions whenever possible (e.g., `yourname@emaildomain.com`).\n * * For errors, provide instructions for fixing the issue and explain what is happening.\n * * Set `maxLength` on every textarea so there's a hard cap on what can be entered. Use `recommendedMaxLength` alongside it to show a lower, soft limit. Enforce the same limit on the server, since `maxLength` only constrains the browser.\n *\n * ### Don'ts\n *\n * * Don't use colons or periods at the end of labels, and don't omit labels.\n * * Don't have a hint without a visible label.\n * * Don't force the use of helper text—there is frequently no need for it.\n * * Don't place placeholder text within the field, as it can cause accessibility issues.\n */\nexport const TextareaField: TextareaFieldType = forwardRef(\n  (\n    {\n      'aria-describedby': ariaDescribedBy,\n      children,\n      className,\n      defaultValue,\n      disabled,\n      fieldNote,\n      id,\n      label,\n      maxLength,\n      onChange,\n      readOnly,\n      recommendedMaxLength,\n      required,\n      showHint,\n      status = 'default',\n      subLabel,\n      value,\n      ...other\n    },\n    ref,\n  ) => {\n    const [fieldText, setFieldText] = useState(defaultValue);\n\n    const shouldRenderOverline = !!(label || required);\n    const fieldLength = fieldText?.toString().length ?? 0;\n    const textExceedsMaxLength =\n      maxLength !== undefined ? fieldLength > maxLength : false;\n\n    const textExceedsRecommendedLength =\n      recommendedMaxLength !== undefined\n        ? fieldLength > recommendedMaxLength\n        : false;\n\n    const shouldRenderError =\n      status === 'critical' ||\n      textExceedsMaxLength ||\n      textExceedsRecommendedLength;\n\n    const overlineClassName = clsx(\n      styles['textarea-field__overline'],\n      !label && styles['textarea-field__overline--no-label'],\n    );\n\n    const labelClassName = clsx(\n      disabled && styles['textarea-field__label--disabled'],\n    );\n\n    const subLabelClassName = clsx(\n      styles['textarea-field__subLabel'],\n      disabled && styles['textarea-field__label--disabled'],\n    );\n\n    const requiredTextClassName = clsx(\n      styles['textarea-field__hint'],\n      disabled && styles['textarea-field__hint--disabled'],\n    );\n\n    // Accessibility: attach the IDs of fieldnote and/or subLabel to the input\n    const generatedIdVar = React.useId();\n    const idVar = id || generatedIdVar;\n    const generatedFieldNoteId = React.useId();\n    const generatedSubLabelId = React.useId();\n\n    // set up the aria-describedby based on the following rules:\n    // - describedby is blank if subLabel and fieldnote are not defined\n    // - for each subLabel/fieldnote, append the space-separated, generated IDs to aria-describedby\n    // - if the user has given a aria-describedby prop, override the calculation\n    const completeDescribedByVar = ariaDescribedBy\n      ? ariaDescribedBy\n      : `${subLabel ? generatedSubLabelId : ''}${fieldNote ? ' ' + generatedFieldNoteId : ''}`;\n\n    // Pick the smallest of the lengths to set as the maximum value allowed\n    const maxLengthShown = getMinValue(maxLength, recommendedMaxLength);\n\n    return (\n      <div className={className}>\n        {shouldRenderOverline && (\n          <div className={overlineClassName}>\n            {label && (\n              <FieldLabel className={labelClassName} htmlFor={idVar} size=\"md\">\n                {label}\n              </FieldLabel>\n            )}\n            {required && showHint && (\n              <Text\n                as=\"span\"\n                className={requiredTextClassName}\n                preset=\"body-sm\"\n              >\n                (Required)\n              </Text>\n            )}\n            {!required && showHint && (\n              <Text\n                as=\"span\"\n                className={requiredTextClassName}\n                preset=\"body-sm\"\n              >\n                (Optional)\n              </Text>\n            )}\n            {maxLengthShown && (\n              <Counter\n                className={styles['textarea-field__character-counter']}\n                count={fieldLength}\n                total={maxLengthShown}\n              />\n            )}\n            {label && subLabel && (\n              <div className={subLabelClassName}>\n                <Text as=\"span\" id={generatedSubLabelId} preset=\"body-sm\">\n                  {subLabel}\n                </Text>\n              </div>\n            )}\n          </div>\n        )}\n        <TextArea\n          aria-describedby={completeDescribedByVar ?? undefined}\n          aria-disabled={disabled}\n          defaultValue={defaultValue}\n          disabled={disabled}\n          id={idVar}\n          maxLength={maxLength}\n          onChange={(e) => {\n            setFieldText(e.target.value);\n            onChange && onChange(e);\n          }}\n          readOnly={readOnly}\n          ref={ref}\n          required={required}\n          status={shouldRenderError ? 'critical' : status}\n          value={value}\n          {...other}\n        >\n          {children}\n        </TextArea>\n        {(fieldNote || maxLengthShown) && (\n          <div className={styles['textarea-field__footer']}>\n            {fieldNote && (\n              <FieldNote\n                className={styles['textarea-field__field-note']}\n                disabled={disabled}\n                id={generatedFieldNoteId}\n                status={shouldRenderError ? 'critical' : status}\n              >\n                {fieldNote}\n              </FieldNote>\n            )}\n          </div>\n        )}\n      </div>\n    );\n  },\n);\n\nTextareaField.displayName = 'TextareaField';\nTextArea.displayName = 'TextareaField.TextArea';\n\nTextareaField.TextArea = TextArea;\nTextareaField.Label = FieldLabel;\n"],"names":["React"],"mappings":";;;;;;;;;AAyGA;;AAEG;AACH,MAAM,QAAQ,GAAG,UAAU,CACzB,CAAC,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,GAAG,SAAS,EAAE,GAAG,KAAK,EAAE,EAAE,GAAG,KAAI;AAC7D,IAAA,MAAM,kBAAkB,GAAG,IAAI,CAC7B,MAAM,CAAC,UAAU,CAAC,EAClB,MAAM,KAAK,UAAU,IAAI,MAAM,CAAC,OAAO,CAAC,EACxC,MAAM,KAAK,SAAS,IAAI,MAAM,CAAC,SAAS,CAAC,EACzC,SAAS,CACV;AAED,IAAA,QACEA,cAAA,CAAA,aAAA,CAAA,UAAA,EAAA,EACE,SAAS,EAAE,kBAAkB,EAC7B,QAAQ,EAAE,QAAQ,EAClB,GAAG,EAAE,GAAG,KACJ,KAAK,EAAA,CACT;AAEN,CAAC,CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCG;AACI,MAAM,aAAa,GAAsB,UAAU,CACxD,CACE,EACE,kBAAkB,EAAE,eAAe,EACnC,QAAQ,EACR,SAAS,EACT,YAAY,EACZ,QAAQ,EACR,SAAS,EACT,EAAE,EACF,KAAK,EACL,SAAS,EACT,QAAQ,EACR,QAAQ,EACR,oBAAoB,EACpB,QAAQ,EACR,QAAQ,EACR,MAAM,GAAG,SAAS,EAClB,QAAQ,EACR,KAAK,EACL,GAAG,KAAK,EACT,EACD,GAAG,KACD;;IACF,MAAM,CAAC,SAAS,EAAE,YAAY,CAAC,GAAG,QAAQ,CAAC,YAAY,CAAC;IAExD,MAAM,oBAAoB,GAAG,CAAC,EAAE,KAAK,IAAI,QAAQ,CAAC;AAClD,IAAA,MAAM,WAAW,GAAG,CAAA,EAAA,GAAA,SAAS,aAAT,SAAS,KAAA,MAAA,GAAA,MAAA,GAAT,SAAS,CAAE,QAAQ,E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