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