UNPKG

tdesign-vue

Version:
292 lines (288 loc) 11 kB
/** * tdesign v1.12.1 * (c) 2025 tdesign * @license MIT */ import _typeof from '@babel/runtime/helpers/typeof'; import _defineProperty from '@babel/runtime/helpers/defineProperty'; import Vue from 'vue'; import { merge, isFunction } from 'lodash-es'; import { limitUnicodeMaxLength, getUnicodeLength } from '../_common/js/utils/helper.js'; import { getCharacterLength, getPropsApiByEvent } from '../utils/helper.js'; import calcTextareaHeight from './calcTextareaHeight.js'; import { renderTNodeJSX } from '../utils/render-tnode.js'; import { getClassPrefixMixins } from '../config-provider/config-receiver.js'; import mixins from '../utils/mixins.js'; import setStyle from '../_common/js/utils/setStyle.js'; import props from './props.js'; import '@babel/runtime/helpers/toConsumableArray'; import '@babel/runtime/helpers/objectWithoutProperties'; import '@babel/runtime/helpers/slicedToArray'; import '@babel/runtime/helpers/readOnlyError'; import '@vue/composition-api'; import '../config-provider/context.js'; import '../_common/js/global-config/default-config.js'; import '../_common/js/global-config/locale/zh_CN.js'; import '../_chunks/dep-c44a474d.js'; import '../_chunks/dep-d639fbd7.js'; import 'dayjs'; import '../_chunks/dep-3c66615e.js'; import '../config-provider/type.js'; import '../_common/js/global-config/t.js'; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } var classPrefixMixins = 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 = mixins(Vue, classPrefixMixins).extend({ name: "TTextarea", props: _objectSpread({}, props), inject: { tFormItem: { "default": void 0 } }, data: function data() { return { formDisabled: void 0, focused: false, mouseHover: false, textareaStyle: {}, isComposing: false }; }, computed: { tDisabled: function tDisabled() { return this.formDisabled || this.disabled; }, textareaClasses: function textareaClasses() { return [this.componentName, _defineProperty(_defineProperty({}, "".concat(this.classPrefix, "-is-disabled"), this.tDisabled), "".concat(this.classPrefix, "-is-readonly"), this.readonly)]; }, limitClasses: function limitClasses() { return ["".concat(this.componentName, "__limit"), _defineProperty({}, "".concat(this.classPrefix, "-is-disabled"), this.tDisabled)]; }, 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 = getCharacterLength(String(this.value || "")); if (_typeof(characterInfo) === "object") { return characterInfo.length; } return characterInfo; } }, mounted: function mounted() { var textareaElem = this.$refs.refTextareaElem; if (textareaElem) { textareaElem.addEventListener("transitionend", this.adjustTextareaHeightAfterReady); } else { this.adjustTextareaHeightAfterReady(); } }, beforeDestroy: function beforeDestroy() { var textareaElem = this.$refs.refTextareaElem; if (textareaElem) { textareaElem.removeEventListener("transitionend", this.adjustTextareaHeightAfterReady); } }, watch: { autofocus: { handler: function handler(val) { var _this = this; if (val === true) { this.$nextTick(function () { var textArea = _this.$refs.refTextareaElem; textArea === null || textArea === void 0 || textArea.focus(); }); } }, immediate: true }, value: { handler: function handler() { var _this2 = this; this.$nextTick(function () { return _this2.adjustTextareaHeight(); }); }, immediate: true }, textareaStyle: { handler: function handler(val) { var style = this.$attrs.style; setStyle(this.$refs.refTextareaElem, merge(style, val)); }, immediate: true } }, methods: { adjustTextareaHeight: function adjustTextareaHeight() { var _this3 = this; this.$nextTick(function () { if (!_this3.$refs.refTextareaElem) return; if (_this3.autosize === true) { _this3.textareaStyle = calcTextareaHeight(_this3.$refs.refTextareaElem); } else if (_this3.autosize && _typeof(_this3.autosize) === "object") { var _this3$autosize, _this3$autosize2; _this3.textareaStyle = calcTextareaHeight(_this3.$refs.refTextareaElem, (_this3$autosize = _this3.autosize) === null || _this3$autosize === void 0 ? void 0 : _this3$autosize.minRows, (_this3$autosize2 = _this3.autosize) === null || _this3$autosize2 === void 0 ? void 0 : _this3$autosize2.maxRows); } else if (_this3.$attrs.rows) { _this3.textareaStyle = { height: "auto", minHeight: "auto" }; } }); }, adjustTextareaHeightAfterReady: function adjustTextareaHeightAfterReady() { var _this4 = this; this.$nextTick(function () { return _this4.adjustTextareaHeight(); }); }, emitEvent: function emitEvent(name, value, context) { this.$emit(name, value, context); var handleName = getPropsApiByEvent(name); isFunction(this[handleName]) && this[handleName](value, context); }, focus: function focus() { var textArea = this.$refs.refTextareaElem; textArea === null || textArea === void 0 || textArea.focus(); }, blur: function blur() { var textArea = this.$refs.refTextareaElem; textArea === null || textArea === void 0 || textArea.blur(); }, handleInput: function handleInput(e) { this.inputValueChangeHandle(e); }, onCompositionstart: function onCompositionstart() { this.isComposing = true; }, onCompositionend: function onCompositionend(e) { this.isComposing = false; this.inputValueChangeHandle(e); }, inputValueChangeHandle: function inputValueChangeHandle(e) { var _this5 = this; var target = e.target; var val = target.value; if (!this.isComposing) { if (this.maxlength) { val = limitUnicodeMaxLength(val, Number(this.maxlength)); } if (this.maxcharacter && this.maxcharacter >= 0) { var stringInfo = getCharacterLength(val, this.maxcharacter); val = _typeof(stringInfo) === "object" && stringInfo.characters; } } this.$emit("input", val); if (!this.isComposing) { this.emitEvent("change", val, { e: e }); } this.$nextTick(function () { return _this5.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 || _this$tFormItem.validate("blur"); this.emitEvent("blur", this.value, { e: e }); } }, render: function render() { 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"), _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "".concat(this.classPrefix, "-is-").concat(this.status), this.status), this.commonStatusClassName.disabled, this.tDisabled), this.commonStatusClassName.focused, this.focused), "".concat(this.classPrefix, "-resize-none"), _typeof(this.autosize) === "object")]; var tips = renderTNodeJSX(this, "tips"); var textTips = tips && h("div", { "class": ["".concat(this.componentName, "__tips"), "".concat(this.componentName, "__tips--").concat(this.status || "normal")] }, [tips]); var limitText = this.maxcharacter && h("span", { "class": this.limitClasses }, ["".concat(this.characterNumber, "/").concat(this.maxcharacter)]) || !this.maxcharacter && this.maxlength && h("span", { "class": this.limitClasses }, ["".concat(this.value ? getUnicodeLength(String(this.value)) : 0, "/").concat(this.maxlength)]); return h("div", { "class": this.textareaClasses }, [h("textarea", { "on": _objectSpread({ "input": this.handleInput, "compositionstart": this.onCompositionstart, "compositionend": this.onCompositionend }, inputEvents), "attrs": _objectSpread({}, _objectSpread(_objectSpread({}, this.$attrs), this.inputAttrs)), "domProps": { "value": this.value }, "class": classes, "ref": "refTextareaElem" }), textTips || limitText ? h("div", { "class": ["".concat(this.componentName, "__info_wrapper"), _defineProperty({}, "".concat(this.componentName, "__info_wrapper_align"), !textTips)] }, [textTips, limitText]) : null]); } }); export { _Textarea as default }; //# sourceMappingURL=textarea.js.map