"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.default = exports.FormControlPropKeys = exports.FormControl = void 0;

var _propTypes = _interopRequireDefault(require("prop-types"));

var _FormLabel = _interopRequireDefault(require("../FormLabel/FormLabel"));

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

var _useFormLabel2 = _interopRequireDefault(require("../FormLabel/useFormLabel"));

var _excluded = ["component", "render", "inputRef"];

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(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; }

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

var FormControl = function FormControl(props) {
  var focusRef = (0, _react.useRef)(); // TODO: Make a hook out of this
  // Automatically set focus on field input element when `focusTrigger` prop is used

  (0, _react.useEffect)(function () {
    if (props.focusTrigger && focusRef.current) {
      focusRef.current.focus();
    }
  }, [focusRef, props.focusTrigger]);

  var component = props.component,
      render = props.render,
      inputRef = props.inputRef,
      remainingProps = _objectWithoutProperties(props, _excluded);

  var ComponentType = component;
  var wrapperIsFieldset = ComponentType === 'fieldset';

  var _useFormLabel = (0, _useFormLabel2.default)(_objectSpread(_objectSpread({}, remainingProps), {}, {
    wrapperIsFieldset: wrapperIsFieldset
  })),
      labelProps = _useFormLabel.labelProps,
      fieldProps = _useFormLabel.fieldProps,
      wrapperProps = _useFormLabel.wrapperProps,
      bottomError = _useFormLabel.bottomError;

  function setRef(el) {
    focusRef.current = el;

    if (inputRef) {
      inputRef(el);
    }
  } // Field input props handled by <FormControl>
  // TODO: Move `setRef` logic into <TextField> and <Select>
  // TODO: Simplify `id` props by using a singular `fieldId` prop, and a consistent id naming convention
  // TODO: Use React Context to provide shared form props like `errorPlacement`, `inversed`, `fieldId`


  var fieldInputProps = _objectSpread(_objectSpread({}, fieldProps), {}, {
    labelId: labelProps.id,
    setRef: setRef
  });

  return /*#__PURE__*/_react.default.createElement(ComponentType, wrapperProps, /*#__PURE__*/_react.default.createElement(_FormLabel.default, labelProps), render(fieldInputProps), bottomError);
};

exports.FormControl = FormControl;
FormControl.propTypes = {
  /**
     * Additional classes to be added to the field container.
     */
  className: _propTypes.default.string,

  /**
     * The HTML element used to render the container
     */
  component: _propTypes.default.oneOf(['div', 'fieldset']).isRequired,

  /**
     * A unique ID to be used for the error message. If one isn't provided, a unique ID will be generated.
     */
  errorId: _propTypes.default.string,
  errorMessage: _propTypes.default.node,

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

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

  /**
     * Used to focus the field input on `componentDidMount()`
     */
  focusTrigger: _propTypes.default.bool,

  /**
     * Additional hint text to display
     */
  hint: _propTypes.default.node,

  /**
     * A unique ID to be used for the field input. If one isn't provided, a unique ID will be generated.
     */
  id: _propTypes.default.string,

  /**
     * Access a reference to the field input
     */
  inputRef: _propTypes.default.func,

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

  /**
     * Label for the field.
     */
  label: _propTypes.default.node.isRequired,

  /**
     * Additional classes to be added to the field label
     */
  labelClassName: _propTypes.default.string,

  /**
     * The root HTML element used to render the field label
     */
  labelComponent: _propTypes.default.oneOf(['label', 'legend']).isRequired,

  /**
     * A unique `id` to be used on the field label. If one isn't provided, a unique ID will be generated.
     */
  labelId: _propTypes.default.string,

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

  /**
     * A function that returns a field input element to accept render props
     */
  render: _propTypes.default.func.isRequired
};
var FormControlPropKeys = ['className', 'component', 'errorId', 'errorMessage', 'errorMessageClassName', 'errorPlacement', 'focusTrigger', 'hint', 'id', 'inputRef', 'inversed', 'label', 'labelClassName', 'labelComponent', 'labelId', 'requirementLabel', 'render'];
exports.FormControlPropKeys = FormControlPropKeys;
var _default = FormControl;
exports.default = _default;