input2.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. var core = require('@vueuse/core');
  5. var lodashUnified = require('lodash-unified');
  6. var index$4 = require('../../icon/index.js');
  7. var iconsVue = require('@element-plus/icons-vue');
  8. var utils = require('./utils.js');
  9. var input = require('./input.js');
  10. var pluginVue_exportHelper = require('../../../_virtual/plugin-vue_export-helper.js');
  11. var index = require('../../../hooks/use-attrs/index.js');
  12. var useFormItem = require('../../form/src/hooks/use-form-item.js');
  13. var useFormCommonProps = require('../../form/src/hooks/use-form-common-props.js');
  14. var index$2 = require('../../../hooks/use-focus-controller/index.js');
  15. var icon = require('../../../utils/vue/icon.js');
  16. var index$3 = require('../../../hooks/use-composition/index.js');
  17. var index$5 = require('../../../hooks/use-cursor/index.js');
  18. var index$1 = require('../../../hooks/use-namespace/index.js');
  19. var error = require('../../../utils/error.js');
  20. var shared = require('@vue/shared');
  21. var event = require('../../../constants/event.js');
  22. const __default__ = vue.defineComponent({
  23. name: "ElInput",
  24. inheritAttrs: false
  25. });
  26. const _sfc_main = /* @__PURE__ */ vue.defineComponent({
  27. ...__default__,
  28. props: input.inputProps,
  29. emits: input.inputEmits,
  30. setup(__props, { expose, emit }) {
  31. const props = __props;
  32. const rawAttrs = vue.useAttrs();
  33. const attrs = index.useAttrs();
  34. const slots = vue.useSlots();
  35. const containerKls = vue.computed(() => [
  36. props.type === "textarea" ? nsTextarea.b() : nsInput.b(),
  37. nsInput.m(inputSize.value),
  38. nsInput.is("disabled", inputDisabled.value),
  39. nsInput.is("exceed", inputExceed.value),
  40. {
  41. [nsInput.b("group")]: slots.prepend || slots.append,
  42. [nsInput.m("prefix")]: slots.prefix || props.prefixIcon,
  43. [nsInput.m("suffix")]: slots.suffix || props.suffixIcon || props.clearable || props.showPassword,
  44. [nsInput.bm("suffix", "password-clear")]: showClear.value && showPwdVisible.value,
  45. [nsInput.b("hidden")]: props.type === "hidden"
  46. },
  47. rawAttrs.class
  48. ]);
  49. const wrapperKls = vue.computed(() => [
  50. nsInput.e("wrapper"),
  51. nsInput.is("focus", isFocused.value)
  52. ]);
  53. const { form: elForm, formItem: elFormItem } = useFormItem.useFormItem();
  54. const { inputId } = useFormItem.useFormItemInputId(props, {
  55. formItemContext: elFormItem
  56. });
  57. const inputSize = useFormCommonProps.useFormSize();
  58. const inputDisabled = useFormCommonProps.useFormDisabled();
  59. const nsInput = index$1.useNamespace("input");
  60. const nsTextarea = index$1.useNamespace("textarea");
  61. const input = vue.shallowRef();
  62. const textarea = vue.shallowRef();
  63. const hovering = vue.ref(false);
  64. const passwordVisible = vue.ref(false);
  65. const countStyle = vue.ref();
  66. const textareaCalcStyle = vue.shallowRef(props.inputStyle);
  67. const _ref = vue.computed(() => input.value || textarea.value);
  68. const { wrapperRef, isFocused, handleFocus, handleBlur } = index$2.useFocusController(_ref, {
  69. beforeFocus() {
  70. return inputDisabled.value;
  71. },
  72. afterBlur() {
  73. var _a;
  74. if (props.validateEvent) {
  75. (_a = elFormItem == null ? void 0 : elFormItem.validate) == null ? void 0 : _a.call(elFormItem, "blur").catch((err) => error.debugWarn(err));
  76. }
  77. }
  78. });
  79. const needStatusIcon = vue.computed(() => {
  80. var _a;
  81. return (_a = elForm == null ? void 0 : elForm.statusIcon) != null ? _a : false;
  82. });
  83. const validateState = vue.computed(() => (elFormItem == null ? void 0 : elFormItem.validateState) || "");
  84. const validateIcon = vue.computed(() => validateState.value && icon.ValidateComponentsMap[validateState.value]);
  85. const passwordIcon = vue.computed(() => passwordVisible.value ? iconsVue.View : iconsVue.Hide);
  86. const containerStyle = vue.computed(() => [
  87. rawAttrs.style
  88. ]);
  89. const textareaStyle = vue.computed(() => [
  90. props.inputStyle,
  91. textareaCalcStyle.value,
  92. { resize: props.resize }
  93. ]);
  94. const nativeInputValue = vue.computed(() => lodashUnified.isNil(props.modelValue) ? "" : String(props.modelValue));
  95. const showClear = vue.computed(() => props.clearable && !inputDisabled.value && !props.readonly && !!nativeInputValue.value && (isFocused.value || hovering.value));
  96. const showPwdVisible = vue.computed(() => props.showPassword && !inputDisabled.value && !!nativeInputValue.value && (!!nativeInputValue.value || isFocused.value));
  97. const isWordLimitVisible = vue.computed(() => props.showWordLimit && !!props.maxlength && (props.type === "text" || props.type === "textarea") && !inputDisabled.value && !props.readonly && !props.showPassword);
  98. const textLength = vue.computed(() => nativeInputValue.value.length);
  99. const inputExceed = vue.computed(() => !!isWordLimitVisible.value && textLength.value > Number(props.maxlength));
  100. const suffixVisible = vue.computed(() => !!slots.suffix || !!props.suffixIcon || showClear.value || props.showPassword || isWordLimitVisible.value || !!validateState.value && needStatusIcon.value);
  101. const [recordCursor, setCursor] = index$5.useCursor(input);
  102. core.useResizeObserver(textarea, (entries) => {
  103. onceInitSizeTextarea();
  104. if (!isWordLimitVisible.value || props.resize !== "both")
  105. return;
  106. const entry = entries[0];
  107. const { width } = entry.contentRect;
  108. countStyle.value = {
  109. right: `calc(100% - ${width + 15 + 6}px)`
  110. };
  111. });
  112. const resizeTextarea = () => {
  113. const { type, autosize } = props;
  114. if (!core.isClient || type !== "textarea" || !textarea.value)
  115. return;
  116. if (autosize) {
  117. const minRows = shared.isObject(autosize) ? autosize.minRows : void 0;
  118. const maxRows = shared.isObject(autosize) ? autosize.maxRows : void 0;
  119. const textareaStyle2 = utils.calcTextareaHeight(textarea.value, minRows, maxRows);
  120. textareaCalcStyle.value = {
  121. overflowY: "hidden",
  122. ...textareaStyle2
  123. };
  124. vue.nextTick(() => {
  125. textarea.value.offsetHeight;
  126. textareaCalcStyle.value = textareaStyle2;
  127. });
  128. } else {
  129. textareaCalcStyle.value = {
  130. minHeight: utils.calcTextareaHeight(textarea.value).minHeight
  131. };
  132. }
  133. };
  134. const createOnceInitResize = (resizeTextarea2) => {
  135. let isInit = false;
  136. return () => {
  137. var _a;
  138. if (isInit || !props.autosize)
  139. return;
  140. const isElHidden = ((_a = textarea.value) == null ? void 0 : _a.offsetParent) === null;
  141. if (!isElHidden) {
  142. resizeTextarea2();
  143. isInit = true;
  144. }
  145. };
  146. };
  147. const onceInitSizeTextarea = createOnceInitResize(resizeTextarea);
  148. const setNativeInputValue = () => {
  149. const input2 = _ref.value;
  150. const formatterValue = props.formatter ? props.formatter(nativeInputValue.value) : nativeInputValue.value;
  151. if (!input2 || input2.value === formatterValue)
  152. return;
  153. input2.value = formatterValue;
  154. };
  155. const handleInput = async (event$1) => {
  156. recordCursor();
  157. let { value } = event$1.target;
  158. if (props.formatter) {
  159. value = props.parser ? props.parser(value) : value;
  160. }
  161. if (isComposing.value)
  162. return;
  163. if (value === nativeInputValue.value) {
  164. setNativeInputValue();
  165. return;
  166. }
  167. emit(event.UPDATE_MODEL_EVENT, value);
  168. emit("input", value);
  169. await vue.nextTick();
  170. setNativeInputValue();
  171. setCursor();
  172. };
  173. const handleChange = (event) => {
  174. emit("change", event.target.value);
  175. };
  176. const {
  177. isComposing,
  178. handleCompositionStart,
  179. handleCompositionUpdate,
  180. handleCompositionEnd
  181. } = index$3.useComposition({ emit, afterComposition: handleInput });
  182. const handlePasswordVisible = () => {
  183. recordCursor();
  184. passwordVisible.value = !passwordVisible.value;
  185. setTimeout(setCursor);
  186. };
  187. const focus = () => {
  188. var _a;
  189. return (_a = _ref.value) == null ? void 0 : _a.focus();
  190. };
  191. const blur = () => {
  192. var _a;
  193. return (_a = _ref.value) == null ? void 0 : _a.blur();
  194. };
  195. const handleMouseLeave = (evt) => {
  196. hovering.value = false;
  197. emit("mouseleave", evt);
  198. };
  199. const handleMouseEnter = (evt) => {
  200. hovering.value = true;
  201. emit("mouseenter", evt);
  202. };
  203. const handleKeydown = (evt) => {
  204. emit("keydown", evt);
  205. };
  206. const select = () => {
  207. var _a;
  208. (_a = _ref.value) == null ? void 0 : _a.select();
  209. };
  210. const clear = () => {
  211. emit(event.UPDATE_MODEL_EVENT, "");
  212. emit("change", "");
  213. emit("clear");
  214. emit("input", "");
  215. };
  216. vue.watch(() => props.modelValue, () => {
  217. var _a;
  218. vue.nextTick(() => resizeTextarea());
  219. if (props.validateEvent) {
  220. (_a = elFormItem == null ? void 0 : elFormItem.validate) == null ? void 0 : _a.call(elFormItem, "change").catch((err) => error.debugWarn(err));
  221. }
  222. });
  223. vue.watch(nativeInputValue, () => setNativeInputValue());
  224. vue.watch(() => props.type, async () => {
  225. await vue.nextTick();
  226. setNativeInputValue();
  227. resizeTextarea();
  228. });
  229. vue.onMounted(() => {
  230. if (!props.formatter && props.parser) {
  231. error.debugWarn("ElInput", "If you set the parser, you also need to set the formatter.");
  232. }
  233. setNativeInputValue();
  234. vue.nextTick(resizeTextarea);
  235. });
  236. expose({
  237. input,
  238. textarea,
  239. ref: _ref,
  240. textareaStyle,
  241. autosize: vue.toRef(props, "autosize"),
  242. isComposing,
  243. focus,
  244. blur,
  245. select,
  246. clear,
  247. resizeTextarea
  248. });
  249. return (_ctx, _cache) => {
  250. return vue.openBlock(), vue.createElementBlock("div", {
  251. class: vue.normalizeClass([
  252. vue.unref(containerKls),
  253. {
  254. [vue.unref(nsInput).bm("group", "append")]: _ctx.$slots.append,
  255. [vue.unref(nsInput).bm("group", "prepend")]: _ctx.$slots.prepend
  256. }
  257. ]),
  258. style: vue.normalizeStyle(vue.unref(containerStyle)),
  259. onMouseenter: handleMouseEnter,
  260. onMouseleave: handleMouseLeave
  261. }, [
  262. vue.createCommentVNode(" input "),
  263. _ctx.type !== "textarea" ? (vue.openBlock(), vue.createElementBlock(vue.Fragment, { key: 0 }, [
  264. vue.createCommentVNode(" prepend slot "),
  265. _ctx.$slots.prepend ? (vue.openBlock(), vue.createElementBlock("div", {
  266. key: 0,
  267. class: vue.normalizeClass(vue.unref(nsInput).be("group", "prepend"))
  268. }, [
  269. vue.renderSlot(_ctx.$slots, "prepend")
  270. ], 2)) : vue.createCommentVNode("v-if", true),
  271. vue.createElementVNode("div", {
  272. ref_key: "wrapperRef",
  273. ref: wrapperRef,
  274. class: vue.normalizeClass(vue.unref(wrapperKls))
  275. }, [
  276. vue.createCommentVNode(" prefix slot "),
  277. _ctx.$slots.prefix || _ctx.prefixIcon ? (vue.openBlock(), vue.createElementBlock("span", {
  278. key: 0,
  279. class: vue.normalizeClass(vue.unref(nsInput).e("prefix"))
  280. }, [
  281. vue.createElementVNode("span", {
  282. class: vue.normalizeClass(vue.unref(nsInput).e("prefix-inner"))
  283. }, [
  284. vue.renderSlot(_ctx.$slots, "prefix"),
  285. _ctx.prefixIcon ? (vue.openBlock(), vue.createBlock(vue.unref(index$4.ElIcon), {
  286. key: 0,
  287. class: vue.normalizeClass(vue.unref(nsInput).e("icon"))
  288. }, {
  289. default: vue.withCtx(() => [
  290. (vue.openBlock(), vue.createBlock(vue.resolveDynamicComponent(_ctx.prefixIcon)))
  291. ]),
  292. _: 1
  293. }, 8, ["class"])) : vue.createCommentVNode("v-if", true)
  294. ], 2)
  295. ], 2)) : vue.createCommentVNode("v-if", true),
  296. vue.createElementVNode("input", vue.mergeProps({
  297. id: vue.unref(inputId),
  298. ref_key: "input",
  299. ref: input,
  300. class: vue.unref(nsInput).e("inner")
  301. }, vue.unref(attrs), {
  302. minlength: _ctx.minlength,
  303. maxlength: _ctx.maxlength,
  304. type: _ctx.showPassword ? passwordVisible.value ? "text" : "password" : _ctx.type,
  305. disabled: vue.unref(inputDisabled),
  306. readonly: _ctx.readonly,
  307. autocomplete: _ctx.autocomplete,
  308. tabindex: _ctx.tabindex,
  309. "aria-label": _ctx.ariaLabel,
  310. placeholder: _ctx.placeholder,
  311. style: _ctx.inputStyle,
  312. form: _ctx.form,
  313. autofocus: _ctx.autofocus,
  314. role: _ctx.containerRole,
  315. onCompositionstart: vue.unref(handleCompositionStart),
  316. onCompositionupdate: vue.unref(handleCompositionUpdate),
  317. onCompositionend: vue.unref(handleCompositionEnd),
  318. onInput: handleInput,
  319. onChange: handleChange,
  320. onKeydown: handleKeydown
  321. }), null, 16, ["id", "minlength", "maxlength", "type", "disabled", "readonly", "autocomplete", "tabindex", "aria-label", "placeholder", "form", "autofocus", "role", "onCompositionstart", "onCompositionupdate", "onCompositionend"]),
  322. vue.createCommentVNode(" suffix slot "),
  323. vue.unref(suffixVisible) ? (vue.openBlock(), vue.createElementBlock("span", {
  324. key: 1,
  325. class: vue.normalizeClass(vue.unref(nsInput).e("suffix"))
  326. }, [
  327. vue.createElementVNode("span", {
  328. class: vue.normalizeClass(vue.unref(nsInput).e("suffix-inner"))
  329. }, [
  330. !vue.unref(showClear) || !vue.unref(showPwdVisible) || !vue.unref(isWordLimitVisible) ? (vue.openBlock(), vue.createElementBlock(vue.Fragment, { key: 0 }, [
  331. vue.renderSlot(_ctx.$slots, "suffix"),
  332. _ctx.suffixIcon ? (vue.openBlock(), vue.createBlock(vue.unref(index$4.ElIcon), {
  333. key: 0,
  334. class: vue.normalizeClass(vue.unref(nsInput).e("icon"))
  335. }, {
  336. default: vue.withCtx(() => [
  337. (vue.openBlock(), vue.createBlock(vue.resolveDynamicComponent(_ctx.suffixIcon)))
  338. ]),
  339. _: 1
  340. }, 8, ["class"])) : vue.createCommentVNode("v-if", true)
  341. ], 64)) : vue.createCommentVNode("v-if", true),
  342. vue.unref(showClear) ? (vue.openBlock(), vue.createBlock(vue.unref(index$4.ElIcon), {
  343. key: 1,
  344. class: vue.normalizeClass([vue.unref(nsInput).e("icon"), vue.unref(nsInput).e("clear")]),
  345. onMousedown: vue.withModifiers(vue.unref(shared.NOOP), ["prevent"]),
  346. onClick: clear
  347. }, {
  348. default: vue.withCtx(() => [
  349. vue.createVNode(vue.unref(iconsVue.CircleClose))
  350. ]),
  351. _: 1
  352. }, 8, ["class", "onMousedown"])) : vue.createCommentVNode("v-if", true),
  353. vue.unref(showPwdVisible) ? (vue.openBlock(), vue.createBlock(vue.unref(index$4.ElIcon), {
  354. key: 2,
  355. class: vue.normalizeClass([vue.unref(nsInput).e("icon"), vue.unref(nsInput).e("password")]),
  356. onClick: handlePasswordVisible
  357. }, {
  358. default: vue.withCtx(() => [
  359. (vue.openBlock(), vue.createBlock(vue.resolveDynamicComponent(vue.unref(passwordIcon))))
  360. ]),
  361. _: 1
  362. }, 8, ["class"])) : vue.createCommentVNode("v-if", true),
  363. vue.unref(isWordLimitVisible) ? (vue.openBlock(), vue.createElementBlock("span", {
  364. key: 3,
  365. class: vue.normalizeClass(vue.unref(nsInput).e("count"))
  366. }, [
  367. vue.createElementVNode("span", {
  368. class: vue.normalizeClass(vue.unref(nsInput).e("count-inner"))
  369. }, vue.toDisplayString(vue.unref(textLength)) + " / " + vue.toDisplayString(_ctx.maxlength), 3)
  370. ], 2)) : vue.createCommentVNode("v-if", true),
  371. vue.unref(validateState) && vue.unref(validateIcon) && vue.unref(needStatusIcon) ? (vue.openBlock(), vue.createBlock(vue.unref(index$4.ElIcon), {
  372. key: 4,
  373. class: vue.normalizeClass([
  374. vue.unref(nsInput).e("icon"),
  375. vue.unref(nsInput).e("validateIcon"),
  376. vue.unref(nsInput).is("loading", vue.unref(validateState) === "validating")
  377. ])
  378. }, {
  379. default: vue.withCtx(() => [
  380. (vue.openBlock(), vue.createBlock(vue.resolveDynamicComponent(vue.unref(validateIcon))))
  381. ]),
  382. _: 1
  383. }, 8, ["class"])) : vue.createCommentVNode("v-if", true)
  384. ], 2)
  385. ], 2)) : vue.createCommentVNode("v-if", true)
  386. ], 2),
  387. vue.createCommentVNode(" append slot "),
  388. _ctx.$slots.append ? (vue.openBlock(), vue.createElementBlock("div", {
  389. key: 1,
  390. class: vue.normalizeClass(vue.unref(nsInput).be("group", "append"))
  391. }, [
  392. vue.renderSlot(_ctx.$slots, "append")
  393. ], 2)) : vue.createCommentVNode("v-if", true)
  394. ], 64)) : (vue.openBlock(), vue.createElementBlock(vue.Fragment, { key: 1 }, [
  395. vue.createCommentVNode(" textarea "),
  396. vue.createElementVNode("textarea", vue.mergeProps({
  397. id: vue.unref(inputId),
  398. ref_key: "textarea",
  399. ref: textarea,
  400. class: [vue.unref(nsTextarea).e("inner"), vue.unref(nsInput).is("focus", vue.unref(isFocused))]
  401. }, vue.unref(attrs), {
  402. minlength: _ctx.minlength,
  403. maxlength: _ctx.maxlength,
  404. tabindex: _ctx.tabindex,
  405. disabled: vue.unref(inputDisabled),
  406. readonly: _ctx.readonly,
  407. autocomplete: _ctx.autocomplete,
  408. style: vue.unref(textareaStyle),
  409. "aria-label": _ctx.ariaLabel,
  410. placeholder: _ctx.placeholder,
  411. form: _ctx.form,
  412. autofocus: _ctx.autofocus,
  413. rows: _ctx.rows,
  414. role: _ctx.containerRole,
  415. onCompositionstart: vue.unref(handleCompositionStart),
  416. onCompositionupdate: vue.unref(handleCompositionUpdate),
  417. onCompositionend: vue.unref(handleCompositionEnd),
  418. onInput: handleInput,
  419. onFocus: vue.unref(handleFocus),
  420. onBlur: vue.unref(handleBlur),
  421. onChange: handleChange,
  422. onKeydown: handleKeydown
  423. }), null, 16, ["id", "minlength", "maxlength", "tabindex", "disabled", "readonly", "autocomplete", "aria-label", "placeholder", "form", "autofocus", "rows", "role", "onCompositionstart", "onCompositionupdate", "onCompositionend", "onFocus", "onBlur"]),
  424. vue.unref(isWordLimitVisible) ? (vue.openBlock(), vue.createElementBlock("span", {
  425. key: 0,
  426. style: vue.normalizeStyle(countStyle.value),
  427. class: vue.normalizeClass(vue.unref(nsInput).e("count"))
  428. }, vue.toDisplayString(vue.unref(textLength)) + " / " + vue.toDisplayString(_ctx.maxlength), 7)) : vue.createCommentVNode("v-if", true)
  429. ], 64))
  430. ], 38);
  431. };
  432. }
  433. });
  434. var Input = /* @__PURE__ */ pluginVue_exportHelper["default"](_sfc_main, [["__file", "input.vue"]]);
  435. exports["default"] = Input;
  436. //# sourceMappingURL=input2.js.map