index.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. exports.__esModule = true;
  4. exports["default"] = void 0;
  5. var _babelHelperVueJsxMergeProps = _interopRequireDefault(require("@vue/babel-helper-vue-jsx-merge-props"));
  6. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  7. var _icon = _interopRequireDefault(require("../icon"));
  8. var _cell = _interopRequireDefault(require("../cell"));
  9. var _shared = require("../cell/shared");
  10. var _utils = require("../utils");
  11. var _event = require("../utils/event");
  12. var _scroll = require("../utils/scroll");
  13. var _number = require("../utils/validate/number");
  14. var _use = (0, _utils.use)('field'),
  15. sfc = _use[0],
  16. bem = _use[1];
  17. var _default = sfc({
  18. inheritAttrs: false,
  19. props: (0, _extends2["default"])({}, _shared.cellProps, {
  20. error: Boolean,
  21. leftIcon: String,
  22. rightIcon: String,
  23. readonly: Boolean,
  24. clearable: Boolean,
  25. labelWidth: [String, Number],
  26. labelAlign: String,
  27. inputAlign: String,
  28. onIconClick: Function,
  29. autosize: [Boolean, Object],
  30. errorMessage: String,
  31. errorMessageAlign: String,
  32. type: {
  33. type: String,
  34. "default": 'text'
  35. }
  36. }),
  37. data: function data() {
  38. return {
  39. focused: false
  40. };
  41. },
  42. watch: {
  43. value: function value() {
  44. this.$nextTick(this.adjustSize);
  45. }
  46. },
  47. mounted: function mounted() {
  48. this.format();
  49. this.$nextTick(this.adjustSize);
  50. },
  51. computed: {
  52. showClear: function showClear() {
  53. return this.clearable && this.focused && this.value !== '' && (0, _utils.isDef)(this.value) && !this.readonly;
  54. },
  55. listeners: function listeners() {
  56. return (0, _extends2["default"])({}, this.$listeners, {
  57. input: this.onInput,
  58. keypress: this.onKeypress,
  59. focus: this.onFocus,
  60. blur: this.onBlur
  61. });
  62. },
  63. labelStyle: function labelStyle() {
  64. var labelWidth = this.labelWidth;
  65. if (labelWidth) {
  66. var width = (0, _number.isNumber)(String(labelWidth)) ? labelWidth + "px" : labelWidth;
  67. return {
  68. maxWidth: width,
  69. minWidth: width
  70. };
  71. }
  72. }
  73. },
  74. methods: {
  75. focus: function focus() {
  76. this.$refs.input && this.$refs.input.focus();
  77. },
  78. blur: function blur() {
  79. this.$refs.input && this.$refs.input.blur();
  80. },
  81. // native maxlength not work when type = number
  82. format: function format(target) {
  83. if (target === void 0) {
  84. target = this.$refs.input;
  85. }
  86. var _target = target,
  87. value = _target.value;
  88. var maxlength = this.$attrs.maxlength;
  89. if (this.type === 'number' && (0, _utils.isDef)(maxlength) && value.length > maxlength) {
  90. value = value.slice(0, maxlength);
  91. target.value = value;
  92. }
  93. return value;
  94. },
  95. onInput: function onInput(event) {
  96. this.$emit('input', this.format(event.target));
  97. },
  98. onFocus: function onFocus(event) {
  99. this.focused = true;
  100. this.$emit('focus', event); // hack for safari
  101. /* istanbul ignore if */
  102. if (this.readonly) {
  103. this.blur();
  104. }
  105. },
  106. onBlur: function onBlur(event) {
  107. this.focused = false;
  108. this.$emit('blur', event); // Hack for iOS12 page scroll
  109. // https://developers.weixin.qq.com/community/develop/doc/00044ae90742f8c82fb78fcae56800
  110. /* istanbul ignore next */
  111. if ((0, _utils.isIOS)()) {
  112. window.scrollTo(0, (0, _scroll.getRootScrollTop)());
  113. }
  114. },
  115. onClickLeftIcon: function onClickLeftIcon() {
  116. this.$emit('click-left-icon');
  117. },
  118. onClickRightIcon: function onClickRightIcon() {
  119. // compatible old version
  120. this.$emit('click-icon');
  121. this.$emit('click-right-icon');
  122. this.onIconClick && this.onIconClick();
  123. },
  124. onClear: function onClear(event) {
  125. (0, _event.preventDefault)(event);
  126. this.$emit('input', '');
  127. this.$emit('clear');
  128. },
  129. onKeypress: function onKeypress(event) {
  130. if (this.type === 'number') {
  131. var keyCode = event.keyCode;
  132. var allowPoint = String(this.value).indexOf('.') === -1;
  133. var isValidKey = keyCode >= 48 && keyCode <= 57 || keyCode === 46 && allowPoint || keyCode === 45;
  134. if (!isValidKey) {
  135. (0, _event.preventDefault)(event);
  136. }
  137. } // trigger blur after click keyboard search button
  138. /* istanbul ignore next */
  139. if (this.type === 'search' && event.keyCode === 13) {
  140. this.blur();
  141. }
  142. this.$emit('keypress', event);
  143. },
  144. adjustSize: function adjustSize() {
  145. var input = this.$refs.input;
  146. if (!(this.type === 'textarea' && this.autosize) || !input) {
  147. return;
  148. }
  149. input.style.height = 'auto';
  150. var height = input.scrollHeight;
  151. if ((0, _utils.isObj)(this.autosize)) {
  152. var _this$autosize = this.autosize,
  153. maxHeight = _this$autosize.maxHeight,
  154. minHeight = _this$autosize.minHeight;
  155. if (maxHeight) {
  156. height = Math.min(height, maxHeight);
  157. }
  158. if (minHeight) {
  159. height = Math.max(height, minHeight);
  160. }
  161. }
  162. if (height) {
  163. input.style.height = height + 'px';
  164. }
  165. },
  166. renderInput: function renderInput() {
  167. var h = this.$createElement;
  168. var inputProps = {
  169. ref: 'input',
  170. "class": bem('control', this.inputAlign),
  171. domProps: {
  172. value: this.value
  173. },
  174. attrs: (0, _extends2["default"])({}, this.$attrs, {
  175. readonly: this.readonly
  176. }),
  177. on: this.listeners
  178. };
  179. if (this.type === 'textarea') {
  180. return h("textarea", (0, _babelHelperVueJsxMergeProps["default"])([{}, inputProps]));
  181. }
  182. return h("input", (0, _babelHelperVueJsxMergeProps["default"])([{
  183. "attrs": {
  184. "type": this.type
  185. }
  186. }, inputProps]));
  187. },
  188. renderLeftIcon: function renderLeftIcon() {
  189. var h = this.$createElement;
  190. var showLeftIcon = this.slots('left-icon') || this.leftIcon;
  191. if (showLeftIcon) {
  192. return h("div", {
  193. "class": bem('left-icon'),
  194. "on": {
  195. "click": this.onClickLeftIcon
  196. }
  197. }, [this.slots('left-icon') || h(_icon["default"], {
  198. "attrs": {
  199. "name": this.leftIcon
  200. }
  201. })]);
  202. }
  203. },
  204. renderRightIcon: function renderRightIcon() {
  205. var h = this.$createElement;
  206. var slots = this.slots;
  207. var showRightIcon = slots('right-icon') || slots('icon') || this.rightIcon || this.icon;
  208. if (showRightIcon) {
  209. return h("div", {
  210. "class": bem('right-icon'),
  211. "on": {
  212. "click": this.onClickRightIcon
  213. }
  214. }, [slots('right-icon') || slots('icon') || h(_icon["default"], {
  215. "attrs": {
  216. "name": this.rightIcon || this.icon
  217. }
  218. })]);
  219. }
  220. }
  221. },
  222. render: function render(h) {
  223. var _bem;
  224. var slots = this.slots,
  225. labelAlign = this.labelAlign;
  226. var scopedSlots = {
  227. icon: this.renderLeftIcon
  228. };
  229. if (slots('label')) {
  230. scopedSlots.title = function () {
  231. return slots('label');
  232. };
  233. }
  234. return h(_cell["default"], {
  235. "attrs": {
  236. "icon": this.leftIcon,
  237. "size": this.size,
  238. "title": this.label,
  239. "center": this.center,
  240. "border": this.border,
  241. "isLink": this.isLink,
  242. "required": this.required,
  243. "titleStyle": this.labelStyle,
  244. "titleClass": bem('label', labelAlign)
  245. },
  246. "class": bem((_bem = {
  247. error: this.error,
  248. disabled: this.$attrs.disabled
  249. }, _bem["label-" + labelAlign] = labelAlign, _bem['min-height'] = this.type === 'textarea' && !this.autosize, _bem)),
  250. "scopedSlots": scopedSlots
  251. }, [h("div", {
  252. "class": bem('body')
  253. }, [this.renderInput(), this.showClear && h(_icon["default"], {
  254. "attrs": {
  255. "name": "clear"
  256. },
  257. "class": bem('clear'),
  258. "on": {
  259. "touchstart": this.onClear
  260. }
  261. }), this.renderRightIcon(), slots('button') && h("div", {
  262. "class": bem('button')
  263. }, [slots('button')])]), this.errorMessage && h("div", {
  264. "class": bem('error-message', this.errorMessageAlign)
  265. }, [this.errorMessage])]);
  266. }
  267. });
  268. exports["default"] = _default;