dk-stepper.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const Constants = require("@/utils/Constants");
  2. // components/dkbase/dk-stepper/dk-stepper.js
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. properties: {
  8. // 设置数字部分显示宽度
  9. inputWidth: {
  10. type: String,
  11. value: "32px"
  12. },
  13. value: {
  14. type: Number,
  15. observer: function (newVal) {
  16. this.setData({
  17. dataValue: newVal
  18. })
  19. }
  20. },
  21. min: {
  22. type: Number
  23. },
  24. decimalLength: {
  25. type: Number
  26. },
  27. step: {
  28. type: Number,
  29. value: 1
  30. },
  31. readonly: {
  32. type: Boolean,
  33. value: false
  34. },
  35. negative: {
  36. type: Boolean,
  37. value: true
  38. }
  39. },
  40. /**
  41. * 组件的初始数据
  42. */
  43. data: {
  44. inputTimer: null,
  45. dataValue: null,
  46. max: 0
  47. },
  48. /**
  49. * 组件的方法列表
  50. */
  51. methods: {
  52. /**
  53. * 步进器数值改变
  54. * @author 刘尧
  55. */
  56. changeStep(e) {
  57. let inputValue = this.data.negative ? ( Math.abs( e.detail.inputValue )* -1) : Number(e.detail.inputValue)
  58. if (this.data.decimalLength) {
  59. this.setData({
  60. dataValue: inputValue.toFixed(this.data.decimalLength),
  61. })
  62. } else {
  63. this.setData({
  64. dataValue: inputValue,
  65. })
  66. }
  67. // 触发自定义事件
  68. this.triggerEvent('change', this.data.dataValue);
  69. },
  70. /**
  71. * @desc : 减法
  72. * @author : 周兴
  73. * @date : 2024/2/2 11:46
  74. */
  75. handleMinus() {
  76. let dataValue = this.data.dataValue
  77. let min = this.data.min
  78. dataValue = dataValue - this.data.step
  79. if (dataValue < 0) {
  80. min = min * -1
  81. }
  82. // 当数值计算后小于最大值或大于最小值都可以触发减法
  83. if (dataValue >= min) {
  84. this.setData({
  85. dataValue: dataValue,
  86. })
  87. }
  88. // 触发自定义事件
  89. this.triggerEvent('change', this.data.dataValue);
  90. },
  91. /**
  92. * @desc : 加法
  93. * @author : 周兴
  94. * @date : 2024/2/2 11:46
  95. */
  96. handlePlus() {
  97. let dataValue = this.data.dataValue
  98. let max = this.data.max
  99. dataValue = dataValue + this.data.step
  100. if (dataValue <= max) {
  101. this.setData({
  102. dataValue: dataValue,
  103. })
  104. }
  105. // 触发自定义事件
  106. this.triggerEvent('change', this.data.dataValue);
  107. },
  108. },
  109. })