function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { Edit05 } from '@pisell/icon';
import { Input } from '@pisell/materials';
import { isBoolean } from '@pisell/utils';
import cs from 'classnames';
import React, { useEffect, useMemo, useRef, useState } from 'react';
import "./index.less";
/**
 * 可编辑string
 * @param props
 * @constructor
 */
var EditableString = function EditableString(props) {
  var text = props.text,
    style = props.style,
    onChange = props.onChange,
    editable = props.editable,
    editingProps = props.editing,
    maxLength = props.maxLength,
    className = props.className,
    ellipsis = props.ellipsis;

  // 是否正在编辑
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    editing = _useState2[0],
    setEditing = _useState2[1];
  var _useState3 = useState(text),
    _useState4 = _slicedToArray(_useState3, 2),
    originText = _useState4[0],
    setOriginText = _useState4[1];

  // input临时的value
  var _useState5 = useState(text),
    _useState6 = _slicedToArray(_useState5, 2),
    temporaryValue = _useState6[0],
    setTemporaryValue = _useState6[1];
  var inputRef = useRef(null);
  var textRef = useRef(null);
  useEffect(function () {
    setOriginText(text);
    setTemporaryValue(text);
  }, [text]);
  useEffect(function () {
    isBoolean(editingProps) && setEditing(editingProps);
  }, [editingProps]);
  useEffect(function () {
    if (editing) {
      setTimeout(function () {
        var _inputRef$current;
        (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 || _inputRef$current.focus();
      });
    }
  }, [editing]);
  useEffect(function () {
    if (textRef.current) {
      textRef.current.style.webkitLineClamp = ellipsis === null || ellipsis === void 0 ? void 0 : ellipsis.row;
    }
  }, [ellipsis, editing]);
  var handleChangeText = function handleChangeText() {
    if (!temporaryValue) return;
    onChange === null || onChange === void 0 || onChange(temporaryValue);
    setEditing(false);
  };
  var handleEdit = function handleEdit() {
    setEditing(true);
  };
  return useMemo(function () {
    if (editing) {
      return /*#__PURE__*/React.createElement("span", {
        style: style
      }, /*#__PURE__*/React.createElement(Input, {
        ref: inputRef,
        value: temporaryValue,
        onChange: function onChange(e) {
          return setTemporaryValue(e.target.value);
        },
        onPressEnter: handleChangeText,
        onBlur: handleChangeText,
        maxLength: maxLength
      }));
    }
    return /*#__PURE__*/React.createElement("div", {
      style: style,
      size: 16,
      className: cs(className, 'edit-wrap')
    }, /*#__PURE__*/React.createElement("span", {
      className: 'text',
      ref: textRef
    }, temporaryValue), editable && temporaryValue && /*#__PURE__*/React.createElement(Edit05, {
      className: "edit-icon",
      onClick: handleEdit
    }));
  }, [editing, temporaryValue, ellipsis]);
};
export default EditableString;