UNPKG

tdesign-vue

Version:
598 lines (594 loc) 22.5 kB
/** * tdesign v1.12.1 * (c) 2025 tdesign * @license MIT */ import { h as helper } from '../_chunks/dep-323b993c.js'; import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray'; import _typeof from '@babel/runtime/helpers/typeof'; import _slicedToArray from '@babel/runtime/helpers/slicedToArray'; import _defineProperty from '@babel/runtime/helpers/defineProperty'; import { BrowseIcon, BrowseOffIcon, CloseCircleFilledIcon } from 'tdesign-icons-vue'; import { kebabCase, camelCase } from 'lodash-es'; import { getUnicodeLength, getIEVersion, limitUnicodeMaxLength } from '../_common/js/utils/helper.js'; import { getCharacterLength, omit } from '../utils/helper.js'; import getConfigReceiverMixins, { getGlobalIconMixins } from '../config-provider/config-receiver.js'; import mixins from '../utils/mixins.js'; import { emitEvent } from '../utils/event.js'; import props from './props.js'; import { renderTNodeJSX } from '../utils/render-tnode.js'; import '../_common/js/log/index.js'; import log from '../_common/js/log/log.js'; import '@babel/runtime/helpers/objectWithoutProperties'; import 'vue'; 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'; import '@babel/runtime/helpers/readOnlyError'; import '@vue/composition-api'; 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 ANIMATION_TIME = 100; function getValidAttrs(obj) { var newObj = {}; Object.keys(obj).forEach(function (key) { if (typeof obj[key] !== "undefined") { newObj[key] = obj[key]; } }); return newObj; } var _Input = mixins(getConfigReceiverMixins("input"), getGlobalIconMixins()).extend({ name: "TInput", inheritAttrs: false, props: _objectSpread(_objectSpread({}, props), {}, { showInput: { type: Boolean, "default": true }, keepWrapperWidth: { type: Boolean, "default": false } }), inject: { tFormItem: { "default": void 0 } }, data: function data() { return { formDisabled: void 0, isHover: false, focused: false, renderType: this.type, inputValue: this.value, composingRef: false, composingRefValue: this.value, preValue: this.value, timer: null, observerTimer: null, containerObserver: null }; }, computed: { tDisabled: function tDisabled() { return this.formDisabled || this.disabled; }, tPlaceholder: function tPlaceholder() { var _this$placeholder; return (_this$placeholder = this.placeholder) !== null && _this$placeholder !== void 0 ? _this$placeholder : this.t(this.global.placeholder); }, showClear: function showClear() { return (this.value && !this.tDisabled && this.clearable && !this.readonly || this.showClearIconOnEmpty) && this.isHover; }, inputAttrs: function inputAttrs() { var _this$autocomplete; return getValidAttrs({ autofocus: this.autofocus, disabled: this.tDisabled, readonly: this.readonly, autocomplete: (_this$autocomplete = this.autocomplete) !== null && _this$autocomplete !== void 0 ? _this$autocomplete : this.global.autocomplete, placeholder: this.tPlaceholder, name: this.name || void 0, type: this.renderType, unselectable: this.readonly ? "on" : "off", spellCheck: this.spellCheck }); }, inputClasses: function inputClasses() { return [this.componentName, _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, this.commonSizeClassName[this.size], this.size !== "medium"), this.commonStatusClassName.disabled, this.tDisabled), this.commonStatusClassName.focused, this.focused), "".concat(this.classPrefix, "-is-").concat(this.tStatus), this.tStatus && this.tStatus !== "default"), "".concat(this.classPrefix, "-align-").concat(this.align), this.align !== "left"), "".concat(this.classPrefix, "-is-disabled"), this.tDisabled), "".concat(this.classPrefix, "-is-readonly"), this.readonly), "".concat(this.componentName, "--borderless"), this.borderless), "".concat(this.componentName, "--focused"), this.focused)]; }, inputWrapClass: function inputWrapClass() { var wrapClass = "".concat(this.componentName, "__wrap"); return ["".concat(wrapClass), _defineProperty(_defineProperty({}, "".concat(wrapClass, "--focused"), this.focused), "".concat(this.componentName, "--auto-width"), this.autoWidth && !this.keepWrapperWidth)]; }, limitNumber: function limitNumber() { var maxlength = this.maxlength, maxcharacter = this.maxcharacter, value = this.value; if (typeof value === "number") return String(value); if (maxlength && maxcharacter) { log.warn("Input", "Pick one of maxlength and maxcharacter please."); } if (maxlength) { var length = value !== null && value !== void 0 && value.length ? getUnicodeLength(value) : 0; return "".concat(length, "/").concat(maxlength); } if (maxcharacter) { return "".concat(getCharacterLength(value || ""), "/").concat(maxcharacter); } return ""; }, innerStatus: function innerStatus() { if (this.limitNumber) { var _this$limitNumber$spl = this.limitNumber.split("/"), _this$limitNumber$spl2 = _slicedToArray(_this$limitNumber$spl, 2), current = _this$limitNumber$spl2[0], total = _this$limitNumber$spl2[1]; return Number(current) > Number(total) ? "error" : ""; } return ""; }, tStatus: function tStatus() { return this.status || this.innerStatus; }, isIE: function isIE() { return getIEVersion() <= 11; } }, watch: { autofocus: { handler: function handler(val) { var _this = this; if (val === true) { this.$nextTick(function () { var input = _this.$refs.inputRef; input === null || input === void 0 || input.focus(); }); } }, immediate: true }, value: { handler: function handler(val) { this.inputValue = this.format ? this.format(val) : val; this.preValue = this.inputValue; var newVal = this.getValueByLimitNumber(val); if (newVal !== val && this.type !== "number") { emitEvent(this, "change", newVal, { trigger: "initial" }); } }, immediate: true }, type: { handler: function handler(val) { this.renderType = val; }, immediate: true } }, created: function created() { var _this2 = this; if (this.autoWidth) { this.addListeners(); } if (this.maxlength || this.maxcharacter) { this.$watch(function () { return _this2.innerStatus; }, function () { return _this2.onValidateChange(); }); } this.innerStatus && this.onValidateChange(); }, mounted: function mounted() { this.addResizeObserver(); }, beforeDestroy: function beforeDestroy() { this.cleanupObserver(this.containerObserver, this.$refs.inputRef); }, methods: { getOutputValue: function getOutputValue(val) { if (this.type === "number") { return val || val === 0 ? Number(val) : void 0; } return val; }, addListeners: function addListeners() { var _this3 = this; this.$watch(function () { return _this3.value + _this3.placeholder; }, function () { if (!_this3.autoWidth) return; _this3.$nextTick(function () { _this3.updateInputWidth(); }); }, { immediate: true }); }, addResizeObserver: function addResizeObserver() { var _this4 = this; if (this.$refs.inputRef) { this.$watch(function () { return _this4.$refs.inputRef; }, function () { _this4.cleanupObserver(_this4.containerObserver, _this4.$refs.inputRef); _this4.containerObserver = _this4.useResizeObserver(_this4.$refs.inputRef, function () { if (_this4.autoWidth) { _this4.observerTimer = setTimeout(function () { _this4.updateInputWidth(); clearTimeout(_this4.observerTimer); }, ANIMATION_TIME); } }); }, { immediate: true }); } }, renderIcon: function renderIcon(h, icon, iconType) { if (typeof icon === "function") { return icon(h); } if (this.$scopedSlots[kebabCase(iconType)]) { return this.$scopedSlots[kebabCase(iconType)](h); } if (this.$scopedSlots[camelCase(iconType)]) { return this.$scopedSlots[camelCase(iconType)](h); } return null; }, setInputValue: function setInputValue() { var v = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ""; var input = this.$refs.inputRef; if (!input) return; var sV = String(v); if (input.value !== sV) { input.value = sV; } }, focus: function focus() { this.$refs.inputRef.focus(); }, blur: function blur() { this.$refs.inputRef.blur(); }, handleKeydown: function handleKeydown(e) { if (this.tDisabled) return; var value = e.currentTarget.value; var tmpValue = this.getOutputValue(value); if (/enter/i.test(e.key) || /enter/i.test(e.code)) { emitEvent(this, "enter", tmpValue, { e: e }); } else { emitEvent(this, "keydown", tmpValue, { e: e }); } }, handleKeyUp: function handleKeyUp(e) { if (this.tDisabled) return; var value = e.currentTarget.value; if (e.key === "Process") { return; } var tmpValue = this.getOutputValue(value); emitEvent(this, "keyup", tmpValue, { e: e }); }, handleKeypress: function handleKeypress(e) { if (this.tDisabled) return; var value = e.currentTarget.value; var tmpValue = this.getOutputValue(value); emitEvent(this, "keypress", tmpValue, { e: e }); }, handlePaste: function handlePaste(e) { if (this.tDisabled) return; var clipData = e.clipboardData || window.clipboardData; emitEvent(this, "paste", { e: e, pasteValue: clipData === null || clipData === void 0 ? void 0 : clipData.getData("text/plain") }); }, onHandleMousewheel: function onHandleMousewheel(e) { emitEvent(this, "wheel", { e: e }); }, emitPassword: function emitPassword() { if (this.tDisabled) return; var renderType = this.renderType; var toggleType = renderType === "password" ? "text" : "password"; this.renderType = toggleType; }, emitClear: function emitClear(e) { var val = this.type === "number" ? void 0 : ""; emitEvent(this, "change", val, { e: e, trigger: "clear" }); emitEvent(this, "clear", { e: e }); }, emitFocus: function emitFocus(e) { this.inputValue = this.value; if (this.tDisabled) return; this.focused = true; emitEvent(this, "focus", this.value, { e: e }); }, formatAndEmitBlur: function formatAndEmitBlur(e) { var _this$tFormItem; if (this.format) { this.inputValue = this.type === "number" || typeof this.value === "number" ? this.value : this.format(this.value); } this.focused = false; (_this$tFormItem = this.tFormItem) === null || _this$tFormItem === void 0 || _this$tFormItem.validate("blur"); emitEvent(this, "blur", this.value, { e: e }); }, compositionstartHandler: function compositionstartHandler(e) { var _this$onCompositionst; this.composingRef = true; var value = e.currentTarget.value; this.composingRefValue = value; this.$emit("compositionstart", value, { e: e }); (_this$onCompositionst = this.onCompositionstart) === null || _this$onCompositionst === void 0 || _this$onCompositionst.call(this, value, { e: e }); }, compositionendHandler: function compositionendHandler(e) { var _this$onCompositionen; var value = e.currentTarget.value; if (this.composingRef) { this.composingRef = false; this.handleInput(e); } this.composingRefValue = ""; this.$emit("compositionend", value, { e: e }); (_this$onCompositionen = this.onCompositionend) === null || _this$onCompositionen === void 0 || _this$onCompositionen.call(this, value, { e: e }); }, onRootClick: function onRootClick(e) { var _this$$refs$inputRef, _this$onClick; (_this$$refs$inputRef = this.$refs.inputRef) === null || _this$$refs$inputRef === void 0 || _this$$refs$inputRef.focus(); this.$emit("click", { e: e }); (_this$onClick = this.onClick) === null || _this$onClick === void 0 || _this$onClick.call(this, { e: e }); }, throttleChangeCursorPos: function throttleChangeCursorPos(ref, pos) { if (this.type === "number") return; ref.selectionEnd = pos; }, handleInput: function handleInput(e) { var _this5 = this; this.preValue = this.inputValue + e.data; var val = e.currentTarget.value; var preCursorPos; if (this.composingRef) { this.composingRefValue = val; } else { if (this.type === "number") { val = this.getOutputValue(val); } else { val = this.getValueByLimitNumber(val); } emitEvent(this, "change", val, { e: e, trigger: "input" }); if (!this.isIE) { var inputRef = this.$refs.inputRef; preCursorPos = inputRef.selectionStart; clearTimeout(this.timer); this.timer = setTimeout(function () { _this5.throttleChangeCursorPos(inputRef, preCursorPos); }); } this.$nextTick(function () { _this5.setInputValue(_this5.value); }); } }, onInputMouseenter: function onInputMouseenter(e) { this.isHover = true; emitEvent(this, "mouseenter", { e: e }); }, onInputMouseleave: function onInputMouseleave(e) { this.isHover = false; emitEvent(this, "mouseleave", { e: e }); }, updateInputWidth: function updateInputWidth() { var pre = this.$refs.inputPreRef; if (!pre) return; var _pre$getBoundingClien = pre.getBoundingClientRect(), width = _pre$getBoundingClien.width; if (this.$refs.inputRef) { this.$refs.inputRef.style.width = "".concat(width, "px"); } }, getValueByLimitNumber: function getValueByLimitNumber(inputValue) { var allowInputOverMax = this.allowInputOverMax, maxlength = this.maxlength, maxcharacter = this.maxcharacter; if (!(maxlength || maxcharacter) || allowInputOverMax || !inputValue) return inputValue; if (maxlength) { return limitUnicodeMaxLength(inputValue, Number(maxlength)); } if (maxcharacter) { var r = getCharacterLength(inputValue, maxcharacter); if (_typeof(r) === "object") { return r.characters; } } }, onValidateChange: function onValidateChange() { var _this$onValidate; var error = this.innerStatus ? "exceed-maximum" : void 0; (_this$onValidate = this.onValidate) === null || _this$onValidate === void 0 || _this$onValidate.call(this, { error: error }); this.$emit("validate", { error: error }); }, useResizeObserver: function useResizeObserver(el, callback) { if (typeof window === "undefined") return; var isSupport = typeof window !== "undefined" && window.ResizeObserver; if (!isSupport) return; var containerObserver = new ResizeObserver(callback); containerObserver.observe(el); return containerObserver; }, cleanupObserver: function cleanupObserver(observer, container) { if (!observer || !container) return; observer.unobserve(container); observer.disconnect(); } }, render: function render(h) { var inputEvents = { focus: this.emitFocus, blur: this.formatAndEmitBlur, keydown: this.handleKeydown, keyup: this.handleKeyUp, keypress: this.handleKeypress, paste: this.handlePaste, compositionstart: this.compositionstartHandler, compositionend: this.compositionendHandler, change: function change() {} }; var wrapperAttrs = omit(this.$attrs, Object.keys(this.inputAttrs)); var wrapperEvents = omit(this.$listeners, [].concat(_toConsumableArray(Object.keys(inputEvents)), ["input", "paste"])); var prefixIcon = this.renderIcon(h, this.prefixIcon, "prefix-icon"); var suffixIcon = this.renderIcon(h, this.suffixIcon, "suffix-icon"); var passwordIcon = this.renderIcon(h, void 0, "password-icon"); var label = renderTNodeJSX(this, "label"); var suffix = renderTNodeJSX(this, "suffix"); var limitNode = this.limitNumber && this.showLimitNumber ? h("div", { "class": ["".concat(this.classPrefix, "-input__limit-number"), _defineProperty({}, "".concat(this.classPrefix, "-is-disabled"), this.disabled)] }, [this.limitNumber]) : null; var labelContent = label ? h("div", { "class": "".concat(this.componentName, "__prefix") }, [label]) : null; var suffixContent = suffix || limitNode ? h("div", { "class": "".concat(this.componentName, "__suffix") }, [suffix, limitNode]) : null; var _this$useGlobalIcon = this.useGlobalIcon({ BrowseIcon: BrowseIcon, BrowseOffIcon: BrowseOffIcon, CloseCircleFilledIcon: CloseCircleFilledIcon }), BrowseIcon$1 = _this$useGlobalIcon.BrowseIcon, BrowseOffIcon$1 = _this$useGlobalIcon.BrowseOffIcon, CloseCircleFilledIcon$1 = _this$useGlobalIcon.CloseCircleFilledIcon; if (this.type === "password") { if (this.renderType === "password") { suffixIcon = h(BrowseOffIcon$1, { "class": _defineProperty({}, "".concat(this.componentName, "__suffix-clear"), !this.tDisabled), "nativeOn": { "click": this.emitPassword } }); } else if (this.renderType === "text") { suffixIcon = h(BrowseIcon$1, { "class": _defineProperty({}, "".concat(this.componentName, "__suffix-clear"), !this.tDisabled), "nativeOn": { "click": this.emitPassword } }); } } if (this.showClear) { if (this.type === "password") { passwordIcon = h(CloseCircleFilledIcon$1, { "class": "".concat(this.componentName, "__suffix-clear"), "nativeOn": { "click": this.emitClear } }); } else { suffixIcon = h(CloseCircleFilledIcon$1, { "class": "".concat(this.componentName, "__suffix-clear"), "nativeOn": { "click": this.emitClear } }); } } var classes = [this.inputClasses, this.inputClass, _defineProperty(_defineProperty({}, "".concat(this.componentName, "--prefix"), prefixIcon || labelContent), "".concat(this.componentName, "--suffix"), suffixIcon || suffixContent)]; var inputTextValue = this.composingRef ? this.composingRefValue : this.inputValue; var inputNode = h("div", { "class": classes, "attrs": _objectSpread({}, wrapperAttrs), "on": _objectSpread(_objectSpread({}, wrapperEvents), {}, { "click": this.onRootClick, "mouseenter": this.onInputMouseenter, "mouseleave": this.onInputMouseleave, "wheel": this.onHandleMousewheel }) }, [prefixIcon ? h("span", { "class": ["".concat(this.componentName, "__prefix"), "".concat(this.componentName, "__prefix-icon")] }, [prefixIcon]) : null, labelContent, h("input", helper([{}, { "attrs": this.inputAttrs }, {}, { "on": inputEvents }, { "ref": "inputRef", "class": ["".concat(this.componentName, "__inner"), _defineProperty({}, "".concat(this.componentName, "--soft-hidden"), !this.showInput)], "domProps": { "value": inputTextValue }, "on": { "input": this.handleInput }, "attrs": { "title": this.disabled ? inputTextValue : void 0 } }])), this.autoWidth && h("span", { "ref": "inputPreRef", "class": "".concat(this.classPrefix, "-input__input-pre") }, [this.preValue || this.tPlaceholder]), suffixContent, passwordIcon ? h("span", { "class": ["".concat(this.componentName, "__suffix"), "".concat(this.componentName, "__suffix-icon"), "".concat(this.componentName, "__clear")] }, [passwordIcon]) : null, suffixIcon ? h("span", { "class": ["".concat(this.componentName, "__suffix"), "".concat(this.componentName, "__suffix-icon"), _defineProperty({}, "".concat(this.componentName, "__clear"), this.showClear)] }, [suffixIcon]) : null]); var tips = renderTNodeJSX(this, "tips"); return h("div", { "class": this.inputWrapClass }, [inputNode, tips && h("div", { "class": "".concat(this.componentName, "__tips ").concat(this.componentName, "__tips--").concat(this.tStatus || "default") }, [tips])]); } }); export { _Input as default }; //# sourceMappingURL=input.js.map