numberbox.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. data: {
  67. inputValue: 0
  68. },
  69. lifetimes: {
  70. attached: function() {
  71. this.setValue()
  72. }
  73. },
  74. methods: {
  75. setValue() {
  76. this.setData({
  77. inputValue: +this.data.value
  78. })
  79. },
  80. getScale() {
  81. let scale = 1;
  82. //浮点型
  83. if (this.data.step != parseInt(this.data.step)) {
  84. scale = Math.pow(10, (this.data.step + '').split('.')[1].length)
  85. }
  86. return scale;
  87. },
  88. calcNum: function(type) {
  89. if (this.data.disabled) {
  90. return
  91. }
  92. const scale = this.getScale()
  93. let num = this.data.value * scale
  94. let step = this.data.step * scale
  95. if (type === 'reduce') {
  96. num -= step
  97. } else if (type === 'plus') {
  98. num += step
  99. }
  100. let value = num / scale
  101. if (value < this.data.min || value > this.data.max) {
  102. return
  103. }
  104. this.handleChange(value, type)
  105. },
  106. plus: function() {
  107. this.calcNum("plus")
  108. },
  109. reduce: function() {
  110. this.calcNum("reduce")
  111. },
  112. blur: function(e) {
  113. let value = e.detail.value
  114. if (value) {
  115. value = +value
  116. if (value > this.data.max) {
  117. value = this.data.max
  118. } else if (value < this.data.min) {
  119. value = this.data.min
  120. }
  121. } else {
  122. value = this.data.min
  123. }
  124. if (value == this.data.value && value != this.data.inputValue) {
  125. this.setData({
  126. inputValue: value
  127. })
  128. }
  129. this.handleChange(value, "blur")
  130. },
  131. handleChange(value, type) {
  132. if (this.data.disabled) {
  133. return
  134. }
  135. this.triggerEvent('change', {
  136. value: value,
  137. type: type,
  138. index: this.data.index
  139. })
  140. }
  141. }
  142. })