su-number-box.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. "use strict";
  2. const common_vendor = require("../../../common/vendor.js");
  3. const _sfc_main = {
  4. name: "UniNumberBox",
  5. emits: ["change", "input", "update:modelValue", "blur", "focus"],
  6. props: {
  7. value: {
  8. type: [Number, String],
  9. default: 1
  10. },
  11. modelValue: {
  12. type: [Number, String],
  13. default: 1
  14. },
  15. min: {
  16. type: Number,
  17. default: 0
  18. },
  19. max: {
  20. type: Number,
  21. default: 100
  22. },
  23. step: {
  24. type: Number,
  25. default: 1
  26. },
  27. background: {
  28. type: String,
  29. default: "#f5f5f5"
  30. },
  31. color: {
  32. type: String,
  33. default: "#333"
  34. },
  35. disabled: {
  36. type: Boolean,
  37. default: false
  38. },
  39. activity: {
  40. type: String,
  41. default: "none"
  42. }
  43. },
  44. data() {
  45. return {
  46. inputValue: 0
  47. };
  48. },
  49. watch: {
  50. value(val) {
  51. this.inputValue = +val;
  52. },
  53. modelValue(val) {
  54. this.inputValue = +val;
  55. }
  56. },
  57. created() {
  58. if (this.value === 1) {
  59. this.inputValue = +this.modelValue;
  60. }
  61. if (this.modelValue === 1) {
  62. this.inputValue = +this.value;
  63. }
  64. },
  65. methods: {
  66. _calcValue(type) {
  67. if (this.disabled) {
  68. return;
  69. }
  70. const scale = this._getDecimalScale();
  71. let value = this.inputValue * scale;
  72. let step = this.step * scale;
  73. if (type === "minus") {
  74. value -= step;
  75. if (value < this.min * scale) {
  76. return;
  77. }
  78. if (value > this.max * scale) {
  79. value = this.max * scale;
  80. }
  81. }
  82. if (type === "plus") {
  83. value += step;
  84. if (value > this.max * scale) {
  85. return;
  86. }
  87. if (value < this.min * scale) {
  88. value = this.min * scale;
  89. }
  90. }
  91. this.inputValue = (value / scale).toFixed(String(scale).length - 1);
  92. this.$emit("change", +this.inputValue);
  93. this.$emit("input", +this.inputValue);
  94. this.$emit("update:modelValue", +this.inputValue);
  95. },
  96. _getDecimalScale() {
  97. let scale = 1;
  98. if (~~this.step !== this.step) {
  99. scale = Math.pow(10, String(this.step).split(".")[1].length);
  100. }
  101. return scale;
  102. },
  103. _onBlur(event) {
  104. this.$emit("blur", event);
  105. let value = event.detail.value;
  106. if (!value) {
  107. return;
  108. }
  109. value = +value;
  110. if (value > this.max) {
  111. value = this.max;
  112. } else if (value < this.min) {
  113. value = this.min;
  114. }
  115. const scale = this._getDecimalScale();
  116. this.inputValue = value.toFixed(String(scale).length - 1);
  117. this.$emit("change", +this.inputValue);
  118. this.$emit("input", +this.inputValue);
  119. },
  120. _onFocus(event) {
  121. this.$emit("focus", event);
  122. }
  123. }
  124. };
  125. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  126. return {
  127. a: $data.inputValue <= $props.min || $props.disabled ? 1 : "",
  128. b: $props.activity === "groupon" ? 1 : "",
  129. c: $props.activity === "seckill" ? 1 : "",
  130. d: common_vendor.o(($event) => $options._calcValue("minus")),
  131. e: $props.disabled,
  132. f: common_vendor.o((...args) => $options._onFocus && $options._onFocus(...args)),
  133. g: common_vendor.o((...args) => $options._onBlur && $options._onBlur(...args)),
  134. h: $props.color,
  135. i: $data.inputValue,
  136. j: common_vendor.o(($event) => $data.inputValue = $event.detail.value),
  137. k: $data.inputValue >= $props.max || $props.disabled ? 1 : "",
  138. l: $props.activity === "groupon" ? 1 : "",
  139. m: $props.activity === "seckill" ? 1 : "",
  140. n: common_vendor.o(($event) => $options._calcValue("plus"))
  141. };
  142. }
  143. const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-5212235c"], ["__file", "D:/zx/mall-front-app/sheep/ui/su-number-box/su-number-box.vue"]]);
  144. wx.createComponent(Component);