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

import _pt from "prop-types";
var _excluded = ["mask", "labelMask"];

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 _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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

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

import React from 'react';
import LabelMask from './LabelMask';
import Mask from './Mask';
import TextInput from './TextInput';
import classNames from 'classnames';
import omit from 'lodash/omit';
import pick from 'lodash/pick';
import { FormControl, FormControlPropKeys } from '../FormControl/FormControl'; // TODO: Remove `maskValue` and `unmaskValue` exports with next major release (v3.x.x)

export { unmaskValue } from './maskHelpers';
export var TextField = /*#__PURE__*/function (_React$PureComponent) {
  _inherits(TextField, _React$PureComponent);

  var _super = _createSuper(TextField);

  function TextField(props) {
    var _this;

    _classCallCheck(this, TextField);

    _this = _super.call(this, props);

    if (process.env.NODE_ENV !== 'production') {
      if (props.type === 'number') {
        console.warn("Please use the 'numeric' prop instead of 'type=\"number\"' unless your user research suggests otherwise.");
      }
    }

    return _this;
  }

  _createClass(TextField, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var containerProps = pick(this.props, FormControlPropKeys);

      var _omit = omit(this.props, FormControlPropKeys),
          mask = _omit.mask,
          labelMask = _omit.labelMask,
          inputOnlyProps = _objectWithoutProperties(_omit, _excluded); // Add clearfix class


      var containerClassName = classNames('ds-u-clearfix', // fixes issue where the label's margin is collapsed
      this.props.className);
      return /*#__PURE__*/React.createElement(FormControl, _extends({}, containerProps, {
        className: containerClassName,
        component: "div",
        labelComponent: "label",
        label: this.props.label,
        render: function render(_ref) {
          var id = _ref.id,
              setRef = _ref.setRef,
              errorId = _ref.errorId,
              errorMessage = _ref.errorMessage,
              errorPlacement = _ref.errorPlacement;
          var input = /*#__PURE__*/React.createElement(TextInput, _extends({
            type: TextField.defaultProps.type // Appeases TypeScript

          }, inputOnlyProps, {
            id: id,
            setRef: setRef,
            errorId: errorId,
            errorMessage: errorMessage,
            errorPlacement: errorPlacement,
            inversed: _this2.props.inversed
          }));

          if (mask) {
            return /*#__PURE__*/React.createElement(Mask, {
              mask: mask
            }, input);
          } else if (labelMask) {
            return /*#__PURE__*/React.createElement(LabelMask, {
              labelMask: labelMask
            }, input);
          } else {
            return input;
          }
        }
      }));
    }
  }]);

  return TextField;
}(React.PureComponent);

_defineProperty(TextField, "propTypes", {
  /**
     * Apply an `aria-label` to the text field to provide additional
     * context to assistive devices.
     */
  ariaLabel: _pt.string,

  /**
     * Additional classes to be added to the root `div` element
     */
  className: _pt.string,

  /**
     * Sets the initial value. Use this for an uncontrolled component; otherwise,
     * use the `value` property.
     */
  defaultValue: _pt.oneOfType([_pt.string, _pt.number]),
  disabled: _pt.bool,
  errorMessage: _pt.node,

  /**
     * Additional classes to be added to the error message
     */
  errorMessageClassName: _pt.string,

  /**
     * Location of the error message relative to the field input
     */
  errorPlacement: _pt.oneOf(['top', 'bottom']),

  /**
     * Additional classes to be added to the field element
     */
  fieldClassName: _pt.string,

  /**
     * Used to focus `input` on `componentDidMount()`
     */
  focusTrigger: _pt.bool,

  /**
     * Additional hint text to display
     */
  hint: _pt.node,

  /**
     * A unique `id` to be used on the text field.
     */
  id: _pt.string,

  /**
     * Access a reference to the `input` or `textarea` element
     */
  inputRef: _pt.func,

  /**
     * Text showing the requirement ("Required", "Optional", etc.). See [Required and Optional Fields]({{root}}/guidelines/forms/#required-and-optional-fields).
     */
  requirementLabel: _pt.node,

  /**
     * Applies the "inverse" UI theme
     */
  inversed: _pt.bool,

  /**
     * Label for the input
     */
  label: _pt.node.isRequired,

  /**
     * Applies date format masking to the input value entered
     * and renders to a text field above the input.
     * Passing `true` to `valueOnly` will return just the
     * formatted value entered.
     */
  labelMask: _pt.func,

  /**
     * Additional classes to be added to the `FormLabel`.
     */
  labelClassName: _pt.string,

  /**
     * A unique `id` to be used on the label field.
     */
  labelId: _pt.string,

  /**
     * Apply formatting to the field that's unique to the value
     * you expect to be entered. Depending on the mask, the
     * field's appearance and functionality may be affected.
     */
  mask: _pt.oneOf(['currency', 'phone', 'ssn', 'zip']),

  /**
     * Whether or not the text field is a multiline text field
     */
  multiline: _pt.bool,
  name: _pt.string.isRequired,

  /**
     * Sets `inputMode`, `type`, and `pattern` to improve accessibility and consistency for number fields. Use this prop instead of `type="number"`, see [here](https://technology.blog.gov.uk/2020/02/24/why-the-gov-uk-design-system-team-changed-the-input-type-for-numbers/) for more information.
     */
  numeric: _pt.bool,
  onBlur: _pt.func,
  onChange: _pt.func,

  /**
     * @hide-prop HTML `input` [pattern](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefpattern).
     */
  pattern: _pt.string
});

_defineProperty(TextField, "defaultProps", {
  type: 'text'
});

export default TextField;