import _extends from "@babel/runtime/helpers/extends";
// Copyright 1999-2020. Plesk International GmbH. All rights reserved.

/* eslint-disable react/prefer-stateless-function */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { CLS_PREFIX } from '../../constants';
import FormField from '../FormField';
import Input from '../Input';
import TextArea from '../TextArea';

/**
 * `FormFieldText` component is used for entering of user data in text format.
 *
 * [FormField](#!/FormField) with [InputNumber](#!/InputNumber) can be used for entering of integer numbers.
 * @since 0.0.58
 */
class FormFieldText extends Component {
  render() {
    const {
      baseClassName,
      className,
      size,
      placeholder,
      multiline,
      rows,
      autoFocus,
      autoComplete,
      autoheight,
      ...props
    } = this.props;
    return /*#__PURE__*/React.createElement(FormField, _extends({
      className: classNames(baseClassName, {
        ["".concat(baseClassName, "--multiline")]: multiline
      }, className)
    }, props), ({
      getId,
      getName,
      getValue,
      setValue,
      isDisabled
    }) => multiline ? /*#__PURE__*/React.createElement(TextArea, {
      id: getId(),
      name: getName(),
      value: getValue(''),
      onChange: e => setValue(e.target.value),
      disabled: isDisabled(),
      size: size,
      rows: rows,
      placeholder: placeholder,
      autoFocus: autoFocus,
      autoComplete: autoComplete,
      autoheight: autoheight
    }) : /*#__PURE__*/React.createElement(Input, {
      id: getId(),
      name: getName(),
      className: "".concat(baseClassName, "__input"),
      value: getValue(''),
      onChange: e => setValue(e.target.value),
      disabled: isDisabled(),
      size: size,
      placeholder: placeholder,
      autoFocus: autoFocus,
      autoComplete: autoComplete
    }));
  }

}

FormFieldText.propTypes = {
  /**
   * Size of the input
   * @since 0.0.58
   */
  size: PropTypes.oneOf(['sm', 'md', 'lg', 'xl', 'fill']),

  /**
   * If `true`, a textarea element will be rendered.
   * @since 1.3.0
   */
  multiline: PropTypes.bool,

  /**
   * Number of rows to display when multiline option is set to true.
   * @since 1.3.0
   */
  rows: PropTypes.oneOfType([PropTypes.number.isRequired, PropTypes.shape({
    min: PropTypes.number.isRequired,
    max: PropTypes.number.isRequired
  })]),

  /**
   * Placeholder of the input
   * @since 0.0.58
   */
  placeholder: PropTypes.string,

  /**
   * Disabled state of field.
   * @since 0.2.0
   */
  disabled: PropTypes.bool,

  /**
   * The browser will automatically focus on the component upon rendering the screen
   * @since 1.9.0
   */
  autoFocus: PropTypes.bool,

  /**
   * Whether the value of the control can be automatically completed by the browser or not.
   * @since 2.5.1
   */
  autoComplete: PropTypes.oneOf(['on', 'off']),

  /**
   * If 'true', a field will be multiple
   * @since 1.12.0
   */
  multi: PropTypes.bool,

  /**
   * @ignore
   */
  className: PropTypes.string,

  /**
   * @ignore
   */
  baseClassName: PropTypes.string,

  /**
   * Adjust height automatically when multiline option is set to true.
   * @since 1.9.0
   */
  autoheight: PropTypes.bool
};
FormFieldText.defaultProps = {
  size: undefined,
  placeholder: undefined,
  disabled: undefined,
  multiline: false,
  rows: 4,
  autoFocus: undefined,
  autoComplete: undefined,
  multi: undefined,
  className: undefined,
  baseClassName: "".concat(CLS_PREFIX, "form-field-text"),
  autoheight: false
};
export default FormFieldText;