text: function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
  
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
  
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
  
  function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  
  function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
  
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
  
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  
  /**
   * Copyright IBM Corp. 2016, 2018
   *
   * This source code is licensed under the Apache-2.0 license found in the
   * LICENSE file in the root directory of this source tree.
   */
  import PropTypes from 'prop-types';
  import React, { useState } from 'react';
  import classNames from 'classnames';
  import { settings } from 'carbon-components';
  import WarningFilled16 from '@carbon/icons-react/lib/warning--filled/16';
  var prefix = settings.prefix;
  
  var DefaultCharCounter = function DefaultCharCounter(_ref) {
    var disabled = _ref.disabled,
        count = _ref.count,
        maxLength = _ref.maxLength;
    var charCounterClasses = classNames("".concat(prefix, "--text-area--character-counter"), _defineProperty({}, "".concat(prefix, "--text-area--character-counter--disabled"), disabled));
    return React.createElement("span", {
      className: charCounterClasses
    }, React.createElement("span", {
      className: "".concat(prefix, "--text-area--character-counter--length")
    }, count), "/", React.createElement("span", {
      className: "".concat(prefix, "--text-area--character-counter--maxlength")
    }, maxLength));
  };
  
  var TextArea = function TextArea(_ref2) {
    var _classNames2, _classNames4;
  
    var className = _ref2.className,
        id = _ref2.id,
        labelText = _ref2.labelText,
        hideLabel = _ref2.hideLabel,
        _onChange = _ref2.onChange,
        _onClick = _ref2.onClick,
        invalid = _ref2.invalid,
        invalidText = _ref2.invalidText,
        helperText = _ref2.helperText,
        light = _ref2.light,
        charCount = _ref2.charCount,
        maxLength = _ref2.maxLength,
        _ref2$renderCharCount = _ref2.renderCharCounter,
        CharCounter = _ref2$renderCharCount === void 0 ? DefaultCharCounter : _ref2$renderCharCount,
        other = _objectWithoutProperties(_ref2, ["className", "id", "labelText", "hideLabel", "onChange", "onClick", "invalid", "invalidText", "helperText", "light", "charCount", "maxLength", "renderCharCounter"]);
  
    var _useState = useState(''),
        _useState2 = _slicedToArray(_useState, 2),
        textareaVal = _useState2[0],
        setInput = _useState2[1];
  
    var textareaProps = {
      id: id,
      onChange: function onChange(evt) {
        if (!other.disabled) {
          _onChange(evt);
        }
      },
      onClick: function onClick(evt) {
        if (!other.disabled) {
          _onClick(evt);
        }
      },
      maxLength: maxLength || null
    };
    var labelClasses = classNames("".concat(prefix, "--label"), (_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefix, "--visually-hidden"), hideLabel), _defineProperty(_classNames2, "".concat(prefix, "--label--disabled"), other.disabled), _classNames2));
  
    var label = function () {
      var labelContent = labelText ? React.createElement("label", {
        htmlFor: id,
        className: labelClasses
      }, labelText) : null;
  
      if (labelContent && charCount) {
        return React.createElement("div", {
          className: "".concat(prefix, "--text-area__character-counter-title")
        }, labelContent, React.createElement(CharCounter, {
          disabled: other.disabled,
          count: textareaVal.length,
          maxLength: maxLength
        }));
      }
  
      return labelContent;
    }();
  
    var helperTextClasses = classNames("".concat(prefix, "--form__helper-text"), _defineProperty({}, "".concat(prefix, "--form__helper-text--disabled"), other.disabled));
  
    var helper = function () {
      var helperContent = helperText ? React.createElement("div", {
        className: helperTextClasses
      }, helperText) : null;
  
      if (!labelText && charCount) {
        return React.createElement("div", {
          className: "".concat(prefix, "--text-area__character-counter-title")
        }, helperContent, React.createElement(CharCounter, {
          disabled: other.disabled,
          count: textareaVal.length,
          maxLength: maxLength
        }));
      }
  
      return helperContent;
    }();
  
    var errorId = id + '-error-msg';
    var error = invalid ? React.createElement("div", {
      className: "".concat(prefix, "--form-requirement"),
      id: errorId
    }, invalidText) : null;
    var textareaClasses = classNames("".concat(prefix, "--text-area"), className, (_classNames4 = {}, _defineProperty(_classNames4, "".concat(prefix, "--text-area--light"), light), _defineProperty(_classNames4, "".concat(prefix, "--text-area--invalid"), invalid), _classNames4));
    var input = React.createElement("textarea", _extends({}, other, textareaProps, {
      className: textareaClasses,
      "aria-invalid": invalid || null,
      "aria-describedby": invalid ? errorId : null,
      disabled: other.disabled,
      value: textareaVal,
      onInput: function onInput(e) {
        return setInput(e.target.value);
      }
    }));
    return React.createElement("div", {
      className: "".concat(prefix, "--form-item")
    }, label, helper, React.createElement("div", {
      className: "".concat(prefix, "--text-area__wrapper"),
      "data-invalid": invalid || null
    }, invalid && React.createElement(WarningFilled16, {
      className: "".concat(prefix, "--text-area__invalid-icon")
    }), input), error);
  };
  
  TextArea.propTypes = {
    /**
     * Provide a custom className that is applied directly to the underlying
     * <textarea> node
     */
    className: PropTypes.string,
  
    /**
     * Specify the `cols` attribute for the underlying <textarea> node
     */
    cols: PropTypes.number,
  
    /**
     * Optionally provide the default value of the <textarea>
     */
    defaultValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
  
    /**
     * Specify whether the control is disabled
     */
    disabled: PropTypes.bool,
  
    /**
     * Provide a unique identifier for the control
     */
    id: PropTypes.string,
  
    /**
     * Provide the text that will be read by a screen reader when visiting this
     * control
     */
    labelText: PropTypes.node.isRequired,
  
    /**
     * Optionally provide an `onChange` handler that is called whenever <textarea>
     * is updated
     */
    onChange: PropTypes.func,
  
    /**
     * Optionally provide an `onClick` handler that is called whenever the
     * <textarea> is clicked
     */
    onClick: PropTypes.func,
  
    /**
     * Specify the placeholder attribute for the <textarea>
     */
    placeholder: PropTypes.string,
  
    /**
     * Specify the rows attribute for the <textarea>
     */
    rows: PropTypes.number,
  
    /**
     * Provide the current value of the <textarea>
     */
    value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
  
    /**
     * Specify whether the control is currently invalid
     */
    invalid: PropTypes.bool,
  
    /**
     * Provide the text that is displayed when the control is in an invalid state
     */
    invalidText: PropTypes.string,
  
    /**
     * Provide text that is used alongside the control label for additional help
     */
    helperText: PropTypes.node,
  
    /**
     * Specify whether you want the underlying label to be visually hidden
     */
    hideLabel: PropTypes.bool,
  
    /**
     * Specify whether you want the light version of this control
     */
    light: PropTypes.bool,
  
    /**
     * Specify whether the character counter is shown
     */
    charCount: PropTypes.bool,
  
    /**
     * The maximum allowed input value length
     */
    maxLength: PropTypes.number
  };
  TextArea.defaultProps = {
    disabled: false,
    onChange: function onChange() {},
    onClick: function onClick() {},
    placeholder: '',
    rows: 4,
    cols: 50,
    invalid: false,
    invalidText: '',
    helperText: '',
    light: false
  };
  export default TextArea;