index.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. function addNum (num1, num2) {
  2. let sq1, sq2, m;
  3. try {
  4. sq1 = num1.toString().split('.')[1].length;
  5. }
  6. catch (e) {
  7. sq1 = 0;
  8. }
  9. try {
  10. sq2 = num2.toString().split('.')[1].length;
  11. }
  12. catch (e) {
  13. sq2 = 0;
  14. }
  15. m = Math.pow(10, Math.max(sq1, sq2));
  16. return (Math.round(num1 * m) + Math.round(num2 * m)) / m;
  17. }
  18. Component({
  19. externalClasses: ['i-class'],
  20. properties: {
  21. // small || default || large
  22. size: String,
  23. value: {
  24. type: Number,
  25. value: 1
  26. },
  27. min: {
  28. type: Number,
  29. value: -Infinity
  30. },
  31. max: {
  32. type: Number,
  33. value: Infinity
  34. },
  35. step: {
  36. type: Number,
  37. value: 1
  38. }
  39. },
  40. methods: {
  41. handleChangeStep(e, type) {
  42. const { dataset = {} } = e.currentTarget;
  43. const { disabled } = dataset;
  44. const { step } = this.data;
  45. let { value } = this.data;
  46. if (disabled) return null;
  47. if (type === 'minus') {
  48. value = addNum(value, -step);
  49. } else if (type === 'plus') {
  50. value = addNum(value, step);
  51. }
  52. if (value < this.data.min || value > this.data.max) return null;
  53. this.handleEmit(value, type);
  54. },
  55. handleMinus(e) {
  56. this.handleChangeStep(e, 'minus');
  57. },
  58. handlePlus(e) {
  59. this.handleChangeStep(e, 'plus');
  60. },
  61. handleBlur(e) {
  62. let { value } = e.detail;
  63. const { min, max } = this.data;
  64. if (!value) {
  65. setTimeout(() => {
  66. this.handleEmit(value);
  67. }, 16);
  68. return;
  69. }
  70. value = +value;
  71. if (value > max) {
  72. value = max;
  73. } else if (value < min) {
  74. value = min;
  75. }
  76. this.handleEmit(value);
  77. },
  78. handleEmit (value, type) {
  79. const data = {
  80. value: value
  81. };
  82. if (type) data.type = type;
  83. this.triggerEvent('change', data);
  84. }
  85. }
  86. });