numberbox.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. Component({
  2. externalClasses: ['tui-numberbox-class'],
  3. properties: {
  4. value: {
  5. type: Number,
  6. value: 1,
  7. observer(val) {
  8. this.setValue()
  9. }
  10. },
  11. //最小值
  12. min: {
  13. type: Number,
  14. value: 0
  15. },
  16. //最大值
  17. max: {
  18. type: Number,
  19. value: 100
  20. },
  21. //迈步大小 1 1.1 10...
  22. step: {
  23. type: Number,
  24. value: 1
  25. },
  26. //是否禁用操作
  27. disabled: {
  28. type: Boolean,
  29. value: false
  30. },
  31. //加减图标大小 rpx
  32. iconsize: {
  33. type: Number,
  34. value: 24
  35. },
  36. iconcolor: {
  37. type: String,
  38. value: "#333"
  39. },
  40. //input 高度
  41. height: {
  42. type: Number,
  43. value: 50
  44. },
  45. //input 宽度
  46. width: {
  47. type: Number,
  48. value: 90
  49. },
  50. //input 背景颜色
  51. bgcolor: {
  52. type: String,
  53. value: "#f2f2f2"
  54. },
  55. //input 字体颜色
  56. color: {
  57. type: String,
  58. value: "#333"
  59. },
  60. //索引值,列表中使用
  61. index: {
  62. type: Number,
  63. value: 0
  64. },
  65. //自定义参数
  66. custom: {
  67. type: Number,
  68. value: 0
  69. }
  70. },
  71. data: {
  72. inputValue: 0
  73. },
  74. lifetimes: {
  75. attached: function() {
  76. this.setValue()
  77. }
  78. },
  79. methods: {
  80. setValue() {
  81. this.setData({
  82. inputValue: +this.data.value
  83. })
  84. },
  85. getScale() {
  86. let scale = 1;
  87. //浮点型
  88. if (this.data.step != parseInt(this.data.step)) {
  89. scale = Math.pow(10, (this.data.step + '').split('.')[1].length)
  90. }
  91. return scale;
  92. },
  93. calcNum: function(type) {
  94. if (this.data.disabled) {
  95. return
  96. }
  97. const scale = this.getScale()
  98. let num = this.data.value * scale
  99. let step = this.data.step * scale
  100. if (type === 'reduce') {
  101. num -= step
  102. } else if (type === 'plus') {
  103. num += step
  104. }
  105. let value = num / scale
  106. if (type === "plus" && value < this.data.min) {
  107. value = this.data.min
  108. } else if (type === "reduce" && value > this.data.max) {
  109. value = this.data.max
  110. }
  111. if (value < this.data.min || value > this.data.max) {
  112. return
  113. }
  114. this.handleChange(value, type)
  115. },
  116. plus: function() {
  117. this.calcNum("plus")
  118. },
  119. reduce: function() {
  120. this.calcNum("reduce")
  121. },
  122. blur: function(e) {
  123. let value = e.detail.value
  124. if (value) {
  125. value = +value
  126. if (value > this.data.max) {
  127. value = this.data.max
  128. } else if (value < this.data.min) {
  129. value = this.data.min
  130. }
  131. } else {
  132. value = this.data.min
  133. }
  134. if (value == this.data.value && value != this.data.inputValue) {
  135. this.setData({
  136. inputValue: value
  137. })
  138. }
  139. this.handleChange(value, "blur")
  140. },
  141. handleChange(value, type) {
  142. if (this.data.disabled) {
  143. return
  144. }
  145. this.triggerEvent('change', {
  146. value: value,
  147. type: type,
  148. index: this.data.index,
  149. custom: this.data.custom
  150. })
  151. }
  152. }
  153. })