"use strict";

function _typeof(obj) { 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.FormInput = void 0;

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

var _styledComponents = _interopRequireDefault(require("styled-components"));

var _primitives = require("../../../primitives");

var _clickableSpan = require("../../typography/clickable-span");

var _formInput = require("./form-input.proptypes");

var _formInput2 = require("./form-input.css-rules");

var _branding = require("../../../mixins/branding");

var _typography = require("../../../mixins/typography");

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

var StyledBox = (0, _styledComponents["default"])(_primitives.Box).withConfig({
  displayName: "StyledBox",
  componentId: "sc-17zue2n-0"
})(["height:", ";border-radius:6px;border:1px solid ", ";max-width:100%;background-color:white;:focus-within{border:1px solid ", ";}&:not([disabled]):hover{border:1px solid ", ";}"], _formInput2.getInputHeight, (0, _branding.getPaletteColor)('greyscale-light'), (0, _branding.getPaletteColor)('greyscale-darker'), (0, _branding.getPaletteColor)('greyscale-medium'));

var StyledInput = _styledComponents["default"].input.attrs(function (_ref) {
  var large = _ref.large;
  return {
    fontSize: large ? 'f3' : 'f1',
    bold: large ? 'true' : undefined
  };
}).withConfig({
  displayName: "StyledInput",
  componentId: "sc-17zue2n-1"
})(["color:", ";font-family:", ";font-size:", ";font-weight:", ";padding-bottom:2px;padding-left:", ";border:none;background-image:", ";background-repeat:no-repeat;background-position:", ";margin-top:5px;margin-bottom:5px;flex-grow:2;&&::placeholder{color:", ";opacity:1;}&&::-ms-input-placeholder{color:", ";}&&:-ms-input-placeholder{color:", ";}"], _formInput2.getTextColor, _typography.getFontFamily, _typography.getFontSize, _typography.getFontWeight, _formInput2.getInputLeftPadding, _formInput2.getIconDataUri, _formInput2.getInputIconPosition, _formInput2.getPlaceholderColor, _formInput2.getPlaceholderColor, _formInput2.getPlaceholderColor);

var StyledButton = (0, _styledComponents["default"])(_clickableSpan.ClickableSpan).withConfig({
  displayName: "StyledButton",
  componentId: "sc-17zue2n-2"
})(["font-family:", ";color:", ";width:auto;margin-right:10px;"], _typography.getFontFamily, (0, _branding.getPaletteColor)('greyscale-darker'));

var FormInput = function FormInput(_ref2) {
  var focusOnMount = _ref2.focusOnMount,
      onRef = _ref2.onRef,
      onChange = _ref2.onChange,
      props = _objectWithoutProperties(_ref2, ["focusOnMount", "onRef", "onChange"]);

  var inputRef = _react["default"].useRef(); // TODO: we should just let the component pass a ref in instead of
  // an onRef function. https://github.com/wealthsimple/patchwork/issues/771


  (0, _react.useEffect)(function () {
    if (onRef && inputRef.current) {
      onRef(inputRef.current);
    }
  }, [onRef, inputRef]);
  var handleChange = (0, _react.useCallback)(function (event) {
    onChange(event.target.value, event);
  }, [onChange]);
  return _react["default"].createElement(StyledBox, _extends({}, props, {
    stacked: "row",
    large: props.large
  }), _react["default"].createElement(StyledInput, _extends({}, props, {
    id: props.id,
    ref: inputRef,
    type: props.type || 'text',
    placeholder: props.placeholder || '',
    disabled: props.disabled,
    value: props.value || '',
    error: !!props.error,
    onChange: handleChange,
    onFocus: props.onFocus,
    onBlur: props.onBlur,
    large: props.large,
    iconType: props.iconType,
    iconPosition: props.iconPosition,
    autoFocus: focusOnMount,
    inputMode: props.inputMode,
    pattern: props.inputMode === 'numeric' ? '[0-9]*' : null
  })), props.buttonText !== undefined && props.buttonText !== null ? _react["default"].createElement(StyledButton, {
    type: "button",
    onClick: props.buttonPress
  }, props.buttonText) : null);
};

exports.FormInput = FormInput;
FormInput.displayName = 'FormInput';
FormInput.propTypes = _formInput.formInputPropTypes;