index.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <template>
  2. <div class="hc-counter-box" :class="[block?'hc-counter-block':'', size, ui]">
  3. <div class="counter-box">
  4. <div class="counter-btn first" :disabled="modelValues <= 1" @click="moveBtnClick">-</div>
  5. <div class='counter-val w-20'>
  6. <span>{{modelValues}}</span>
  7. <span class="ml-2" v-if="text">{{text}}</span>
  8. </div>
  9. <div class="counter-btn end" @click="addBtnClick">+</div>
  10. </div>
  11. </div>
  12. </template>
  13. <script setup>
  14. import {nextTick, ref, watch} from "vue";
  15. const props = defineProps({
  16. ui: {
  17. type: String,
  18. default: ''
  19. },
  20. modelValue: {
  21. type: [String,Number],
  22. default: 1
  23. },
  24. text: {
  25. type: String,
  26. default: ''
  27. },
  28. block: {
  29. type: Boolean,
  30. default: false
  31. },
  32. size: {
  33. type: String,
  34. default: ''
  35. },
  36. })
  37. const modelValues = ref(1)
  38. nextTick(() => {
  39. setModelVal(props.modelValue)
  40. })
  41. //监听
  42. watch(() => [
  43. props.modelValue,
  44. ], ([val]) => {
  45. setModelVal(val)
  46. })
  47. //转换
  48. const setModelVal = (val) => {
  49. modelValues.value = Number(val)
  50. setEmitData(val)
  51. }
  52. const emit = defineEmits(['update:modelValue','addClick','moveClick', 'change'])
  53. //减少
  54. const moveBtnClick = () => {
  55. let val = modelValues.value - 1;
  56. if (val < 1) {
  57. modelValues.value = 1;
  58. } else {
  59. modelValues.value = val;
  60. setEmitData(val)
  61. }
  62. }
  63. //增加
  64. const addBtnClick = () => {
  65. let val = modelValues.value + 1;
  66. modelValues.value = val;
  67. setEmitData(val)
  68. }
  69. //事件
  70. const setEmitData = (val) => {
  71. emit('update:modelValue', val)
  72. emit('addClick', val)
  73. emit('change', val)
  74. }
  75. </script>
  76. <style lang="scss" scoped>
  77. .hc-counter-box {
  78. position: relative;
  79. display: inline-block;
  80. height: 32px;
  81. .counter-box {
  82. display: flex;
  83. align-items: center;
  84. height: inherit;
  85. color: #000000;
  86. .counter-btn {
  87. height: 32px;
  88. width: 32px;
  89. border: 1px solid #dddfe6;
  90. display: flex;
  91. align-items: center;
  92. justify-content: center;
  93. font-size: 22px;
  94. font-weight: 100;
  95. cursor: pointer;
  96. user-select: none;
  97. background-color: white;
  98. transition: color 0.2s, background-color 0.2s;
  99. &.first {
  100. border-radius: 4px 0 0 4px;
  101. }
  102. &.end {
  103. border-radius: 0 4px 4px 0;
  104. }
  105. &:hover {
  106. color: var(--el-color-primary);
  107. background-color: var(--el-color-primary-light-8);
  108. }
  109. &[disabled=true] {
  110. cursor: not-allowed;
  111. color: #c5c5c5;
  112. background-color: #f3f3f3;
  113. }
  114. }
  115. .counter-val {
  116. height: inherit;
  117. border-top: 1px solid #dddfe6;
  118. border-bottom: 1px solid #dddfe6;
  119. display: flex;
  120. align-items: center;
  121. justify-content: center;
  122. }
  123. }
  124. &.hc-counter-block {
  125. width: 100%;
  126. .counter-box {
  127. width: 100%;
  128. .counter-val {
  129. width: auto;
  130. flex: 1;
  131. }
  132. }
  133. }
  134. &.large {
  135. height: 40px;
  136. .counter-btn {
  137. height: 40px;
  138. width: 40px;
  139. font-size: 26px;
  140. }
  141. }
  142. }
  143. </style>