/**
 * tdesign v0.46.4
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _typeof = require('@babel/runtime/helpers/typeof');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var Vue = require('vue');
var isFunction = require('lodash/isFunction');
var _common_js_utils_helper = require('../_common/js/utils/helper.js');
var textarea_props = require('./props.js');
var utils_helper = require('../utils/helper.js');
var textarea_calcTextareaHeight = require('./calcTextareaHeight.js');
var utils_renderTnode = require('../utils/render-tnode.js');
var configProvider_configReceiver = require('../config-provider/config-receiver.js');
var utils_mixins = require('../utils/mixins.js');
require('@babel/runtime/helpers/toConsumableArray');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/camelCase');
require('lodash/kebabCase');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var Vue__default = /*#__PURE__*/_interopDefaultLegacy(Vue);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); 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 = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var classPrefixMixins = configProvider_configReceiver.getClassPrefixMixins("textarea");

function getValidAttrs(obj) {
  var newObj = {};
  Object.keys(obj).forEach(function (key) {
    if (typeof obj[key] !== "undefined") {
      newObj[key] = obj[key];
    }
  });
  return newObj;
}

var _Textarea = utils_mixins["default"](Vue__default["default"], classPrefixMixins).extend({
  name: "TTextarea",
  props: _objectSpread({}, textarea_props["default"]),
  inject: {
    tFormItem: {
      "default": void 0
    }
  },
  data: function data() {
    return {
      formDisabled: void 0,
      focused: false,
      mouseHover: false,
      textareaStyle: {}
    };
  },
  computed: {
    tDisabled: function tDisabled() {
      return this.formDisabled || this.disabled;
    },
    textareaClasses: function textareaClasses() {
      var _ref;

      return [this.componentName, (_ref = {}, _defineProperty__default["default"](_ref, "".concat(this.classPrefix, "-is-disabled"), this.tDisabled), _defineProperty__default["default"](_ref, "".concat(this.classPrefix, "-is-readonly"), this.readonly), _ref)];
    },
    inputAttrs: function inputAttrs() {
      return getValidAttrs({
        autofocus: this.autofocus,
        disabled: this.tDisabled,
        readonly: this.readonly,
        placeholder: this.placeholder,
        name: this.name || void 0,
        unselectable: this.readonly ? "on" : "off"
      });
    },
    characterNumber: function characterNumber() {
      var characterInfo = utils_helper.getCharacterLength(String(this.value || ""));

      if (_typeof__default["default"](characterInfo) === "object") {
        return characterInfo.length;
      }

      return characterInfo;
    }
  },
  mounted: function mounted() {
    this.adjustTextareaHeight();
  },
  methods: {
    adjustTextareaHeight: function adjustTextareaHeight() {
      if (this.autosize === true) {
        this.textareaStyle = textarea_calcTextareaHeight["default"](this.$refs.refTextareaElem);
      } else if (_typeof__default["default"](this.autosize) === "object") {
        var _this$autosize, _this$autosize2;

        this.textareaStyle = textarea_calcTextareaHeight["default"](this.$refs.refTextareaElem, (_this$autosize = this.autosize) === null || _this$autosize === void 0 ? void 0 : _this$autosize.minRows, (_this$autosize2 = this.autosize) === null || _this$autosize2 === void 0 ? void 0 : _this$autosize2.maxRows);
      } else if (this.$attrs.rows) {
        this.textareaStyle = {
          height: "auto",
          minHeight: "auto"
        };
      }
    },
    emitEvent: function emitEvent(name, value, context) {
      this.$emit(name, value, context);
      var handleName = utils_helper.getPropsApiByEvent(name);
      isFunction__default["default"](this[handleName]) && this[handleName](value, context);
    },
    focus: function focus() {
      var input = this.$refs.refTextareaElem;
      input === null || input === void 0 ? void 0 : input.focus();
    },
    blur: function blur() {
      var input = this.$refs.refTextareaElem;
      input === null || input === void 0 ? void 0 : input.blur();
    },
    handleInput: function handleInput(e) {
      if (e.isComposing || e.inputType === "insertCompositionText") return;
      this.inputValueChangeHandle(e);
    },
    onCompositionend: function onCompositionend(e) {
      this.inputValueChangeHandle(e);
    },
    inputValueChangeHandle: function inputValueChangeHandle(e) {
      var _this = this;

      var target = e.target;
      var val = target.value;
      val = _common_js_utils_helper.limitUnicodeMaxLength(val, this.maxlength);

      if (this.maxcharacter && this.maxcharacter >= 0) {
        var stringInfo = utils_helper.getCharacterLength(val, this.maxcharacter);
        val = _typeof__default["default"](stringInfo) === "object" && stringInfo.characters;
      }

      this.$emit("input", val);
      this.emitEvent("change", val, {
        e: e
      });
      this.$nextTick(function () {
        return _this.setInputValue(val);
      });
      this.adjustTextareaHeight();
    },
    setInputValue: function setInputValue() {
      var v = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
      var textareaElem = this.$refs.refTextareaElem;
      var sV = String(v);

      if (!textareaElem) {
        return;
      }

      if (textareaElem.value !== sV) {
        textareaElem.value = sV;
      }
    },
    emitKeyDown: function emitKeyDown(e) {
      if (this.tDisabled) return;
      this.emitEvent("keydown", this.value, {
        e: e
      });
    },
    emitKeyUp: function emitKeyUp(e) {
      if (this.tDisabled) return;
      this.emitEvent("keyup", this.value, {
        e: e
      });
    },
    emitKeypress: function emitKeypress(e) {
      if (this.tDisabled) return;
      this.emitEvent("keypress", this.value, {
        e: e
      });
    },
    emitFocus: function emitFocus(e) {
      if (this.tDisabled) return;
      this.focused = true;
      this.emitEvent("focus", this.value, {
        e: e
      });
    },
    emitBlur: function emitBlur(e) {
      var _this$tFormItem;

      this.focused = false;
      (_this$tFormItem = this.tFormItem) === null || _this$tFormItem === void 0 ? void 0 : _this$tFormItem.validate("blur");
      this.emitEvent("blur", this.value, {
        e: e
      });
    }
  },
  render: function render() {
    var _ref2;

    var h = arguments[0];
    var inputEvents = getValidAttrs({
      focus: this.emitFocus,
      blur: this.emitBlur,
      keydown: this.emitKeyDown,
      keyup: this.emitKeyUp,
      keypress: this.emitKeypress
    });
    var classes = ["".concat(this.componentName, "__inner"), (_ref2 = {}, _defineProperty__default["default"](_ref2, "".concat(this.classPrefix, "-is-").concat(this.status), this.status), _defineProperty__default["default"](_ref2, this.commonStatusClassName.disabled, this.tDisabled), _defineProperty__default["default"](_ref2, this.commonStatusClassName.focused, this.focused), _defineProperty__default["default"](_ref2, "".concat(this.classPrefix, "-resize-none"), _typeof__default["default"](this.autosize) === "object"), _ref2)];
    var tips = utils_renderTnode.renderTNodeJSX(this, "tips");
    return h("div", {
      "class": this.textareaClasses
    }, [h("textarea", {
      "on": _objectSpread({
        "input": this.handleInput,
        "compositionend": this.onCompositionend
      }, inputEvents),
      "attrs": _objectSpread({}, _objectSpread(_objectSpread({}, this.$attrs), this.inputAttrs)),
      "domProps": {
        "value": this.value
      },
      "class": classes,
      "style": this.textareaStyle,
      "ref": "refTextareaElem"
    }), this.maxcharacter ? h("span", {
      "class": "".concat(this.componentName, "__limit")
    }, ["".concat(this.characterNumber, "/").concat(this.maxcharacter)]) : null, !this.maxcharacter && this.maxlength ? h("span", {
      "class": "".concat(this.componentName, "__limit")
    }, ["".concat(this.value ? _common_js_utils_helper.getUnicodeLength(String(this.value)) : 0, "/").concat(this.maxlength)]) : null, tips && h("div", {
      "class": ["".concat(this.componentName, "__tips"), "".concat(this.componentName, "__tips--").concat(this.status || "normal")]
    }, [tips])]);
  }
});

exports["default"] = _Textarea;
//# sourceMappingURL=textarea.js.map
