input2.js 19 KB

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