uni-easyinput.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. "use strict";
  2. const common_vendor = require("../../../../common/vendor.js");
  3. const _sfc_main = {
  4. name: "uni-easyinput",
  5. emits: ["click", "iconClick", "update:modelValue", "input", "focus", "blur", "confirm"],
  6. model: {
  7. prop: "modelValue",
  8. event: "update:modelValue"
  9. },
  10. props: {
  11. name: String,
  12. value: [Number, String],
  13. modelValue: [Number, String],
  14. type: {
  15. type: String,
  16. default: "text"
  17. },
  18. clearable: {
  19. type: Boolean,
  20. default: true
  21. },
  22. autoHeight: {
  23. type: Boolean,
  24. default: false
  25. },
  26. placeholder: String,
  27. placeholderStyle: String,
  28. focus: {
  29. type: Boolean,
  30. default: false
  31. },
  32. disabled: {
  33. type: Boolean,
  34. default: false
  35. },
  36. maxlength: {
  37. type: [Number, String],
  38. default: 140
  39. },
  40. confirmType: {
  41. type: String,
  42. default: "done"
  43. },
  44. clearSize: {
  45. type: [Number, String],
  46. default: 15
  47. },
  48. inputBorder: {
  49. type: Boolean,
  50. default: true
  51. },
  52. prefixIcon: {
  53. type: String,
  54. default: ""
  55. },
  56. suffixIcon: {
  57. type: String,
  58. default: ""
  59. },
  60. trim: {
  61. type: [Boolean, String],
  62. default: true
  63. },
  64. passwordIcon: {
  65. type: Boolean,
  66. default: true
  67. },
  68. styles: {
  69. type: Object,
  70. default() {
  71. return {
  72. color: "#333",
  73. disableColor: "#F7F6F6",
  74. borderColor: "#e5e5e5"
  75. };
  76. }
  77. },
  78. errorMessage: {
  79. type: [String, Boolean],
  80. default: ""
  81. },
  82. paddingLeft: {
  83. type: [Number, String],
  84. default: 0
  85. }
  86. },
  87. data() {
  88. return {
  89. focused: false,
  90. errMsg: "",
  91. val: "",
  92. showMsg: "",
  93. border: false,
  94. isFirstBorder: false,
  95. showClearIcon: false,
  96. showPassword: false
  97. };
  98. },
  99. computed: {
  100. msg() {
  101. return this.errorMessage || this.errMsg;
  102. },
  103. // 因为uniapp的input组件的maxlength组件必须要数值,这里转为数值,用户可以传入字符串数值
  104. inputMaxlength() {
  105. return Number(this.maxlength);
  106. }
  107. },
  108. watch: {
  109. value(newVal) {
  110. if (this.errMsg)
  111. this.errMsg = "";
  112. this.val = newVal;
  113. if (this.form && this.formItem && !this.is_reset) {
  114. this.is_reset = false;
  115. this.formItem.setValue(newVal);
  116. }
  117. },
  118. modelValue(newVal) {
  119. if (this.errMsg)
  120. this.errMsg = "";
  121. this.val = newVal;
  122. if (this.form && this.formItem && !this.is_reset) {
  123. this.is_reset = false;
  124. this.formItem.setValue(newVal);
  125. }
  126. },
  127. focus(newVal) {
  128. this.$nextTick(() => {
  129. this.focused = this.focus;
  130. });
  131. }
  132. },
  133. created() {
  134. if (!this.value && this.value !== 0) {
  135. this.val = this.modelValue;
  136. }
  137. if (!this.modelValue && this.modelValue !== 0) {
  138. this.val = this.value;
  139. }
  140. this.form = this.getForm("uniForms");
  141. this.formItem = this.getForm("uniFormsItem");
  142. if (this.form && this.formItem) {
  143. if (this.formItem.name) {
  144. if (!this.is_reset) {
  145. this.is_reset = false;
  146. this.formItem.setValue(this.val);
  147. }
  148. this.rename = this.formItem.name;
  149. this.form.inputChildrens.push(this);
  150. }
  151. }
  152. },
  153. mounted() {
  154. this.$nextTick(() => {
  155. this.focused = this.focus;
  156. });
  157. },
  158. methods: {
  159. /**
  160. * 初始化变量值
  161. */
  162. init() {
  163. },
  164. onClickIcon(type) {
  165. this.$emit("iconClick", type);
  166. },
  167. /**
  168. * 获取父元素实例
  169. */
  170. getForm(name = "uniForms") {
  171. let parent = this.$parent;
  172. let parentName = parent.$options.name;
  173. while (parentName !== name) {
  174. parent = parent.$parent;
  175. if (!parent)
  176. return false;
  177. parentName = parent.$options.name;
  178. }
  179. return parent;
  180. },
  181. onEyes() {
  182. this.showPassword = !this.showPassword;
  183. },
  184. onInput(event) {
  185. let value = event.detail.value;
  186. if (this.trim) {
  187. if (typeof this.trim === "boolean" && this.trim) {
  188. value = this.trimStr(value);
  189. }
  190. if (typeof this.trim === "string") {
  191. value = this.trimStr(value, this.trim);
  192. }
  193. }
  194. if (this.errMsg)
  195. this.errMsg = "";
  196. this.val = value;
  197. this.$emit("input", value);
  198. this.$emit("update:modelValue", value);
  199. },
  200. onFocus(event) {
  201. this.$emit("focus", event);
  202. },
  203. onBlur(event) {
  204. event.detail.value;
  205. this.$emit("blur", event);
  206. },
  207. onConfirm(e) {
  208. this.$emit("confirm", e.detail.value);
  209. },
  210. onClear(event) {
  211. this.val = "";
  212. this.$emit("input", "");
  213. this.$emit("update:modelValue", "");
  214. },
  215. fieldClick() {
  216. this.$emit("click");
  217. },
  218. trimStr(str, pos = "both") {
  219. if (pos === "both") {
  220. return str.trim();
  221. } else if (pos === "left") {
  222. return str.trimLeft();
  223. } else if (pos === "right") {
  224. return str.trimRight();
  225. } else if (pos === "start") {
  226. return str.trimStart();
  227. } else if (pos === "end") {
  228. return str.trimEnd();
  229. } else if (pos === "all") {
  230. return str.replace(/\s+/g, "");
  231. } else if (pos === "none") {
  232. return str;
  233. }
  234. return str;
  235. }
  236. }
  237. };
  238. if (!Array) {
  239. const _easycom_uni_icons2 = common_vendor.resolveComponent("uni-icons");
  240. _easycom_uni_icons2();
  241. }
  242. const _easycom_uni_icons = () => "../../../uni-icons/components/uni-icons/uni-icons.js";
  243. if (!Math) {
  244. _easycom_uni_icons();
  245. }
  246. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  247. return common_vendor.e({
  248. a: $props.prefixIcon
  249. }, $props.prefixIcon ? {
  250. b: common_vendor.o(($event) => $options.onClickIcon("prefix")),
  251. c: common_vendor.p({
  252. type: $props.prefixIcon,
  253. color: "#c0c4cc"
  254. })
  255. } : {}, {
  256. d: $props.type === "textarea"
  257. }, $props.type === "textarea" ? {
  258. e: $props.inputBorder ? 1 : "",
  259. f: $props.name,
  260. g: $data.val,
  261. h: $props.placeholder,
  262. i: $props.placeholderStyle,
  263. j: $props.disabled,
  264. k: $options.inputMaxlength,
  265. l: $data.focused,
  266. m: $props.autoHeight,
  267. n: common_vendor.o((...args) => $options.onInput && $options.onInput(...args)),
  268. o: common_vendor.o((...args) => $options.onBlur && $options.onBlur(...args)),
  269. p: common_vendor.o((...args) => $options.onFocus && $options.onFocus(...args)),
  270. q: common_vendor.o((...args) => $options.onConfirm && $options.onConfirm(...args))
  271. } : {
  272. r: $props.type === "password" ? "text" : $props.type,
  273. s: $props.type === "password" || $props.clearable || $props.prefixIcon ? "" : "10px",
  274. t: $props.paddingLeft + "px",
  275. v: $props.name,
  276. w: $data.val,
  277. x: !$data.showPassword && $props.type === "password",
  278. y: $props.placeholder,
  279. z: $props.placeholderStyle,
  280. A: $props.disabled,
  281. B: $options.inputMaxlength,
  282. C: $data.focused,
  283. D: $props.confirmType,
  284. E: common_vendor.o((...args) => $options.onFocus && $options.onFocus(...args)),
  285. F: common_vendor.o((...args) => $options.onBlur && $options.onBlur(...args)),
  286. G: common_vendor.o((...args) => $options.onInput && $options.onInput(...args)),
  287. H: common_vendor.o((...args) => $options.onInput && $options.onInput(...args)),
  288. I: common_vendor.o((...args) => $options.onConfirm && $options.onConfirm(...args))
  289. }, {
  290. J: $props.type === "password" && $props.passwordIcon
  291. }, $props.type === "password" && $props.passwordIcon ? common_vendor.e({
  292. K: $data.val
  293. }, $data.val ? {
  294. L: $props.type === "textarea" ? 1 : "",
  295. M: common_vendor.o($options.onEyes),
  296. N: common_vendor.p({
  297. type: $data.showPassword ? "eye-slash-filled" : "eye-filled",
  298. size: 18,
  299. color: "#c0c4cc"
  300. })
  301. } : {}) : $props.suffixIcon ? common_vendor.e({
  302. P: $props.suffixIcon
  303. }, $props.suffixIcon ? {
  304. Q: common_vendor.o(($event) => $options.onClickIcon("suffix")),
  305. R: common_vendor.p({
  306. type: $props.suffixIcon,
  307. color: "#c0c4cc"
  308. })
  309. } : {}) : common_vendor.e({
  310. S: $props.clearable && $data.val && !$props.disabled
  311. }, $props.clearable && $data.val && !$props.disabled ? {
  312. T: $props.type === "textarea" ? 1 : "",
  313. U: common_vendor.o($options.onClear),
  314. V: common_vendor.p({
  315. type: "clear",
  316. size: $props.clearSize,
  317. color: "#c0c4cc"
  318. })
  319. } : {}), {
  320. O: $props.suffixIcon,
  321. W: $props.inputBorder ? 1 : "",
  322. X: $props.inputBorder && $options.msg ? 1 : "",
  323. Y: $props.type === "textarea" ? 1 : "",
  324. Z: $props.disabled ? 1 : "",
  325. aa: $props.inputBorder && $options.msg ? "#dd524d" : $props.styles.borderColor,
  326. ab: $props.disabled ? $props.styles.disableColor : "",
  327. ac: $options.msg ? 1 : "",
  328. ad: $props.inputBorder && $options.msg ? "#e43d33" : $props.styles.color
  329. });
  330. }
  331. const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__file", "/Users/RuHu.Xu/Desktop/mall-newfeifan-zx-app/uni_modules/uni-easyinput/components/uni-easyinput/uni-easyinput.vue"]]);
  332. wx.createComponent(Component);