index.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <view class="nut-ecard">
  3. <view class="nut-ecard__title">{{ chooseText || translate('chooseText') }}</view>
  4. <view class="nut-ecard__list">
  5. <view
  6. v-for="(item, index) in dataList"
  7. :key="index"
  8. :class="['nut-ecard__list__item', currentIndex == index ? 'active' : '']"
  9. @click="handleClick(item, index)"
  10. >
  11. {{ item.price }}
  12. </view>
  13. <view :class="['nut-ecard__list__input', currentIndex == 'input' ? 'active' : '']" @click="inputClick">
  14. <view>{{ otherValueText || translate('otherValueText') }}</view>
  15. <view class="nut-ecard__list__input--con">
  16. <input
  17. class="nut-ecard__list__input--input"
  18. type="text"
  19. v-model="inputValue"
  20. @input="change"
  21. :placeholder="placeholder || translate('placeholder')"
  22. />
  23. {{ suffix }}
  24. </view>
  25. </view>
  26. <view class="nut-ecard__list__step">
  27. <view>{{ suffix }}{{ money }}</view>
  28. <nut-inputnumber v-model="stepValue" :min="cardBuyMin" :max="cardBuyMax" @change="changeStep" />
  29. </view>
  30. </view>
  31. </view>
  32. </template>
  33. <script lang="ts">
  34. import { Ref, ref, watch } from 'vue';
  35. import { createComponent } from '@/packages/utils/create';
  36. const { create, translate } = createComponent('ecard');
  37. import InputNumber from '../inputnumber/index.vue';
  38. export default create({
  39. components: {
  40. [InputNumber.name]: InputNumber
  41. },
  42. props: {
  43. chooseText: {
  44. type: String,
  45. default: ''
  46. },
  47. otherValueText: {
  48. type: String,
  49. default: ''
  50. },
  51. dataList: {
  52. type: [Object, Array],
  53. default: () => {
  54. return [];
  55. }
  56. },
  57. cardAmountMin: {
  58. type: Number,
  59. default: 1
  60. },
  61. cardAmountMax: {
  62. type: Number,
  63. default: 9999
  64. },
  65. cardBuyMin: {
  66. type: Number,
  67. default: 1
  68. },
  69. cardBuyMax: {
  70. type: Number,
  71. default: 9999
  72. },
  73. modelValue: {
  74. type: [Number, String],
  75. default: 0
  76. },
  77. placeholder: {
  78. type: String,
  79. default: ''
  80. },
  81. suffix: {
  82. type: String,
  83. default: '¥'
  84. }
  85. },
  86. emits: ['inputChange', 'changeStep', 'inputClick', 'change', 'update:modelValue'],
  87. setup(props, { emit }) {
  88. const currentIndex: Ref<number | null | string> = ref(null);
  89. const currentValue: Ref<number | null | string | undefined> = ref(null);
  90. const inputValue: Ref<string | undefined | number> = ref('');
  91. const stepValue: Ref<number> = ref(props.cardAmountMin);
  92. const money: Ref<number | string | undefined> = ref(props.modelValue);
  93. const handleClick = (item: { price: number | string }, index: number) => {
  94. currentIndex.value = index;
  95. stepValue.value = props.cardAmountMin;
  96. currentValue.value = item.price;
  97. emit('change', item);
  98. emit('update:modelValue', item.price);
  99. };
  100. const change = (event: Event) => {
  101. let input = event.target as HTMLInputElement;
  102. let val = input.value.replace(/[^\d]/g, '');
  103. inputValue.value = val;
  104. currentValue.value = val;
  105. if (Number(val) > props.cardAmountMax) {
  106. inputValue.value = props.cardAmountMax;
  107. currentValue.value = props.cardAmountMax;
  108. }
  109. if (Number(val) < props.cardAmountMin) {
  110. inputValue.value = props.cardAmountMin;
  111. currentValue.value = props.cardAmountMin;
  112. }
  113. emit('inputChange', Number(inputValue.value));
  114. emit('update:modelValue', Number(inputValue.value));
  115. };
  116. const inputClick = () => {
  117. currentIndex.value = 'input';
  118. stepValue.value = props.cardAmountMin;
  119. currentValue.value = inputValue.value;
  120. emit('update:modelValue', inputValue.value);
  121. emit('inputClick');
  122. };
  123. const changeStep = (value: number) => {
  124. stepValue.value = value;
  125. emit('changeStep', stepValue.value, currentValue.value); // 返回数量, 返回当前选中值
  126. };
  127. watch(
  128. () => props.modelValue,
  129. (value) => {
  130. money.value = value;
  131. }
  132. );
  133. return {
  134. handleClick,
  135. changeStep,
  136. change,
  137. inputClick,
  138. stepValue,
  139. currentIndex,
  140. inputValue,
  141. money,
  142. translate
  143. };
  144. }
  145. });
  146. </script>