"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TextInput = TextInput;

var _react = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _2 = require("..");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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; }

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) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; 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 TextInput(_ref) {
  var _ref$type = _ref.type,
      type = _ref$type === void 0 ? 'input' : _ref$type,
      name = _ref.name,
      label = _ref.label,
      inputOnChange = _ref.onChange,
      onBlur = _ref.onBlur,
      id = _ref.id,
      value = _ref.value,
      defaultValue = _ref.defaultValue,
      disabled = _ref.disabled,
      status = _ref.status,
      small = _ref.small,
      optional = _ref.optional,
      hideOptionalText = _ref.hideOptionalText,
      srOnlyLabel = _ref.srOnlyLabel,
      className = _ref.className,
      inputProps = _ref.inputProps,
      inline = _ref.inline,
      beforeContent = _ref.beforeContent,
      afterContent = _ref.afterContent;

  var _useState = (0, _react.useState)(defaultValue),
      _useState2 = _slicedToArray(_useState, 2),
      _value = _useState2[0],
      updateValue = _useState2[1];

  var onChange = function onChange(e) {
    inputOnChange(e);

    if (!value) {
      // If there isn't an input value, then this component should manage its own value
      updateValue(e.target.value);
    }
  };

  var commonProps = {
    id: id,
    name: name,
    disabled: disabled,
    defaultValue: defaultValue,
    // Use the value if one is passed in; otherwise this manages its own state
    value: value || _value || '',
    'aria-describedby': status ? status.id : undefined,
    'aria-invalid': status && status.type === 'error',
    // Using aria-required to avoid default Chrome behavior
    'aria-required': !optional
  };
  var inputElement;

  switch (type) {
    case 'textarea':
      inputElement = /*#__PURE__*/_react.default.createElement("textarea", _extends({
        className: (0, _classnames.default)('usa-textarea'),
        onChange: onChange,
        onBlur: onBlur,
        "aria-describedby": status ? status.id : undefined,
        "aria-invalid": status && status.type === 'error'
      }, commonProps, inputProps));
      break;

    case 'inline-input':
      // For when the input needs additional context in addition to its label
      // i.e. "[input box] number of children"
      inputElement = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, beforeContent && /*#__PURE__*/_react.default.createElement("span", {
        className: "display-inline-flex padding-right-1"
      }, beforeContent), /*#__PURE__*/_react.default.createElement("input", _extends({
        className: (0, _classnames.default)('usa-input', 'display-inline-flex', _defineProperty({}, "usa-input--".concat(status && status.type), status), {
          'usa-input--small': small
        }, {
          'usa-input--inline': inline
        }),
        type: "text",
        onChange: onChange,
        onBlur: onChange
      }, commonProps, inputProps)), afterContent && /*#__PURE__*/_react.default.createElement("span", {
        className: "display-inline-flex padding-left-1"
      }, afterContent));
      break;

    case 'input':
    default:
      inputElement = /*#__PURE__*/_react.default.createElement("input", _extends({
        className: (0, _classnames.default)('usa-input', _defineProperty({}, "usa-input--".concat(status && status.type), status), {
          'usa-input--small': small
        }, {
          'usa-input--inline': inline
        }),
        type: "text",
        onChange: onChange,
        onBlur: onBlur
      }, commonProps, inputProps));
  }

  return /*#__PURE__*/_react.default.createElement("div", {
    className: "".concat(className || '', " usa-form-group").concat(status ? " usa-form-group--".concat(status.type) : '')
  }, /*#__PURE__*/_react.default.createElement("label", {
    className: (0, _classnames.default)('usa-label', status ? "usa-label--".concat(status.type) : '', {
      'usa-sr-only': srOnlyLabel
    }),
    htmlFor: id
  }, label, ' ', optional && !hideOptionalText && /*#__PURE__*/_react.default.createElement("span", {
    className: "usa-hint"
  }, "\xA0(optional)")), status && status.message && /*#__PURE__*/_react.default.createElement(_2.FormStatus, status), inputElement);
}