lj-number.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <template>
  2. <div class="weui-cell__ft vux-cell-primary" :class="{'vux-number-round': buttonStyle === 'round'}"
  3. v-show="!readonly" style="font-size:0">
  4. <div :style="{float:align}">
  5. <a @click="sub" class="vux-number-selector vux-number-selector-sub"
  6. :class="{'vux-number-disabled':disabledMin,'vux-number-disabled':disable}">
  7. <svg viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"
  8. xmlns:xlink="http://www.w3.org/1999/xlink" width="16" height="16">
  9. <defs></defs>
  10. <path d="M863.74455 544.00086 163.424056 544.00086c-17.664722 0-32.00086-14.336138-32.00086-32.00086s14.336138-32.00086 32.00086-32.00086l700.320495 0c17.695686 0 31.99914 14.336138 31.99914 32.00086S881.440237 544.00086 863.74455 544.00086z"></path>
  11. </svg>
  12. </a>
  13. <label>
  14. <input v-model.number="currentValue" :name="name" class="vux-number-input" :disabled="disable" :style="{width: width}"
  15. :readonly="!fillable" pattern="[0-9]*" type="number" @blur="blur" style="font-size:16px;margin-bottom:5px"/>
  16. </label>
  17. <a @click="add" class="vux-number-selector vux-number-selector-plus"
  18. :class="{'vux-number-disabled':disabledMax,'vux-number-disabled':disable}">
  19. <svg viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"
  20. xmlns:xlink="http://www.w3.org/1999/xlink" width="18" height="18">
  21. <defs></defs>
  22. <path d="M863.328262 481.340895l-317.344013 0.099772L545.984249 162.816826c0-17.664722-14.336138-32.00086-32.00086-32.00086s-31.99914 14.336138-31.99914 32.00086l0 318.400215-322.368714-0.17718c-0.032684 0-0.063647 0-0.096331 0-17.632039 0-31.935493 14.239806-32.00086 31.904529-0.096331 17.664722 14.208843 32.031824 31.871845 32.095471l322.59234 0.17718 0 319.167424c0 17.695686 14.336138 32.00086 31.99914 32.00086s32.00086-14.303454 32.00086-32.00086L545.982529 545.440667l317.087703-0.099772c0.063647 0 0.096331 0 0.127295 0 17.632039 0 31.935493-14.239806 32.00086-31.904529S880.960301 481.404542 863.328262 481.340895z"></path>
  23. </svg>
  24. </a>
  25. </div>
  26. </div>
  27. </template>
  28. <script>
  29. export default {
  30. name: "lj-number",
  31. props: {
  32. min: Number,
  33. max: Number,
  34. readonly: Boolean,
  35. step: {
  36. type: Number,
  37. default: 1
  38. },
  39. value: {
  40. validator(value) {
  41. if (typeof value === 'number') {
  42. return true
  43. } else if (typeof value === 'string') {
  44. return value === ''
  45. }
  46. return false
  47. },
  48. default: 0
  49. },
  50. name: String,
  51. title: String,
  52. fillable: {
  53. type: Boolean,
  54. default: false
  55. },
  56. width: {
  57. type: String,
  58. default: '50px'
  59. },
  60. buttonStyle: {
  61. type: String,
  62. default: 'square'
  63. },
  64. align: {
  65. type: String,
  66. default: 'right'
  67. },
  68. disable:{
  69. type: Boolean,
  70. default: false
  71. }
  72. },
  73. created() {
  74. this.currentValue = this.value
  75. },
  76. data() {
  77. return {
  78. currentValue: 0
  79. }
  80. },
  81. computed: {
  82. disabledMin() {
  83. return typeof this.min === 'undefined' ? false : (this.currentValue === '' ? true : this.currentValue <= this.min)
  84. },
  85. disabledMax() {
  86. return typeof this.max === 'undefined' ? false : (this.currentValue === '' ? true : this.currentValue >= this.max)
  87. },
  88. labelClass() {
  89. return {
  90. 'vux-cell-justify': this.$parent.labelAlign === 'justify' || this.$parent.$parent.labelAlign === 'justify'
  91. }
  92. }
  93. },
  94. watch: {
  95. currentValue(newValue, old) {
  96. if (newValue !== '') {
  97. if (typeof this.min !== 'undefined' && this.currentValue < this.min) {
  98. this.currentValue = this.min
  99. }
  100. if (this.max && this.currentValue > this.max) {
  101. this.currentValue = this.max
  102. }
  103. }
  104. this.$emit('input', this.currentValue)
  105. },
  106. value(newValue) {
  107. this.currentValue = newValue
  108. this.$emit('on-change', newValue)
  109. }
  110. },
  111. methods: {
  112. add() {
  113. if (!this.disabledMax && !this.disable ) {
  114. // const x = new Big(this.currentValue)
  115. // this.currentValue = x.plus(this.step) * 1
  116. this.currentValue += this.step;
  117. this.currentValue = Math.round(this.currentValue * 100) / 100;
  118. }
  119. },
  120. sub() {
  121. if (!this.disabledMin && !this.disable) {
  122. // const x = new Big(this.currentValue)
  123. // this.currentValue = x.minus(this.step) * 1
  124. this.currentValue -= this.step;
  125. this.currentValue = Math.round(this.currentValue * 100) / 100;
  126. }
  127. },
  128. blur() {
  129. if (this.currentValue === '') {
  130. this.currentValue = 0
  131. }
  132. }
  133. }
  134. }
  135. </script>
  136. <style scoped>
  137. </style>