u-number-box.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. "use strict";
  2. const uni_modules_uviewPlus_components_uNumberBox_props = require("./props.js");
  3. const uni_modules_uviewPlus_libs_mixin_mpMixin = require("../../libs/mixin/mpMixin.js");
  4. const uni_modules_uviewPlus_libs_mixin_mixin = require("../../libs/mixin/mixin.js");
  5. const uni_modules_uviewPlus_libs_function_index = require("../../libs/function/index.js");
  6. const common_vendor = require("../../../../common/vendor.js");
  7. const _sfc_main = {
  8. name: "u-number-box",
  9. mixins: [uni_modules_uviewPlus_libs_mixin_mpMixin.mpMixin, uni_modules_uviewPlus_libs_mixin_mixin.mixin, uni_modules_uviewPlus_components_uNumberBox_props.props],
  10. data() {
  11. return {
  12. // 输入框实际操作的值
  13. currentValue: "",
  14. // 定时器
  15. longPressTimer: null
  16. };
  17. },
  18. watch: {
  19. // 多个值之间,只要一个值发生变化,都要重新检查check()函数
  20. watchChange(n) {
  21. this.check();
  22. },
  23. // 监听v-mode的变化,重新初始化内部的值
  24. modelValue: {
  25. handler: function(newV, oldV) {
  26. if (newV !== this.currentValue) {
  27. this.currentValue = this.format(this.modelValue);
  28. }
  29. },
  30. immediate: true
  31. }
  32. },
  33. computed: {
  34. getCursorSpacing() {
  35. return uni_modules_uviewPlus_libs_function_index.getPx(this.cursorSpacing);
  36. },
  37. // 按钮的样式
  38. buttonStyle() {
  39. return (type) => {
  40. const style = {
  41. backgroundColor: this.bgColor,
  42. height: uni_modules_uviewPlus_libs_function_index.addUnit(this.buttonSize),
  43. color: this.color
  44. };
  45. if (this.isDisabled(type)) {
  46. style.backgroundColor = "#f7f8fa";
  47. }
  48. return style;
  49. };
  50. },
  51. // 输入框的样式
  52. inputStyle() {
  53. this.disabled || this.disabledInput;
  54. const style = {
  55. color: this.color,
  56. backgroundColor: this.bgColor,
  57. height: uni_modules_uviewPlus_libs_function_index.addUnit(this.buttonSize),
  58. width: uni_modules_uviewPlus_libs_function_index.addUnit(this.inputWidth)
  59. };
  60. return style;
  61. },
  62. // 用于监听多个值发生变化
  63. watchChange() {
  64. return [this.integer, this.decimalLength, this.min, this.max];
  65. },
  66. isDisabled() {
  67. return (type) => {
  68. if (type === "plus") {
  69. return this.disabled || this.disablePlus || this.currentValue >= this.max;
  70. }
  71. return this.disabled || this.disableMinus || this.currentValue <= this.min;
  72. };
  73. }
  74. },
  75. mounted() {
  76. this.init();
  77. },
  78. emits: ["update:modelValue", "focus", "blur", "overlimit", "change", "plus", "minus"],
  79. methods: {
  80. init() {
  81. this.currentValue = this.format(this.modelValue);
  82. },
  83. // 格式化整理数据,限制范围
  84. format(value) {
  85. value = this.filter(value);
  86. value = value === "" ? 0 : +value;
  87. value = Math.max(Math.min(this.max, value), this.min);
  88. if (this.decimalLength !== null) {
  89. value = value.toFixed(this.decimalLength);
  90. }
  91. return value;
  92. },
  93. // 过滤非法的字符
  94. filter(value) {
  95. value = String(value).replace(/[^0-9.-]/g, "");
  96. if (this.integer && value.indexOf(".") !== -1) {
  97. value = value.split(".")[0];
  98. }
  99. return value;
  100. },
  101. check() {
  102. const val = this.format(this.currentValue);
  103. if (val !== this.currentValue) {
  104. this.currentValue = val;
  105. }
  106. },
  107. // 判断是否出于禁止操作状态
  108. // isDisabled(type) {
  109. // if (type === 'plus') {
  110. // // 在点击增加按钮情况下,判断整体的disabled,是否单独禁用增加按钮,以及当前值是否大于最大的允许值
  111. // return (
  112. // this.disabled ||
  113. // this.disablePlus ||
  114. // this.currentValue >= this.max
  115. // )
  116. // }
  117. // // 点击减少按钮同理
  118. // return (
  119. // this.disabled ||
  120. // this.disableMinus ||
  121. // this.currentValue <= this.min
  122. // )
  123. // },
  124. // 输入框活动焦点
  125. onFocus(event) {
  126. this.$emit("focus", {
  127. ...event.detail,
  128. name: this.name
  129. });
  130. },
  131. // 输入框失去焦点
  132. onBlur(event) {
  133. this.format(event.detail.value);
  134. this.$emit(
  135. "blur",
  136. {
  137. ...event.detail,
  138. name: this.name
  139. }
  140. );
  141. },
  142. // 输入框值发生变化
  143. onInput(e) {
  144. const {
  145. value = ""
  146. } = e.detail || {};
  147. if (value === "")
  148. return;
  149. let formatted = this.filter(value);
  150. if (this.decimalLength !== null && formatted.indexOf(".") !== -1) {
  151. const pair = formatted.split(".");
  152. formatted = `${pair[0]}.${pair[1].slice(0, this.decimalLength)}`;
  153. }
  154. formatted = this.format(formatted);
  155. this.emitChange(formatted);
  156. return formatted;
  157. },
  158. // 发出change事件
  159. emitChange(value) {
  160. if (!this.asyncChange) {
  161. this.$nextTick(() => {
  162. this.$emit("update:modelValue", value);
  163. this.currentValue = value;
  164. this.$forceUpdate();
  165. });
  166. }
  167. this.$emit("change", {
  168. value,
  169. name: this.name
  170. });
  171. },
  172. onChange() {
  173. const {
  174. type
  175. } = this;
  176. if (this.isDisabled(type)) {
  177. return this.$emit("overlimit", type);
  178. }
  179. const diff = type === "minus" ? -this.step : +this.step;
  180. const value = this.format(this.add(+this.currentValue, diff));
  181. this.emitChange(value);
  182. this.$emit(type);
  183. },
  184. // 对值扩大后进行四舍五入,再除以扩大因子,避免出现浮点数操作的精度问题
  185. add(num1, num2) {
  186. const cardinal = Math.pow(10, 10);
  187. return Math.round((num1 + num2) * cardinal) / cardinal;
  188. },
  189. // 点击加减按钮
  190. clickHandler(type) {
  191. this.type = type;
  192. this.onChange();
  193. },
  194. longPressStep() {
  195. this.clearTimeout();
  196. this.longPressTimer = setTimeout(() => {
  197. this.onChange();
  198. this.longPressStep();
  199. }, 250);
  200. },
  201. onTouchStart(type) {
  202. if (!this.longPress)
  203. return;
  204. this.clearTimeout();
  205. this.type = type;
  206. this.longPressTimer = setTimeout(() => {
  207. this.onChange();
  208. this.longPressStep();
  209. }, 600);
  210. },
  211. // 触摸结束,清除定时器,停止长按加减
  212. onTouchEnd() {
  213. if (!this.longPress)
  214. return;
  215. this.clearTimeout();
  216. },
  217. // 清除定时器
  218. clearTimeout() {
  219. clearTimeout(this.longPressTimer);
  220. this.longPressTimer = null;
  221. }
  222. }
  223. };
  224. if (!Array) {
  225. const _easycom_u_icon2 = common_vendor.resolveComponent("u-icon");
  226. _easycom_u_icon2();
  227. }
  228. const _easycom_u_icon = () => "../u-icon/u-icon.js";
  229. if (!Math) {
  230. _easycom_u_icon();
  231. }
  232. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  233. return common_vendor.e({
  234. a: _ctx.showMinus && _ctx.$slots.minus
  235. }, _ctx.showMinus && _ctx.$slots.minus ? {
  236. b: common_vendor.o(($event) => $options.clickHandler("minus")),
  237. c: common_vendor.o(($event) => $options.onTouchStart("minus")),
  238. d: common_vendor.o((...args) => $options.clearTimeout && $options.clearTimeout(...args))
  239. } : _ctx.showMinus ? {
  240. f: common_vendor.p({
  241. name: "minus",
  242. color: $options.isDisabled("minus") ? "#c8c9cc" : "#323233",
  243. size: "15",
  244. bold: true,
  245. customStyle: _ctx.iconStyle
  246. }),
  247. g: common_vendor.o(($event) => $options.clickHandler("minus")),
  248. h: common_vendor.o(($event) => $options.onTouchStart("minus")),
  249. i: common_vendor.o((...args) => $options.clearTimeout && $options.clearTimeout(...args)),
  250. j: $options.isDisabled("minus") ? 1 : "",
  251. k: common_vendor.s($options.buttonStyle("minus"))
  252. } : {}, {
  253. e: _ctx.showMinus,
  254. l: _ctx.disabledInput || _ctx.disabled,
  255. m: $options.getCursorSpacing,
  256. n: _ctx.disabled || _ctx.disabledInput ? 1 : "",
  257. o: $data.currentValue,
  258. p: common_vendor.o((...args) => $options.onBlur && $options.onBlur(...args)),
  259. q: common_vendor.o((...args) => $options.onFocus && $options.onFocus(...args)),
  260. r: common_vendor.o((...args) => $options.onInput && $options.onInput(...args)),
  261. s: common_vendor.s($options.inputStyle),
  262. t: _ctx.showPlus && _ctx.$slots.plus
  263. }, _ctx.showPlus && _ctx.$slots.plus ? {
  264. v: common_vendor.o(($event) => $options.clickHandler("plus")),
  265. w: common_vendor.o(($event) => $options.onTouchStart("plus")),
  266. x: common_vendor.o((...args) => $options.clearTimeout && $options.clearTimeout(...args))
  267. } : _ctx.showPlus ? {
  268. z: common_vendor.p({
  269. name: "plus",
  270. color: $options.isDisabled("plus") ? "#c8c9cc" : "#323233",
  271. size: "15",
  272. bold: true,
  273. customStyle: _ctx.iconStyle
  274. }),
  275. A: common_vendor.o(($event) => $options.clickHandler("plus")),
  276. B: common_vendor.o(($event) => $options.onTouchStart("plus")),
  277. C: common_vendor.o((...args) => $options.clearTimeout && $options.clearTimeout(...args)),
  278. D: $options.isDisabled("plus") ? 1 : "",
  279. E: common_vendor.s($options.buttonStyle("plus"))
  280. } : {}, {
  281. y: _ctx.showPlus
  282. });
  283. }
  284. const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-eb6f6237"]]);
  285. wx.createComponent(Component);