u-input.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. "use strict";
  2. const common_vendor = require("../../../../common/vendor.js");
  3. const _sfc_main = {
  4. name: "u-input",
  5. mixins: [common_vendor.mpMixin, common_vendor.mixin, common_vendor.props$18],
  6. data() {
  7. return {
  8. // 清除操作
  9. clearInput: false,
  10. // 输入框的值
  11. innerValue: "",
  12. // 是否处于获得焦点状态
  13. focused: false,
  14. // value是否第一次变化,在watch中,由于加入immediate属性,会在第一次触发,此时不应该认为value发生了变化
  15. firstChange: true,
  16. // value绑定值的变化是由内部还是外部引起的
  17. changeFromInner: false,
  18. // 过滤处理方法
  19. innerFormatter: (value) => value
  20. };
  21. },
  22. watch: {
  23. modelValue: {
  24. immediate: true,
  25. handler(newVal, oldVal) {
  26. this.innerValue = newVal;
  27. this.firstChange = false;
  28. this.changeFromInner = false;
  29. }
  30. }
  31. },
  32. computed: {
  33. // 是否显示清除控件
  34. isShowClear() {
  35. const { clearable, readonly, focused, innerValue } = this;
  36. return !!clearable && !readonly && !!focused && innerValue !== "";
  37. },
  38. // 组件的类名
  39. inputClass() {
  40. let classes = [], { border, disabled, shape } = this;
  41. border === "surround" && (classes = classes.concat(["u-border", "u-input--radius"]));
  42. classes.push(`u-input--${shape}`);
  43. border === "bottom" && (classes = classes.concat([
  44. "u-border-bottom",
  45. "u-input--no-radius"
  46. ]));
  47. return classes.join(" ");
  48. },
  49. // 组件的样式
  50. wrapperStyle() {
  51. const style = {};
  52. if (this.disabled) {
  53. style.backgroundColor = this.disabledColor;
  54. }
  55. if (this.border === "none") {
  56. style.padding = "0";
  57. } else {
  58. style.paddingTop = "6px";
  59. style.paddingBottom = "6px";
  60. style.paddingLeft = "9px";
  61. style.paddingRight = "9px";
  62. }
  63. return common_vendor.deepMerge(style, common_vendor.addStyle(this.customStyle));
  64. },
  65. // 输入框的样式
  66. inputStyle() {
  67. const style = {
  68. color: this.color,
  69. fontSize: common_vendor.addUnit(this.fontSize),
  70. textAlign: this.inputAlign
  71. };
  72. return style;
  73. }
  74. },
  75. emits: ["update:modelValue", "focus", "blur", "change", "confirm", "clear", "keyboardheightchange"],
  76. methods: {
  77. // 在微信小程序中,不支持将函数当做props参数,故只能通过ref形式调用
  78. setFormatter(e) {
  79. this.innerFormatter = e;
  80. },
  81. // 当键盘输入时,触发input事件
  82. onInput(e) {
  83. let { value = "" } = e.detail || {};
  84. const formatter = this.formatter || this.innerFormatter;
  85. const formatValue = formatter(value);
  86. this.innerValue = value;
  87. this.$nextTick(() => {
  88. this.innerValue = formatValue;
  89. this.valueChange();
  90. });
  91. },
  92. // 输入框失去焦点时触发
  93. onBlur(event) {
  94. this.$emit("blur", event.detail.value);
  95. common_vendor.sleep(150).then(() => {
  96. this.focused = false;
  97. });
  98. common_vendor.formValidate(this, "blur");
  99. },
  100. // 输入框聚焦时触发
  101. onFocus(event) {
  102. this.focused = true;
  103. this.$emit("focus");
  104. },
  105. // 点击完成按钮时触发
  106. onConfirm(event) {
  107. this.$emit("confirm", this.innerValue);
  108. },
  109. // 键盘高度发生变化的时候触发此事件
  110. // 兼容性:微信小程序2.7.0+、App 3.1.0+
  111. onkeyboardheightchange(event) {
  112. this.$emit("keyboardheightchange", event);
  113. },
  114. // 内容发生变化,进行处理
  115. valueChange() {
  116. if (this.clearInput) {
  117. this.innerValue = "";
  118. this.clearInput = false;
  119. }
  120. const value = this.innerValue;
  121. this.$nextTick(() => {
  122. this.$emit("update:modelValue", value);
  123. this.changeFromInner = true;
  124. this.$emit("change", value);
  125. common_vendor.formValidate(this, "change");
  126. });
  127. },
  128. // 点击清除控件
  129. onClear() {
  130. this.clearInput = true;
  131. this.innerValue = "";
  132. this.$nextTick(() => {
  133. this.valueChange();
  134. this.$emit("clear");
  135. });
  136. },
  137. /**
  138. * 在安卓nvue上,事件无法冒泡
  139. * 在某些时间,我们希望监听u-from-item的点击事件,此时会导致点击u-form-item内的u-input后
  140. * 无法触发u-form-item的点击事件,这里通过手动调用u-form-item的方法进行触发
  141. */
  142. clickHandler() {
  143. }
  144. }
  145. };
  146. if (!Array) {
  147. const _easycom_u_icon2 = common_vendor.resolveComponent("u-icon");
  148. _easycom_u_icon2();
  149. }
  150. const _easycom_u_icon = () => "../u-icon/u-icon.js";
  151. if (!Math) {
  152. _easycom_u_icon();
  153. }
  154. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  155. return common_vendor.e({
  156. a: _ctx.prefixIcon || _ctx.$slots.prefix
  157. }, _ctx.prefixIcon || _ctx.$slots.prefix ? {
  158. b: common_vendor.p({
  159. name: _ctx.prefixIcon,
  160. size: "18",
  161. customStyle: _ctx.prefixIconStyle
  162. })
  163. } : {}, {
  164. c: common_vendor.s($options.inputStyle),
  165. d: _ctx.type,
  166. e: _ctx.focus,
  167. f: _ctx.cursor,
  168. g: $data.innerValue,
  169. h: _ctx.autoBlur,
  170. i: _ctx.disabled || _ctx.readonly,
  171. j: _ctx.maxlength,
  172. k: _ctx.placeholder,
  173. l: _ctx.placeholderStyle,
  174. m: _ctx.placeholderClass,
  175. n: _ctx.confirmType,
  176. o: _ctx.confirmHold,
  177. p: _ctx.holdKeyboard,
  178. q: _ctx.cursorSpacing,
  179. r: _ctx.adjustPosition,
  180. s: _ctx.selectionEnd,
  181. t: _ctx.selectionStart,
  182. v: _ctx.password || _ctx.type === "password" || false,
  183. w: _ctx.ignoreCompositionEvent,
  184. x: common_vendor.o((...args) => $options.onInput && $options.onInput(...args)),
  185. y: common_vendor.o((...args) => $options.onBlur && $options.onBlur(...args)),
  186. z: common_vendor.o((...args) => $options.onFocus && $options.onFocus(...args)),
  187. A: common_vendor.o((...args) => $options.onConfirm && $options.onConfirm(...args)),
  188. B: common_vendor.o((...args) => $options.onkeyboardheightchange && $options.onkeyboardheightchange(...args)),
  189. C: common_vendor.o((...args) => $options.clickHandler && $options.clickHandler(...args)),
  190. D: $options.isShowClear
  191. }, $options.isShowClear ? {
  192. E: common_vendor.p({
  193. name: "close",
  194. size: "11",
  195. color: "#ffffff",
  196. customStyle: "line-height: 12px"
  197. }),
  198. F: common_vendor.o((...args) => $options.onClear && $options.onClear(...args))
  199. } : {}, {
  200. G: _ctx.suffixIcon || _ctx.$slots.suffix
  201. }, _ctx.suffixIcon || _ctx.$slots.suffix ? {
  202. H: common_vendor.p({
  203. name: _ctx.suffixIcon,
  204. size: "18",
  205. customStyle: _ctx.suffixIconStyle
  206. })
  207. } : {}, {
  208. I: common_vendor.n($options.inputClass),
  209. J: common_vendor.s($options.wrapperStyle)
  210. });
  211. }
  212. const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-5904192e"], ["__file", "D:/project/学吧/learn_applet/node_modules/uview-plus/components/u-input/u-input.vue"]]);
  213. wx.createComponent(Component);