Input.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import _extends from "@babel/runtime/helpers/esm/extends";
  3. import { resolveDirective as _resolveDirective, Fragment as _Fragment, createVNode as _createVNode } from "vue";
  4. var __rest = this && this.__rest || function (s, e) {
  5. var t = {};
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  7. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  8. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  9. }
  10. return t;
  11. };
  12. import { computed, onMounted, defineComponent, nextTick, shallowRef, watch } from 'vue';
  13. import classNames from '../_util/classNames';
  14. import omit from '../_util/omit';
  15. import { inputProps } from './inputProps';
  16. import { fixControlledValue, hasAddon, hasPrefixSuffix, resolveOnChange, triggerFocus } from './utils/commonUtils';
  17. import BaseInput from './BaseInput';
  18. import BaseInputCore from '../_util/BaseInput';
  19. export default defineComponent({
  20. name: 'VCInput',
  21. inheritAttrs: false,
  22. props: inputProps(),
  23. setup(props, _ref) {
  24. let {
  25. slots,
  26. attrs,
  27. expose,
  28. emit
  29. } = _ref;
  30. const stateValue = shallowRef(props.value === undefined ? props.defaultValue : props.value);
  31. const focused = shallowRef(false);
  32. const inputRef = shallowRef();
  33. const rootRef = shallowRef();
  34. watch(() => props.value, () => {
  35. stateValue.value = props.value;
  36. });
  37. watch(() => props.disabled, () => {
  38. if (props.disabled) {
  39. focused.value = false;
  40. }
  41. });
  42. const focus = option => {
  43. if (inputRef.value) {
  44. triggerFocus(inputRef.value.input, option);
  45. }
  46. };
  47. const blur = () => {
  48. var _a;
  49. (_a = inputRef.value.input) === null || _a === void 0 ? void 0 : _a.blur();
  50. };
  51. const setSelectionRange = (start, end, direction) => {
  52. var _a;
  53. (_a = inputRef.value.input) === null || _a === void 0 ? void 0 : _a.setSelectionRange(start, end, direction);
  54. };
  55. const select = () => {
  56. var _a;
  57. (_a = inputRef.value.input) === null || _a === void 0 ? void 0 : _a.select();
  58. };
  59. expose({
  60. focus,
  61. blur,
  62. input: computed(() => {
  63. var _a;
  64. return (_a = inputRef.value.input) === null || _a === void 0 ? void 0 : _a.input;
  65. }),
  66. stateValue,
  67. setSelectionRange,
  68. select
  69. });
  70. const triggerChange = e => {
  71. emit('change', e);
  72. };
  73. const setValue = (value, callback) => {
  74. if (stateValue.value === value) {
  75. return;
  76. }
  77. if (props.value === undefined) {
  78. stateValue.value = value;
  79. } else {
  80. nextTick(() => {
  81. var _a;
  82. if (inputRef.value.input.value !== stateValue.value) {
  83. (_a = rootRef.value) === null || _a === void 0 ? void 0 : _a.$forceUpdate();
  84. }
  85. });
  86. }
  87. nextTick(() => {
  88. callback && callback();
  89. });
  90. };
  91. const handleChange = e => {
  92. const {
  93. value
  94. } = e.target;
  95. if (stateValue.value === value) return;
  96. const newVal = e.target.value;
  97. resolveOnChange(inputRef.value.input, e, triggerChange);
  98. setValue(newVal);
  99. };
  100. const handleKeyDown = e => {
  101. if (e.keyCode === 13) {
  102. emit('pressEnter', e);
  103. }
  104. emit('keydown', e);
  105. };
  106. const handleFocus = e => {
  107. focused.value = true;
  108. emit('focus', e);
  109. };
  110. const handleBlur = e => {
  111. focused.value = false;
  112. emit('blur', e);
  113. };
  114. const handleReset = e => {
  115. resolveOnChange(inputRef.value.input, e, triggerChange);
  116. setValue('', () => {
  117. focus();
  118. });
  119. };
  120. const getInputElement = () => {
  121. var _a, _b;
  122. const {
  123. addonBefore = slots.addonBefore,
  124. addonAfter = slots.addonAfter,
  125. disabled,
  126. valueModifiers = {},
  127. htmlSize,
  128. autocomplete,
  129. prefixCls,
  130. inputClassName,
  131. prefix = (_a = slots.prefix) === null || _a === void 0 ? void 0 : _a.call(slots),
  132. suffix = (_b = slots.suffix) === null || _b === void 0 ? void 0 : _b.call(slots),
  133. allowClear,
  134. type = 'text'
  135. } = props;
  136. const otherProps = omit(props, ['prefixCls', 'onPressEnter', 'addonBefore', 'addonAfter', 'prefix', 'suffix', 'allowClear',
  137. // Input elements must be either controlled or uncontrolled,
  138. // specify either the value prop, or the defaultValue prop, but not both.
  139. 'defaultValue', 'size', 'bordered', 'htmlSize', 'lazy', 'showCount', 'valueModifiers', 'showCount', 'affixWrapperClassName', 'groupClassName', 'inputClassName', 'wrapperClassName']);
  140. const inputProps = _extends(_extends(_extends({}, otherProps), attrs), {
  141. autocomplete,
  142. onChange: handleChange,
  143. onInput: handleChange,
  144. onFocus: handleFocus,
  145. onBlur: handleBlur,
  146. onKeydown: handleKeyDown,
  147. class: classNames(prefixCls, {
  148. [`${prefixCls}-disabled`]: disabled
  149. }, inputClassName, !hasAddon({
  150. addonAfter,
  151. addonBefore
  152. }) && !hasPrefixSuffix({
  153. prefix,
  154. suffix,
  155. allowClear
  156. }) && attrs.class),
  157. ref: inputRef,
  158. key: 'ant-input',
  159. size: htmlSize,
  160. type,
  161. lazy: props.lazy
  162. });
  163. if (valueModifiers.lazy) {
  164. delete inputProps.onInput;
  165. }
  166. if (!inputProps.autofocus) {
  167. delete inputProps.autofocus;
  168. }
  169. const inputNode = _createVNode(BaseInputCore, omit(inputProps, ['size']), null);
  170. return inputNode;
  171. };
  172. const getSuffix = () => {
  173. var _a;
  174. const {
  175. maxlength,
  176. suffix = (_a = slots.suffix) === null || _a === void 0 ? void 0 : _a.call(slots),
  177. showCount,
  178. prefixCls
  179. } = props;
  180. // Max length value
  181. const hasMaxLength = Number(maxlength) > 0;
  182. if (suffix || showCount) {
  183. const valueLength = [...fixControlledValue(stateValue.value)].length;
  184. const dataCount = typeof showCount === 'object' ? showCount.formatter({
  185. count: valueLength,
  186. maxlength
  187. }) : `${valueLength}${hasMaxLength ? ` / ${maxlength}` : ''}`;
  188. return _createVNode(_Fragment, null, [!!showCount && _createVNode("span", {
  189. "class": classNames(`${prefixCls}-show-count-suffix`, {
  190. [`${prefixCls}-show-count-has-suffix`]: !!suffix
  191. })
  192. }, [dataCount]), suffix]);
  193. }
  194. return null;
  195. };
  196. onMounted(() => {
  197. if (process.env.NODE_ENV === 'test') {
  198. if (props.autofocus) {
  199. focus();
  200. }
  201. }
  202. });
  203. return () => {
  204. const {
  205. prefixCls,
  206. disabled
  207. } = props,
  208. rest = __rest(props, ["prefixCls", "disabled"]);
  209. return _createVNode(BaseInput, _objectSpread(_objectSpread(_objectSpread({}, rest), attrs), {}, {
  210. "ref": rootRef,
  211. "prefixCls": prefixCls,
  212. "inputElement": getInputElement(),
  213. "handleReset": handleReset,
  214. "value": fixControlledValue(stateValue.value),
  215. "focused": focused.value,
  216. "triggerFocus": focus,
  217. "suffix": getSuffix(),
  218. "disabled": disabled
  219. }), slots);
  220. };
  221. }
  222. });