Input.js 7.8 KB

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