function _extends() { _extends = Object.assign ? Object.assign.bind() : 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 _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 (c) Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
import * as React from 'react';
import { mergeOverrides, getOverrides } from '../helpers/overrides';
import { BaseInput, SIZE, CUSTOM_INPUT_TYPE } from '../input';
import { StyledTextAreaRoot, StyledTextarea, StyledTextareaContainer } from './styled-components';

class Textarea extends React.Component {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "state", {
      isFocused: this.props.autoFocus && !this.props.readOnly || false
    });

    _defineProperty(this, "onFocus", e => {
      if (!this.props.readOnly) {
        this.setState({
          isFocused: true
        });
        this.props.onFocus(e);
      }
    });

    _defineProperty(this, "onBlur", e => {
      if (!this.props.readOnly) {
        this.setState({
          isFocused: false
        });
        this.props.onBlur(e);
      }
    });
  }

  render() {
    const {
      overrides = {}
    } = this.props;
    const [Root, rootProps] = getOverrides(overrides.Root, StyledTextAreaRoot);
    const inputOverrides = mergeOverrides({
      Input: {
        component: StyledTextarea
      },
      InputContainer: {
        component: StyledTextareaContainer
      }
    }, overrides);
    return /*#__PURE__*/React.createElement(Root, _extends({
      "data-baseweb": "textarea",
      $isFocused: this.state.isFocused,
      $isReadOnly: this.props.readOnly,
      $disabled: this.props.disabled,
      $error: this.props.error,
      $positive: this.props.positive,
      $required: this.props.required,
      $resize: this.props.resize
    }, rootProps), /*#__PURE__*/React.createElement(BaseInput, _extends({}, this.props, {
      type: CUSTOM_INPUT_TYPE.textarea,
      overrides: inputOverrides,
      onFocus: this.onFocus,
      onBlur: this.onBlur,
      resize: this.props.resize
    })));
  }

}

_defineProperty(Textarea, "defaultProps", {
  autoFocus: false,
  disabled: false,
  readOnly: false,
  error: false,
  name: '',
  onBlur: () => {},
  onChange: () => {},
  onKeyDown: () => {},
  onKeyPress: () => {},
  onKeyUp: () => {},
  onFocus: () => {},
  overrides: {},
  placeholder: '',
  required: false,
  rows: 3,
  size: SIZE.default
});

export default Textarea;