{"version":3,"file":"FormTextarea.mjs","sources":["../../../src/lib/components/FormTextarea/FormTextarea.jsx"],"sourcesContent":["/*\nCopyright 2022 Iguazio Systems Ltd.\nLicensed under the Apache License, Version 2.0 (the \"License\") with\nan addition restriction as set forth herein. You may not use this\nfile except in compliance with the License. You may obtain a copy of\nthe License at http://www.apache.org/licenses/LICENSE-2.0.\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\nimplied. See the License for the specific language governing\npermissions and limitations under the License.\nIn addition, you may not use the software for any purposes that are\nillegal under applicable law, and the grant of the foregoing license\nunder the Apache 2.0 license is conditioned upon your compliance with\nsuch restriction.\n*/\nimport React, { forwardRef, useEffect, useLayoutEffect, useRef, useState } from 'react'\nimport classnames from 'classnames'\nimport PropTypes from 'prop-types'\nimport { Field, useField } from 'react-final-form'\n\nimport TextTooltipTemplate from '../TooltipTemplate/TextTooltipTemplate'\nimport Tip from '../Tip/Tip'\nimport Tooltip from '../Tooltip/Tooltip'\n\nimport ExclamationMarkIcon from '../../images/exclamation-mark.svg?react'\n\nimport './formTextarea.scss'\n\nlet FormTextarea = (\n  {\n    className = '',\n    disabled = false,\n    focused = false,\n    iconClass = '',\n    invalidText = 'This field is invalid',\n    label = '',\n    maxLength = null,\n    name,\n    onBlur = () => {},\n    onChange = () => {},\n    required = false,\n    rows = 3,\n    textAreaIcon = null,\n    tip = '',\n    withoutBorder = false,\n    ...textareaProps\n  },\n  ref\n) => {\n  const { input, meta } = useField(name)\n  const [isInvalid, setIsInvalid] = useState(false)\n  const [textAreaCount, setTextAreaCount] = useState(input.value.length)\n  const textAreaRef = useRef()\n\n  const formFieldClassNames = classnames('form-field-textarea', className)\n  const labelClassNames = classnames('form-field__label', disabled && 'form-field__label-disabled')\n  const textAreaClassNames = classnames(\n    'form-field__wrapper',\n    disabled && 'form-field__wrapper-disabled',\n    isInvalid && 'form-field__wrapper-invalid',\n    withoutBorder && 'without-border'\n  )\n\n  useLayoutEffect(() => {\n    setTextAreaCount(input.value.length)\n  }, [input.value.length])\n\n  useEffect(() => {\n    if (focused) {\n      textAreaRef.current.focus()\n    }\n  }, [focused, textAreaRef])\n\n  useEffect(() => {\n    setIsInvalid(\n      meta.invalid && (meta.validating || meta.modified || (meta.submitFailed && meta.touched))\n    )\n  }, [meta.invalid, meta.modified, meta.submitFailed, meta.touched, meta.validating])\n\n  const handleInputBlur = event => {\n    input.onBlur(event)\n    onBlur && onBlur(event)\n  }\n\n  const handleInputChange = event => {\n    input.onChange(event)\n    onChange && onChange(event.target.value)\n  }\n\n  const handleInputFocus = event => {\n    input.onFocus(event)\n  }\n\n  const validateField = value => {\n    const valueToValidate = value ?? ''\n    let validationError = null\n\n    if (valueToValidate.startsWith(' ')) {\n      validationError = { name: 'empty', label: invalidText }\n    } else if (required && valueToValidate.trim().length === 0) {\n      validationError = { name: 'required', label: 'This field is required' }\n    }\n\n    return validationError\n  }\n\n  return (\n    <Field validate={validateField} name={name}>\n      {({ input, meta }) => (\n        <div ref={ref} className={formFieldClassNames}>\n          <div className={labelClassNames}>\n            {label && (\n              <label data-testid=\"label\" htmlFor={input.name}>\n                {label}\n                {required && <span className=\"form-field__label-mandatory\"> *</span>}\n              </label>\n            )}\n          </div>\n          <div className={textAreaClassNames}>\n            <div className=\"form-field__control\">\n              <textarea\n                data-testid=\"textarea\"\n                id={input.name}\n                maxLength={maxLength}\n                ref={textAreaRef}\n                required={isInvalid || required}\n                {...{\n                  disabled,\n                  rows,\n                  ...textareaProps,\n                  ...input\n                }}\n                onBlur={handleInputBlur}\n                onChange={handleInputChange}\n                onFocus={handleInputFocus}\n              />\n            </div>\n            <div className=\"form-field__icons\">\n              {isInvalid && (\n                <Tooltip\n                  className=\"form-field__warning\"\n                  template={<TextTooltipTemplate text={meta.error?.label ?? invalidText} warning />}\n                >\n                  <ExclamationMarkIcon />\n                </Tooltip>\n              )}\n              {tip && !required && <Tip text={tip} className=\"form-field__tip\" />}\n              {textAreaIcon && (\n                <span data-testid=\"textarea__icon\" className={iconClass}>\n                  {textAreaIcon}\n                </span>\n              )}\n            </div>\n          </div>\n          {maxLength && (\n            <div className=\"form-field__counter\">{`${maxLength - textAreaCount} ${\n              maxLength - textAreaCount !== 1 ? 'characters' : 'character'\n            } left`}</div>\n          )}\n        </div>\n      )}\n    </Field>\n  )\n}\n\nFormTextarea = React.memo(forwardRef(FormTextarea))\n\nFormTextarea.displayName = 'FormTextarea'\n\nFormTextarea.propTypes = {\n  className: PropTypes.string,\n  disabled: PropTypes.bool,\n  focused: PropTypes.bool,\n  iconClass: PropTypes.string,\n  invalidText: PropTypes.string,\n  label: PropTypes.string,\n  maxLength: PropTypes.number,\n  name: PropTypes.string.isRequired,\n  onBlur: PropTypes.func,\n  onChange: PropTypes.func,\n  required: PropTypes.bool,\n  rows: PropTypes.number,\n  textAreaIcon: PropTypes.element,\n  tip: PropTypes.string,\n  withoutBorder: PropTypes.bool\n}\n\nexport default FormTextarea\n"],"names":["FormTextarea","className","disabled","focused","iconClass","invalidText","label","maxLength","name","onBlur","onChange","required","rows","textAreaIcon","tip","withoutBorder","textareaProps","ref","input","meta","useField","isInvalid","setIsInvalid","useState","textAreaCount","setTextAreaCount","textAreaRef","useRef","formFieldClassNames","classnames","labelClassNames","textAreaClassNames","useLayoutEffect","useEffect","handleInputBlur","event","handleInputChange","handleInputFocus","Field","value","valueToValidate","validationError","jsxs","jsx","Tooltip","TextTooltipTemplate","ExclamationMarkIcon","Tip","React","forwardRef","PropTypes","FormTextarea_default"],"mappings":";;;;;;;;;;AA6BA,IAAIA,IAAe,CACjB;AAAA,EACE,WAAAC,IAAY;AAAA,EACZ,UAAAC,IAAW;AAAA,EACX,SAAAC,IAAU;AAAA,EACV,WAAAC,IAAY;AAAA,EACZ,aAAAC,IAAc;AAAA,EACd,OAAAC,IAAQ;AAAA,EACR,WAAAC,IAAY;AAAA,EACZ,MAAAC;AAAA,EACA,QAAAC,IAAS,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