UNPKG

tdesign-vue

Version:
639 lines (631 loc) 25.2 kB
/** * tdesign v1.12.1 * (c) 2025 tdesign * @license MIT */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var helper = require('../_chunks/dep-882fe8fa.js'); var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray'); var _typeof = require('@babel/runtime/helpers/typeof'); var _slicedToArray = require('@babel/runtime/helpers/slicedToArray'); var _defineProperty = require('@babel/runtime/helpers/defineProperty'); var tdesignIconsVue = require('tdesign-icons-vue'); var _common_js_utils_helper = require('../_common/js/utils/helper.js'); var utils_helper = require('../utils/helper.js'); var configProvider_configReceiver = require('../config-provider/config-receiver.js'); var utils_mixins = require('../utils/mixins.js'); var utils_event = require('../utils/event.js'); var input_props = require('./props.js'); var utils_renderTnode = require('../utils/render-tnode.js'); var _common_js_log_log = require('../_common/js/log/log.js'); var kebabCase = require('../_chunks/dep-727a349e.js'); var camelCase = require('../_chunks/dep-bf70543d.js'); require('@babel/runtime/helpers/objectWithoutProperties'); require('../_chunks/dep-5a0b1276.js'); require('../_chunks/dep-2d34b118.js'); require('../_chunks/dep-6b60413b.js'); require('../_chunks/dep-5ea2b00f.js'); require('../_chunks/dep-9b72cbc5.js'); require('../_chunks/dep-d7270eaf.js'); require('../_chunks/dep-2edcfe51.js'); require('vue'); require('../_chunks/dep-6247fdbf.js'); require('../_common/js/global-config/default-config.js'); require('../_common/js/global-config/locale/zh_CN.js'); require('../_chunks/dep-3ed0c42e.js'); require('../_chunks/dep-53935099.js'); require('dayjs'); require('../_chunks/dep-403352a7.js'); require('../_chunks/dep-cca66fee.js'); require('../_chunks/dep-4361b357.js'); require('../_chunks/dep-887c649b.js'); require('../_chunks/dep-411a10dd.js'); require('../_chunks/dep-51fe98f1.js'); require('../_chunks/dep-3d682642.js'); require('../_chunks/dep-dea23841.js'); require('../_chunks/dep-4aa08166.js'); require('../_chunks/dep-ddc1429e.js'); require('../_chunks/dep-65896ff9.js'); require('../_chunks/dep-b65b2f55.js'); require('../_chunks/dep-6e7b8cda.js'); require('../_chunks/dep-98f48ff2.js'); require('../_chunks/dep-80e3757f.js'); require('../_chunks/dep-08d7842c.js'); require('../_chunks/dep-1ca3138c.js'); require('../_chunks/dep-e6ed4b1f.js'); require('../_chunks/dep-c44b2462.js'); require('../_common/js/global-config/t.js'); require('@babel/runtime/helpers/readOnlyError'); require('@vue/composition-api'); require('../_chunks/dep-4b30bfe2.js'); require('../_chunks/dep-e1cc6f64.js'); require('../_chunks/dep-7b78e8c3.js'); require('../_chunks/dep-f895c884.js'); require('../_chunks/dep-68f48b50.js'); require('../_chunks/dep-051ce30d.js'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray); var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof); var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray); var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty); 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__default["default"](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 = utils_mixins["default"](configProvider_configReceiver["default"]("input"), configProvider_configReceiver.getGlobalIconMixins()).extend({ name: "TInput", inheritAttrs: false, props: _objectSpread(_objectSpread({}, input_props["default"]), {}, { 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__default["default"](_defineProperty__default["default"](_defineProperty__default["default"](_defineProperty__default["default"](_defineProperty__default["default"](_defineProperty__default["default"](_defineProperty__default["default"](_defineProperty__default["default"](_defineProperty__default["default"]({}, 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__default["default"](_defineProperty__default["default"]({}, "".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) { _common_js_log_log["default"].warn("Input", "Pick one of maxlength and maxcharacter please."); } if (maxlength) { var length = value !== null && value !== void 0 && value.length ? _common_js_utils_helper.getUnicodeLength(value) : 0; return "".concat(length, "/").concat(maxlength); } if (maxcharacter) { return "".concat(utils_helper.getCharacterLength(value || ""), "/").concat(maxcharacter); } return ""; }, innerStatus: function innerStatus() { if (this.limitNumber) { var _this$limitNumber$spl = this.limitNumber.split("/"), _this$limitNumber$spl2 = _slicedToArray__default["default"](_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 _common_js_utils_helper.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") { utils_event.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.kebabCase(iconType)]) { return this.$scopedSlots[kebabCase.kebabCase(iconType)](h); } if (this.$scopedSlots[camelCase.camelCase(iconType)]) { return this.$scopedSlots[camelCase.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)) { utils_event.emitEvent(this, "enter", tmpValue, { e: e }); } else { utils_event.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); utils_event.emitEvent(this, "keyup", tmpValue, { e: e }); }, handleKeypress: function handleKeypress(e) { if (this.tDisabled) return; var value = e.currentTarget.value; var tmpValue = this.getOutputValue(value); utils_event.emitEvent(this, "keypress", tmpValue, { e: e }); }, handlePaste: function handlePaste(e) { if (this.tDisabled) return; var clipData = e.clipboardData || window.clipboardData; utils_event.emitEvent(this, "paste", { e: e, pasteValue: clipData === null || clipData === void 0 ? void 0 : clipData.getData("text/plain") }); }, onHandleMousewheel: function onHandleMousewheel(e) { utils_event.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 : ""; utils_event.emitEvent(this, "change", val, { e: e, trigger: "clear" }); utils_event.emitEvent(this, "clear", { e: e }); }, emitFocus: function emitFocus(e) { this.inputValue = this.value; if (this.tDisabled) return; this.focused = true; utils_event.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"); utils_event.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); } utils_event.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; utils_event.emitEvent(this, "mouseenter", { e: e }); }, onInputMouseleave: function onInputMouseleave(e) { this.isHover = false; utils_event.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 _common_js_utils_helper.limitUnicodeMaxLength(inputValue, Number(maxlength)); } if (maxcharacter) { var r = utils_helper.getCharacterLength(inputValue, maxcharacter); if (_typeof__default["default"](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 = utils_helper.omit(this.$attrs, Object.keys(this.inputAttrs)); var wrapperEvents = utils_helper.omit(this.$listeners, [].concat(_toConsumableArray__default["default"](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 = utils_renderTnode.renderTNodeJSX(this, "label"); var suffix = utils_renderTnode.renderTNodeJSX(this, "suffix"); var limitNode = this.limitNumber && this.showLimitNumber ? h("div", { "class": ["".concat(this.classPrefix, "-input__limit-number"), _defineProperty__default["default"]({}, "".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: tdesignIconsVue.BrowseIcon, BrowseOffIcon: tdesignIconsVue.BrowseOffIcon, CloseCircleFilledIcon: tdesignIconsVue.CloseCircleFilledIcon }), BrowseIcon = _this$useGlobalIcon.BrowseIcon, BrowseOffIcon = _this$useGlobalIcon.BrowseOffIcon, CloseCircleFilledIcon = _this$useGlobalIcon.CloseCircleFilledIcon; if (this.type === "password") { if (this.renderType === "password") { suffixIcon = h(BrowseOffIcon, { "class": _defineProperty__default["default"]({}, "".concat(this.componentName, "__suffix-clear"), !this.tDisabled), "nativeOn": { "click": this.emitPassword } }); } else if (this.renderType === "text") { suffixIcon = h(BrowseIcon, { "class": _defineProperty__default["default"]({}, "".concat(this.componentName, "__suffix-clear"), !this.tDisabled), "nativeOn": { "click": this.emitPassword } }); } } if (this.showClear) { if (this.type === "password") { passwordIcon = h(CloseCircleFilledIcon, { "class": "".concat(this.componentName, "__suffix-clear"), "nativeOn": { "click": this.emitClear } }); } else { suffixIcon = h(CloseCircleFilledIcon, { "class": "".concat(this.componentName, "__suffix-clear"), "nativeOn": { "click": this.emitClear } }); } } var classes = [this.inputClasses, this.inputClass, _defineProperty__default["default"](_defineProperty__default["default"]({}, "".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.helper([{}, { "attrs": this.inputAttrs }, {}, { "on": inputEvents }, { "ref": "inputRef", "class": ["".concat(this.componentName, "__inner"), _defineProperty__default["default"]({}, "".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__default["default"]({}, "".concat(this.componentName, "__clear"), this.showClear)] }, [suffixIcon]) : null]); var tips = utils_renderTnode.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])]); } }); exports["default"] = _Input; //# sourceMappingURL=input.js.map