UNPKG

tdesign-vue-next

Version:
293 lines (289 loc) 11.6 kB
/** * tdesign v1.11.5 * (c) 2025 tdesign * @license MIT */ import { _ as _defineProperty } from '../_chunks/dep-da95471f.mjs'; import { _ as _typeof } from '../_chunks/dep-4ed7019e.mjs'; import { _ as _slicedToArray } from '../_chunks/dep-6d9c1bfc.mjs'; import { defineComponent, computed, toRefs, ref, nextTick, inject, watch, onMounted, createVNode, mergeProps } from 'vue'; import { isObject, merge, omit } from 'lodash-es'; import './utils/index.mjs'; import { FormItemInjectionKey } from '../form/consts/index.mjs'; import { s as setStyle } from '../_chunks/dep-6cfc5734.mjs'; import { b as getCharacterLength, a as getValidAttrs } from '../_chunks/dep-3d81ac0a.mjs'; import useVModel from '../hooks/useVModel.mjs'; import { useDisabled } from '../hooks/useDisabled.mjs'; import { useReadonly } from '../hooks/useReadonly.mjs'; import { u as useTNodeJSX } from '../_chunks/dep-46ec3e86.mjs'; import { usePrefixClass, useCommonClassName } from '../hooks/useConfig.mjs'; import { useLengthLimit } from '../input/hooks/useLengthLimit.mjs'; import props from './props.mjs'; import { calcTextareaHeight } from './utils/calcTextareaHeight.mjs'; import '../_chunks/dep-9f375e51.mjs'; import '../hooks/index.mjs'; import '../hooks/icon.mjs'; import '../hooks/slot.mjs'; import '../utils/render-tnode.mjs'; import '../hooks/useCommonClassName.mjs'; import '../config-provider/hooks/useConfig.mjs'; import '../config-provider/utils/context.mjs'; import '../_chunks/dep-a5ae2f3f.mjs'; import '../_chunks/dep-82d2a14f.mjs'; import '../_chunks/dep-37fa6ccb.mjs'; import '../_chunks/dep-7fb24b17.mjs'; import '../hooks/useDefaultValue.mjs'; import '../hooks/useDestroyOnClose.mjs'; import '../hooks/useKeepAnimation.mjs'; import '../hooks/useRipple.mjs'; import '../hooks/useVirtualScroll.mjs'; import '../hooks/useImagePreviewUrl.mjs'; import '../_chunks/dep-dda40c90.mjs'; import '../_chunks/dep-88187fb2.mjs'; import '../_chunks/dep-03e058e5.mjs'; import '../_chunks/dep-6a79c950.mjs'; import '../_chunks/dep-198540b0.mjs'; var _Textarea = defineComponent({ name: "TTextarea", inheritAttrs: false, props: props, setup: function setup(props2, _ref) { var attrs = _ref.attrs, expose = _ref.expose; var prefix = usePrefixClass(); var name = usePrefixClass("textarea"); var TEXTAREA_TIPS_CLASS = computed(function () { return "".concat(name.value, "__tips"); }); var TEXTAREA_LIMIT = computed(function () { return "".concat(name.value, "__limit"); }); var _toRefs = toRefs(props2), value = _toRefs.value, modelValue = _toRefs.modelValue; var _useVModel = useVModel(value, modelValue, props2.defaultValue, props2.onChange), _useVModel2 = _slicedToArray(_useVModel, 2), innerValue = _useVModel2[0], setInnerValue = _useVModel2[1]; var disabled = useDisabled(); var isReadonly = useReadonly(); var textareaStyle = ref({}); var refTextareaElem = ref(); var focused = ref(false); var isComposing = ref(false); var focus = function focus() { var _refTextareaElem$valu; return (_refTextareaElem$valu = refTextareaElem.value) === null || _refTextareaElem$valu === void 0 ? void 0 : _refTextareaElem$valu.focus(); }; var blur = function blur() { var _refTextareaElem$valu2; return (_refTextareaElem$valu2 = refTextareaElem.value) === null || _refTextareaElem$valu2 === void 0 ? void 0 : _refTextareaElem$valu2.blur(); }; var adjustTextareaHeight = function adjustTextareaHeight() { var _refTextareaElem$valu3; if (props2.autosize === true) { textareaStyle.value = calcTextareaHeight(refTextareaElem.value); } else if (props2.autosize && _typeof(props2.autosize) === "object") { var _props2$autosize = props2.autosize, minRows = _props2$autosize.minRows, maxRows = _props2$autosize.maxRows; textareaStyle.value = calcTextareaHeight(refTextareaElem.value, minRows, maxRows); } else if (attrs.rows) { textareaStyle.value = { height: "auto", minHeight: "auto" }; } else if (attrs.style && (_refTextareaElem$valu3 = refTextareaElem.value) !== null && _refTextareaElem$valu3 !== void 0 && (_refTextareaElem$valu3 = _refTextareaElem$valu3.style) !== null && _refTextareaElem$valu3 !== void 0 && _refTextareaElem$valu3.height) { textareaStyle.value = { height: refTextareaElem.value.style.height }; } }; var setInputValue = function setInputValue() { var v = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ""; var textareaElem = refTextareaElem.value; var sV = String(v); if (!textareaElem) { return; } if (textareaElem.value !== sV) { textareaElem.value = sV; innerValue.value = sV; } }; var inputValueChangeHandle = function inputValueChangeHandle(e) { var target = e.target; var val = target.value; if (props2.maxcharacter && props2.maxcharacter >= 0) { var stringInfo = getCharacterLength(val, props2.maxcharacter); val = _typeof(stringInfo) === "object" && stringInfo.characters; } !isComposing.value && setInnerValue(val, { e: e }); nextTick(function () { return setInputValue(val); }); adjustTextareaHeight(); }; var handleInput = function handleInput(e) { inputValueChangeHandle(e); }; var onCompositionstart = function onCompositionstart() { isComposing.value = true; }; var onCompositionend = function onCompositionend(e) { isComposing.value = false; inputValueChangeHandle(e); }; var eventDeal = function eventDeal(name2, e) { var _props2$eventName; if (disabled.value) return; var eventName = "on".concat(name2[0].toUpperCase()).concat(name2.slice(1)); (_props2$eventName = props2[eventName]) === null || _props2$eventName === void 0 || _props2$eventName.call(props2, innerValue.value, { e: e }); }; var emitKeyDown = function emitKeyDown(e) { eventDeal("keydown", e); }; var emitKeyUp = function emitKeyUp(e) { eventDeal("keyup", e); }; var emitKeypress = function emitKeypress(e) { eventDeal("keypress", e); }; var emitFocus = function emitFocus(e) { var _props2$onFocus; adjustTextareaHeight(); if (disabled.value) return; focused.value = true; (_props2$onFocus = props2.onFocus) === null || _props2$onFocus === void 0 || _props2$onFocus.call(props2, innerValue.value, { e: e }); }; var formItem = inject(FormItemInjectionKey, void 0); var emitBlur = function emitBlur(e) { var _props2$onBlur; if (!e.target) return; adjustTextareaHeight(); focused.value = false; (_props2$onBlur = props2.onBlur) === null || _props2$onBlur === void 0 || _props2$onBlur.call(props2, innerValue.value, { e: e }); formItem === null || formItem === void 0 || formItem.handleBlur(); }; var textareaClasses = computed(function () { return [name.value, _defineProperty(_defineProperty({}, "".concat(prefix.value, "-is-disabled"), disabled.value), "".concat(prefix.value, "-is-readonly"), isReadonly.value)]; }); var inputAttrs = computed(function () { return getValidAttrs({ autofocus: props2.autofocus, disabled: disabled.value, readonly: isReadonly.value, placeholder: props2.placeholder, maxlength: !props2.allowInputOverMax && props2.maxlength || void 0, name: props2.name || void 0 }); }); var characterNumber = computed(function () { var characterInfo = getCharacterLength(String(innerValue.value || "")); if (_typeof(characterInfo) === "object") { return characterInfo.length; } return characterInfo; }); var limitParams = computed(function () { return { value: [void 0, null].includes(innerValue.value) ? void 0 : String(innerValue.value), status: props2.status, maxlength: Number(props2.maxlength), maxcharacter: props2.maxcharacter, allowInputOverMax: props2.allowInputOverMax, onValidate: props2.onValidate }; }); var _useLengthLimit = useLengthLimit(limitParams), tStatus = _useLengthLimit.tStatus; watch(function () { return innerValue.value; }, function () { return adjustTextareaHeight(); }); watch(refTextareaElem, function (el) { if (!el) return; adjustTextareaHeight(); }); watch(function () { return props2.autofocus; }, function (val) { if (val) { refTextareaElem.value.focus(); } }); watch(textareaStyle, function (val) { var style = attrs.style; if (isObject(style)) { setStyle(refTextareaElem.value, merge(style, val)); } else { setStyle(refTextareaElem.value, val); } }); watch(innerValue, function () { nextTick(function () { return adjustTextareaHeight(); }); }); watch(function () { return props2.autosize; }, adjustTextareaHeight, { deep: true }); expose({ focus: focus, blur: blur }); onMounted(function () { adjustTextareaHeight(); }); var renderTNodeJSX = useTNodeJSX(); return function () { var _String; var inputEvents = getValidAttrs({ onFocus: emitFocus, onBlur: emitBlur, onKeydown: emitKeyDown, onKeyup: emitKeyUp, onKeypress: emitKeypress }); var _useCommonClassName = useCommonClassName(), STATUS = _useCommonClassName.STATUS; var classes = computed(function () { return ["".concat(name.value, "__inner"), _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "".concat(prefix.value, "-is-").concat(tStatus.value), tStatus.value), STATUS.value.disabled, disabled.value), STATUS.value.focused, focused.value), "".concat(prefix.value, "-resize-none"), _typeof(props2.autosize) === "object"), "narrow-scrollbar"]; }); var tips = renderTNodeJSX("tips"); var textTips = tips && createVNode("div", { "class": "".concat(TEXTAREA_TIPS_CLASS.value, " ").concat(name.value, "__tips--").concat(props2.status || "normal") }, [tips]); var limitText = props2.maxcharacter && createVNode("span", { "class": TEXTAREA_LIMIT.value }, ["".concat(characterNumber.value, "/").concat(props2.maxcharacter)]) || !props2.maxcharacter && props2.maxlength && createVNode("span", { "class": TEXTAREA_LIMIT.value }, ["".concat(innerValue.value ? (_String = String(innerValue.value)) === null || _String === void 0 ? void 0 : _String.length : 0, "/").concat(props2.maxlength)]); return createVNode("div", mergeProps({ "class": textareaClasses.value }, omit(attrs, ["style"])), [createVNode("textarea", mergeProps({ "onInput": handleInput, "onCompositionstart": onCompositionstart, "onCompositionend": onCompositionend, "ref": refTextareaElem, "value": innerValue.value, "class": classes.value }, inputEvents, inputAttrs.value), null), textTips || limitText ? createVNode("div", { "class": ["".concat(name.value, "__info_wrapper"), _defineProperty({}, "".concat(name.value, "__info_wrapper_align"), !textTips)] }, [textTips, limitText]) : null]); }; } }); export { _Textarea as default }; //# sourceMappingURL=textarea.mjs.map