"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _react = _interopRequireDefault(require("react"));
var _Heading = _interopRequireDefault(require("../Heading"));
var _EditableTitleSt = require("./EditableTitle.st.css.js");
var _constants = require("./constants");
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/EditableTitle/EditableTitle.jsx";
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2["default"])(o), (0, _possibleConstructorReturn2["default"])(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2["default"])(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
var DEFAULT_MAX_LENGTH = 100;
var EditableTitle = /*#__PURE__*/function (_React$Component) {
  function EditableTitle(props) {
    var _this;
    (0, _classCallCheck2["default"])(this, EditableTitle);
    _this = _callSuper(this, EditableTitle, [props]);
    _this.wsrInput = /*#__PURE__*/_react["default"].createRef();
    _this.onChange = function (e) {
      _this.setState({
        value: e.target.value
      });
    };
    _this.showPlaceholder = function () {
      return !_this.state.value && _this.props.defaultValue;
    };
    _this.onFocus = function () {
      var value = _this.state.value || _this.props.defaultValue;
      var selectAll = !_this.state.focus;
      _this.setState({
        focus: true,
        value: value
      }, function () {
        _this.wsrInput.current.focus();
        selectAll && _this.wsrInput.current.select();
      });
    };
    _this.onKeyDown = function (e) {
      if (e.key === 'Enter' || e.keyCode === 13) {
        _this.onEnterPressed();
      }
    };
    _this.onEnterPressed = function () {
      _this.wsrInput.current.blur();
    };
    _this.onValueSubmission = function () {
      var value = _this.state.value || _this.props.defaultValue;
      _this.setState({
        value: value,
        focus: false
      }, function () {
        if (typeof _this.props.onSubmit === 'function') {
          _this.props.onSubmit(value);
        }
      });
    };
    _this.state = {
      focus: false,
      value: props.initialValue || ''
    };
    return _this;
  }
  (0, _inherits2["default"])(EditableTitle, _React$Component);
  return (0, _createClass2["default"])(EditableTitle, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      if (this.props.autoFocus) this.onFocus();
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
        dataHook = _this$props.dataHook,
        className = _this$props.className,
        maxLength = _this$props.maxLength;
      var focused = this.state.focus;
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: (0, _EditableTitleSt.st)(_EditableTitleSt.classes.root, className),
        "data-hook": dataHook,
        tabIndex: focused ? -1 : 0,
        onFocus: this.onFocus,
        onClick: this.onFocus,
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 51,
          columnNumber: 7
        }
      }, /*#__PURE__*/_react["default"].createElement("div", {
        "data-hook": _constants.dataHooks.heading,
        className: (0, _EditableTitleSt.st)(_EditableTitleSt.classes.headingWrapper, {
          focused: focused,
          showPlaceholder: !!this.showPlaceholder()
        }),
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 58,
          columnNumber: 9
        }
      }, /*#__PURE__*/_react["default"].createElement(_Heading["default"], {
        ellipsis: true,
        size: "extraLarge",
        className: _EditableTitleSt.classes.heading,
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 65,
          columnNumber: 11
        }
      }, this.props.value || this.state.value || this.props.defaultValue)), /*#__PURE__*/_react["default"].createElement("div", {
        className: (0, _EditableTitleSt.st)(_EditableTitleSt.classes.activationIndicator, {
          focused: focused
        }),
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 69,
          columnNumber: 9
        }
      }, this.props.value || this.state.value || this.props.defaultValue), /*#__PURE__*/_react["default"].createElement("div", {
        className: (0, _EditableTitleSt.st)(_EditableTitleSt.classes.renamingField, {
          focused: focused
        }),
        "data-hook": _constants.dataHooks.renamingField,
        onFocus: function onFocus(e) {
          return (
            // input does not pass his event so we need to catch it
            e.stopPropagation()
          );
        },
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 76,
          columnNumber: 9
        }
      }, /*#__PURE__*/_react["default"].createElement("input", {
        className: _EditableTitleSt.classes.input,
        "data-hook": _constants.dataHooks.input,
        onBlur: this.onValueSubmission,
        ref: this.wsrInput,
        value: this.props.value ? this.props.value : this.state.value,
        maxLength: maxLength || DEFAULT_MAX_LENGTH,
        onChange: this.props.onChange ? this.props.onChange : this.onChange,
        onKeyDown: this.onKeyDown,
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 86,
          columnNumber: 11
        }
      })));
    }
  }]);
}(_react["default"].Component);
EditableTitle.displayName = 'EditableTitle';
EditableTitle.defaultProps = {
  defaultValue: ''
};
var _default = exports["default"] = EditableTitle;