UNPKG

element-plus

Version:

A Component Library for Vue 3

592 lines (589 loc) 24.6 kB
import { defineComponent, useAttrs, useSlots, computed, shallowRef, ref, watch, nextTick, onMounted, toRef, openBlock, createElementBlock, normalizeClass, unref, normalizeStyle, createCommentVNode, Fragment, renderSlot, createElementVNode, createBlock, withCtx, resolveDynamicComponent, mergeProps, withModifiers, toDisplayString } from 'vue'; import { useResizeObserver, isClient } from '@vueuse/core'; import { isNil } from 'lodash-unified'; import { ElIcon } from '../../icon/index.mjs'; import { View, Hide } from '@element-plus/icons-vue'; import { looseToNumber, calcTextareaHeight } from './utils.mjs'; import { inputProps, inputEmits } from './input.mjs'; import _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs'; import { useAttrs as useAttrs$1 } from '../../../hooks/use-attrs/index.mjs'; import { useFormItem, useFormItemInputId } from '../../form/src/hooks/use-form-item.mjs'; import { useFormSize, useFormDisabled } from '../../form/src/hooks/use-form-common-props.mjs'; import { useFocusController } from '../../../hooks/use-focus-controller/index.mjs'; import { ValidateComponentsMap } from '../../../utils/vue/icon.mjs'; import { useComposition } from '../../../hooks/use-composition/index.mjs'; import { INPUT_EVENT, UPDATE_MODEL_EVENT, CHANGE_EVENT } from '../../../constants/event.mjs'; import { useCursor } from '../../../hooks/use-cursor/index.mjs'; import { useNamespace } from '../../../hooks/use-namespace/index.mjs'; import { debugWarn } from '../../../utils/error.mjs'; import { NOOP, isObject } from '@vue/shared'; const _hoisted_1 = ["id", "name", "minlength", "maxlength", "type", "disabled", "readonly", "autocomplete", "tabindex", "aria-label", "placeholder", "form", "autofocus", "role", "inputmode"]; const _hoisted_2 = ["id", "name", "minlength", "maxlength", "tabindex", "disabled", "readonly", "autocomplete", "aria-label", "placeholder", "form", "autofocus", "rows", "role"]; const COMPONENT_NAME = "ElInput"; const _sfc_main = defineComponent({ ...{ name: COMPONENT_NAME, inheritAttrs: false }, __name: "input", props: inputProps, emits: inputEmits, setup(__props, { expose: __expose, emit: __emit }) { const props = __props; const emit = __emit; const rawAttrs = useAttrs(); const attrs = useAttrs$1(); const slots = useSlots(); const containerKls = computed(() => [ props.type === "textarea" ? nsTextarea.b() : nsInput.b(), nsInput.m(inputSize.value), nsInput.is("disabled", inputDisabled.value), nsInput.is("exceed", inputExceed.value), { [nsInput.b("group")]: slots.prepend || slots.append, [nsInput.m("prefix")]: slots.prefix || props.prefixIcon, [nsInput.m("suffix")]: slots.suffix || props.suffixIcon || props.clearable || props.showPassword, [nsInput.bm("suffix", "password-clear")]: showClear.value && showPwdVisible.value, [nsInput.b("hidden")]: props.type === "hidden" }, rawAttrs.class ]); const wrapperKls = computed(() => [ nsInput.e("wrapper"), nsInput.is("focus", isFocused.value) ]); const { form: elForm, formItem: elFormItem } = useFormItem(); const { inputId } = useFormItemInputId(props, { formItemContext: elFormItem }); const inputSize = useFormSize(); const inputDisabled = useFormDisabled(); const nsInput = useNamespace("input"); const nsTextarea = useNamespace("textarea"); const input = shallowRef(); const textarea = shallowRef(); const hovering = ref(false); const passwordVisible = ref(false); const countStyle = ref(); const textareaCalcStyle = shallowRef(props.inputStyle); const _ref = computed(() => input.value || textarea.value); const { wrapperRef, isFocused, handleFocus, handleBlur } = useFocusController( _ref, { disabled: inputDisabled, afterBlur() { var _a; if (props.validateEvent) { (_a = elFormItem == null ? void 0 : elFormItem.validate) == null ? void 0 : _a.call(elFormItem, "blur").catch((err) => debugWarn(err)); } } } ); const needStatusIcon = computed(() => { var _a; return (_a = elForm == null ? void 0 : elForm.statusIcon) != null ? _a : false; }); const validateState = computed(() => (elFormItem == null ? void 0 : elFormItem.validateState) || ""); const validateIcon = computed( () => validateState.value && ValidateComponentsMap[validateState.value] ); const passwordIcon = computed( () => passwordVisible.value ? View : Hide ); const containerStyle = computed(() => [ rawAttrs.style ]); const textareaStyle = computed(() => [ props.inputStyle, textareaCalcStyle.value, { resize: props.resize } ]); const nativeInputValue = computed( () => isNil(props.modelValue) ? "" : String(props.modelValue) ); const showClear = computed( () => props.clearable && !inputDisabled.value && !props.readonly && !!nativeInputValue.value && (isFocused.value || hovering.value) ); const showPwdVisible = computed( () => props.showPassword && !inputDisabled.value && !!nativeInputValue.value ); const isWordLimitVisible = computed( () => props.showWordLimit && !!props.maxlength && (props.type === "text" || props.type === "textarea") && !inputDisabled.value && !props.readonly && !props.showPassword ); const textLength = computed(() => nativeInputValue.value.length); const inputExceed = computed( () => !!isWordLimitVisible.value && textLength.value > Number(props.maxlength) ); const suffixVisible = computed( () => !!slots.suffix || !!props.suffixIcon || showClear.value || props.showPassword || isWordLimitVisible.value || !!validateState.value && needStatusIcon.value ); const hasModelModifiers = computed( () => !!Object.keys(props.modelModifiers).length ); const [recordCursor, setCursor] = useCursor(input); useResizeObserver(textarea, (entries) => { onceInitSizeTextarea(); if (!isWordLimitVisible.value || props.resize !== "both" && props.resize !== "horizontal") return; const entry = entries[0]; const { width } = entry.contentRect; countStyle.value = { right: `calc(100% - ${width + 22 - 10}px)` }; }); const resizeTextarea = () => { const { type, autosize } = props; if (!isClient || type !== "textarea" || !textarea.value) return; if (autosize) { const minRows = isObject(autosize) ? autosize.minRows : void 0; const maxRows = isObject(autosize) ? autosize.maxRows : void 0; const textareaStyle2 = calcTextareaHeight(textarea.value, minRows, maxRows); textareaCalcStyle.value = { overflowY: "hidden", ...textareaStyle2 }; nextTick(() => { textarea.value.offsetHeight; textareaCalcStyle.value = textareaStyle2; }); } else { textareaCalcStyle.value = { minHeight: calcTextareaHeight(textarea.value).minHeight }; } }; const createOnceInitResize = (resizeTextarea2) => { let isInit = false; return () => { var _a; if (isInit || !props.autosize) return; const isElHidden = ((_a = textarea.value) == null ? void 0 : _a.offsetParent) === null; if (!isElHidden) { setTimeout(resizeTextarea2); isInit = true; } }; }; const onceInitSizeTextarea = createOnceInitResize(resizeTextarea); const setNativeInputValue = () => { const input2 = _ref.value; const formatterValue = props.formatter ? props.formatter(nativeInputValue.value) : nativeInputValue.value; if (!input2 || input2.value === formatterValue || props.type === "file") return; input2.value = formatterValue; }; const formatValue = (value) => { const { trim, number } = props.modelModifiers; if (trim) { value = value.trim(); } if (number) { value = `${looseToNumber(value)}`; } if (props.formatter && props.parser) { value = props.parser(value); } return value; }; const handleInput = async (event) => { if (isComposing.value) return; const { lazy } = props.modelModifiers; let { value } = event.target; if (lazy) { emit(INPUT_EVENT, value); return; } value = formatValue(value); if (String(value) === nativeInputValue.value) { if (props.formatter) { setNativeInputValue(); } return; } recordCursor(); emit(UPDATE_MODEL_EVENT, value); emit(INPUT_EVENT, value); await nextTick(); if (props.formatter && props.parser || !hasModelModifiers.value) { setNativeInputValue(); } setCursor(); }; const handleChange = async (event) => { let { value } = event.target; value = formatValue(value); if (props.modelModifiers.lazy) { emit(UPDATE_MODEL_EVENT, value); } emit(CHANGE_EVENT, value, event); await nextTick(); setNativeInputValue(); }; const { isComposing, handleCompositionStart, handleCompositionUpdate, handleCompositionEnd } = useComposition({ emit, afterComposition: handleInput }); const handlePasswordVisible = () => { passwordVisible.value = !passwordVisible.value; }; const focus = () => { var _a; return (_a = _ref.value) == null ? void 0 : _a.focus(); }; const blur = () => { var _a; return (_a = _ref.value) == null ? void 0 : _a.blur(); }; const handleMouseLeave = (evt) => { hovering.value = false; emit("mouseleave", evt); }; const handleMouseEnter = (evt) => { hovering.value = true; emit("mouseenter", evt); }; const handleKeydown = (evt) => { emit("keydown", evt); }; const select = () => { var _a; (_a = _ref.value) == null ? void 0 : _a.select(); }; const clear = () => { emit(UPDATE_MODEL_EVENT, ""); emit(CHANGE_EVENT, ""); emit("clear"); emit(INPUT_EVENT, ""); }; watch( () => props.modelValue, () => { var _a; nextTick(() => resizeTextarea()); if (props.validateEvent) { (_a = elFormItem == null ? void 0 : elFormItem.validate) == null ? void 0 : _a.call(elFormItem, "change").catch((err) => debugWarn(err)); } } ); watch(nativeInputValue, (newValue) => { if (!_ref.value) { return; } const { trim, number } = props.modelModifiers; const elValue = _ref.value.value; const displayValue = (number || props.type === "number") && !/^0\d/.test(elValue) ? `${looseToNumber(elValue)}` : elValue; if (displayValue === newValue) { return; } if (document.activeElement === _ref.value && _ref.value.type !== "range") { if (trim && displayValue.trim() === newValue) { return; } } setNativeInputValue(); }); watch( () => props.type, async () => { await nextTick(); setNativeInputValue(); resizeTextarea(); } ); onMounted(() => { if (!props.formatter && props.parser) { debugWarn( COMPONENT_NAME, "If you set the parser, you also need to set the formatter." ); } setNativeInputValue(); nextTick(resizeTextarea); }); __expose({ input, textarea, ref: _ref, textareaStyle, autosize: toRef(props, "autosize"), isComposing, focus, blur, select, clear, resizeTextarea }); return (_ctx, _cache) => { return openBlock(), createElementBlock( "div", { class: normalizeClass([ containerKls.value, { [unref(nsInput).bm("group", "append")]: _ctx.$slots.append, [unref(nsInput).bm("group", "prepend")]: _ctx.$slots.prepend } ]), style: normalizeStyle(containerStyle.value), onMouseenter: handleMouseEnter, onMouseleave: handleMouseLeave }, [ createCommentVNode(" input "), _ctx.type !== "textarea" ? (openBlock(), createElementBlock( Fragment, { key: 0 }, [ createCommentVNode(" prepend slot "), _ctx.$slots.prepend ? (openBlock(), createElementBlock( "div", { key: 0, class: normalizeClass(unref(nsInput).be("group", "prepend")) }, [ renderSlot(_ctx.$slots, "prepend") ], 2 )) : createCommentVNode("v-if", true), createElementVNode( "div", { ref_key: "wrapperRef", ref: wrapperRef, class: normalizeClass(wrapperKls.value) }, [ createCommentVNode(" prefix slot "), _ctx.$slots.prefix || _ctx.prefixIcon ? (openBlock(), createElementBlock( "span", { key: 0, class: normalizeClass(unref(nsInput).e("prefix")) }, [ createElementVNode( "span", { class: normalizeClass(unref(nsInput).e("prefix-inner")) }, [ renderSlot(_ctx.$slots, "prefix"), _ctx.prefixIcon ? (openBlock(), createBlock(unref(ElIcon), { key: 0, class: normalizeClass(unref(nsInput).e("icon")) }, { default: withCtx(() => [ (openBlock(), createBlock(resolveDynamicComponent(_ctx.prefixIcon))) ]), _: 1 }, 8, ["class"])) : createCommentVNode("v-if", true) ], 2 ) ], 2 )) : createCommentVNode("v-if", true), createElementVNode("input", mergeProps({ id: unref(inputId), ref_key: "input", ref: input, class: unref(nsInput).e("inner") }, unref(attrs), { name: _ctx.name, minlength: _ctx.minlength, maxlength: _ctx.maxlength, type: _ctx.showPassword ? passwordVisible.value ? "text" : "password" : _ctx.type, disabled: unref(inputDisabled), readonly: _ctx.readonly, autocomplete: _ctx.autocomplete, tabindex: _ctx.tabindex, "aria-label": _ctx.ariaLabel, placeholder: _ctx.placeholder, style: _ctx.inputStyle, form: _ctx.form, autofocus: _ctx.autofocus, role: _ctx.containerRole, inputmode: _ctx.inputmode, onCompositionstart: _cache[0] || (_cache[0] = (...args) => unref(handleCompositionStart) && unref(handleCompositionStart)(...args)), onCompositionupdate: _cache[1] || (_cache[1] = (...args) => unref(handleCompositionUpdate) && unref(handleCompositionUpdate)(...args)), onCompositionend: _cache[2] || (_cache[2] = (...args) => unref(handleCompositionEnd) && unref(handleCompositionEnd)(...args)), onInput: handleInput, onChange: handleChange, onKeydown: handleKeydown }), null, 16, _hoisted_1), createCommentVNode(" suffix slot "), suffixVisible.value ? (openBlock(), createElementBlock( "span", { key: 1, class: normalizeClass(unref(nsInput).e("suffix")) }, [ createElementVNode( "span", { class: normalizeClass(unref(nsInput).e("suffix-inner")) }, [ !showClear.value || !showPwdVisible.value || !isWordLimitVisible.value ? (openBlock(), createElementBlock( Fragment, { key: 0 }, [ renderSlot(_ctx.$slots, "suffix"), _ctx.suffixIcon ? (openBlock(), createBlock(unref(ElIcon), { key: 0, class: normalizeClass(unref(nsInput).e("icon")) }, { default: withCtx(() => [ (openBlock(), createBlock(resolveDynamicComponent(_ctx.suffixIcon))) ]), _: 1 }, 8, ["class"])) : createCommentVNode("v-if", true) ], 64 )) : createCommentVNode("v-if", true), showClear.value ? (openBlock(), createBlock(unref(ElIcon), { key: 1, class: normalizeClass([unref(nsInput).e("icon"), unref(nsInput).e("clear")]), onMousedown: withModifiers(unref(NOOP), ["prevent"]), onClick: clear }, { default: withCtx(() => [ (openBlock(), createBlock(resolveDynamicComponent(_ctx.clearIcon))) ]), _: 1 }, 8, ["class", "onMousedown"])) : createCommentVNode("v-if", true), showPwdVisible.value ? (openBlock(), createBlock(unref(ElIcon), { key: 2, class: normalizeClass([unref(nsInput).e("icon"), unref(nsInput).e("password")]), onClick: handlePasswordVisible, onMousedown: withModifiers(unref(NOOP), ["prevent"]), onMouseup: withModifiers(unref(NOOP), ["prevent"]) }, { default: withCtx(() => [ (openBlock(), createBlock(resolveDynamicComponent(passwordIcon.value))) ]), _: 1 }, 8, ["class", "onMousedown", "onMouseup"])) : createCommentVNode("v-if", true), isWordLimitVisible.value ? (openBlock(), createElementBlock( "span", { key: 3, class: normalizeClass([ unref(nsInput).e("count"), unref(nsInput).is("outside", _ctx.wordLimitPosition === "outside") ]) }, [ createElementVNode( "span", { class: normalizeClass(unref(nsInput).e("count-inner")) }, toDisplayString(textLength.value) + " / " + toDisplayString(_ctx.maxlength), 3 ) ], 2 )) : createCommentVNode("v-if", true), validateState.value && validateIcon.value && needStatusIcon.value ? (openBlock(), createBlock(unref(ElIcon), { key: 4, class: normalizeClass([ unref(nsInput).e("icon"), unref(nsInput).e("validateIcon"), unref(nsInput).is("loading", validateState.value === "validating") ]) }, { default: withCtx(() => [ (openBlock(), createBlock(resolveDynamicComponent(validateIcon.value))) ]), _: 1 }, 8, ["class"])) : createCommentVNode("v-if", true) ], 2 ) ], 2 )) : createCommentVNode("v-if", true) ], 2 ), createCommentVNode(" append slot "), _ctx.$slots.append ? (openBlock(), createElementBlock( "div", { key: 1, class: normalizeClass(unref(nsInput).be("group", "append")) }, [ renderSlot(_ctx.$slots, "append") ], 2 )) : createCommentVNode("v-if", true) ], 64 )) : (openBlock(), createElementBlock( Fragment, { key: 1 }, [ createCommentVNode(" textarea "), createElementVNode("textarea", mergeProps({ id: unref(inputId), ref_key: "textarea", ref: textarea, class: [unref(nsTextarea).e("inner"), unref(nsInput).is("focus", unref(isFocused))] }, unref(attrs), { name: _ctx.name, minlength: _ctx.minlength, maxlength: _ctx.maxlength, tabindex: _ctx.tabindex, disabled: unref(inputDisabled), readonly: _ctx.readonly, autocomplete: _ctx.autocomplete, style: textareaStyle.value, "aria-label": _ctx.ariaLabel, placeholder: _ctx.placeholder, form: _ctx.form, autofocus: _ctx.autofocus, rows: _ctx.rows, role: _ctx.containerRole, onCompositionstart: _cache[3] || (_cache[3] = (...args) => unref(handleCompositionStart) && unref(handleCompositionStart)(...args)), onCompositionupdate: _cache[4] || (_cache[4] = (...args) => unref(handleCompositionUpdate) && unref(handleCompositionUpdate)(...args)), onCompositionend: _cache[5] || (_cache[5] = (...args) => unref(handleCompositionEnd) && unref(handleCompositionEnd)(...args)), onInput: handleInput, onFocus: _cache[6] || (_cache[6] = (...args) => unref(handleFocus) && unref(handleFocus)(...args)), onBlur: _cache[7] || (_cache[7] = (...args) => unref(handleBlur) && unref(handleBlur)(...args)), onChange: handleChange, onKeydown: handleKeydown }), null, 16, _hoisted_2), isWordLimitVisible.value ? (openBlock(), createElementBlock( "span", { key: 0, style: normalizeStyle(countStyle.value), class: normalizeClass([ unref(nsInput).e("count"), unref(nsInput).is("outside", _ctx.wordLimitPosition === "outside") ]) }, toDisplayString(textLength.value) + " / " + toDisplayString(_ctx.maxlength), 7 )) : createCommentVNode("v-if", true) ], 64 )) ], 38 ); }; } }); var Input = /* @__PURE__ */ _export_sfc(_sfc_main, [["__file", "/home/runner/work/element-plus/element-plus/packages/components/input/src/input.vue"]]); export { Input as default }; //# sourceMappingURL=input2.mjs.map