UNPKG

wix-style-react

Version:
176 lines (149 loc) • 6.5 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; 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 _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; } } import React from 'react'; import PropTypes from 'prop-types'; import Heading from '../Heading'; import Input from '../Input/Input'; import { st, classes } from './EditableTitle.st.css'; import { WixStyleReactContext } from '../WixStyleReactProvider/context'; import { dataHooks } from './constants'; var DEFAULT_MAX_LENGTH = 100; var EditableTitle = /*#__PURE__*/function (_React$Component) { _inherits(EditableTitle, _React$Component); var _super = _createSuper(EditableTitle); function EditableTitle(props) { var _this; _classCallCheck(this, EditableTitle); _this = _super.call(this, props); _defineProperty(_assertThisInitialized(_this), "onChange", function (e) { _this.setState({ value: e.target.value }); }); _defineProperty(_assertThisInitialized(_this), "showPlaceholder", function () { return !_this.state.value && _this.props.defaultValue; }); _defineProperty(_assertThisInitialized(_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.focus(); selectAll && _this.wsrInput.select(); }); }); _defineProperty(_assertThisInitialized(_this), "onEnterPressed", function () { _this.wsrInput.blur(); }); _defineProperty(_assertThisInitialized(_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; } _createClass(EditableTitle, [{ key: "componentDidMount", value: function componentDidMount() { if (this.props.autoFocus) this.onFocus(); } }, { key: "render", value: function render() { var _this2 = this; 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.createElement(WixStyleReactContext.Consumer, null, function (_ref) { var reducedSpacingAndImprovedLayout = _ref.reducedSpacingAndImprovedLayout; return /*#__PURE__*/React.createElement("div", { className: st(classes.root, { reducedSpacingAndImprovedLayout: reducedSpacingAndImprovedLayout }, className), "data-hook": dataHook, tabIndex: 0, onFocus: _this2.onFocus, onClick: _this2.onFocus }, /*#__PURE__*/React.createElement("div", { "data-hook": dataHooks.heading, className: st(classes.headingWrapper, { focused: focused, showPlaceholder: !!_this2.showPlaceholder() }) }, /*#__PURE__*/React.createElement(Heading, { ellipsis: true, appearance: "H1", className: classes.heading }, _this2.props.value || _this2.state.value || _this2.props.defaultValue)), /*#__PURE__*/React.createElement("div", { className: st(classes.activationIndicator, { focused: focused }) }, _this2.props.value || _this2.state.value || _this2.props.defaultValue), /*#__PURE__*/React.createElement("div", { className: st(classes.renamingField, { focused: focused }), "data-hook": dataHooks.renamingField, onFocus: function onFocus(e) { return (// input does not pass his event so we need to catch it e.stopPropagation() ); } }, /*#__PURE__*/React.createElement(Input, { autoSelect: false, textOverflow: "clip", maxLength: maxLength || DEFAULT_MAX_LENGTH, onChange: _this2.props.onChange ? _this2.props.onChange : _this2.onChange, value: _this2.props.value ? _this2.props.value : _this2.state.value, ref: function ref(wsrInput) { return _this2.wsrInput = wsrInput; }, onBlur: _this2.onValueSubmission, onEnterPressed: _this2.onEnterPressed }))); }); } }]); return EditableTitle; }(React.Component); EditableTitle.displayName = 'EditableTitle'; EditableTitle.defaultProps = { defaultValue: '' }; EditableTitle.propTypes = { /** Value - initial value to display */ initialValue: PropTypes.string, /** default - value to display when empty, when clicked the input gets this value */ defaultValue: PropTypes.string, /** onSubmit - invoked when done editing */ onSubmit: PropTypes.func, /** length - maximum chars the input can get */ maxLength: PropTypes.number, /** autoFocus - focus element on mount */ autoFocus: PropTypes.bool, /** onChange-invoked when editing */ onChange: PropTypes.func, /** value- the controlled value of the input */ value: PropTypes.string }; export default EditableTitle;