input-action-sheet.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <div class="input-action-sheet-wrapper" v-if="visible" :style="{position: position}">
  3. <div class="input-action-sheet-content">
  4. <div class="title">
  5. {{title}}
  6. </div>
  7. <div class="input-wrapper">
  8. <input class="input"
  9. v-model="inputVal"
  10. placeholder="0"
  11. autofocus
  12. @input="onValueInput"
  13. @blur="onValueBlur">
  14. </div>
  15. <div class="desc">
  16. {{desc}}
  17. </div>
  18. <div class="btn-wrapper">
  19. <div class="btn cancel" @click="cancel">
  20. {{cancelText}}
  21. </div>
  22. <div class="btn confirm" @click="confirm">
  23. {{confirmText}}
  24. </div>
  25. </div>
  26. </div>
  27. </div>
  28. </template>
  29. <script setup>
  30. import { ref, defineProps, defineEmits, watch } from "vue";
  31. let inputVal = ref('');
  32. const props = defineProps({
  33. visible: {
  34. type: Boolean,
  35. default: false
  36. },
  37. position: {
  38. type: String,
  39. default: 'fixed'
  40. },
  41. title: {
  42. type: String,
  43. default: '',
  44. },
  45. cancelText: {
  46. type: String,
  47. default: 'Cancel',
  48. },
  49. confirmText: {
  50. type: String,
  51. default: 'Continue',
  52. },
  53. desc: {
  54. type: String,
  55. defautl: ''
  56. }
  57. });
  58. watch(() => props.visible, (newVal) => {
  59. if(!newVal) {
  60. inputVal.value = ''
  61. }
  62. })
  63. const emits = defineEmits(["cancel", "confirm", "onInput"]);
  64. const onValueInput = () => {
  65. let val = inputValHandler();
  66. emits("onInput", { inputVal: val });
  67. return val;
  68. }
  69. const onValueBlur = () => {
  70. return inputValHandler();
  71. }
  72. const inputValHandler = () => {
  73. let val = inputVal.value;
  74. val = val.replace(/^\D*(\d*(?:\.\d{0,18})?).*$/g, '$1');
  75. if(val == '00') {
  76. val = '0'
  77. }
  78. if(val.indexOf('.') > -1){ //校验 例:00.12 => 0.12
  79. let arr = val.split('.');
  80. if(arr[0].startsWith('0')) {
  81. let num = +arr[0];
  82. val = num + '.' + arr[1];
  83. }
  84. }
  85. inputVal.value = val;
  86. return val;
  87. }
  88. const cancel = () => {
  89. emits("cancel", { });
  90. }
  91. const confirm = () => {
  92. if(inputVal.value > 0) {
  93. emits("confirm", { inputVal: inputVal.value });
  94. }
  95. }
  96. </script>
  97. <style scoped lang="scss">
  98. .input-action-sheet-wrapper {
  99. width: 100%;
  100. height: 100%;
  101. background-color: rgba(0, 0, 0, 0.5);
  102. position: fixed;
  103. top: 0;
  104. left: 0;
  105. .input-action-sheet-content {
  106. width: 335px;
  107. min-height: 186px;
  108. background: #fff;
  109. border-radius: 20px;
  110. padding: 20px 15px;
  111. box-sizing: border-box;
  112. position: absolute;
  113. left: 50%;
  114. top: 50%;
  115. z-index: 1000;
  116. transform: translate(-50%, -50%);
  117. .title {
  118. font-weight: 600;
  119. font-size: 16px;
  120. }
  121. .input-wrapper {
  122. width: 100%;
  123. border: 1px solid #DFDFDF;
  124. border-radius: 5px;
  125. box-sizing: border-box;
  126. height: 42px;
  127. margin: 17px 0 10px 0;
  128. .input {
  129. width: 100%;
  130. height: 100%;
  131. outline: none;
  132. border: none;
  133. padding: 0 10px;
  134. box-sizing: border-box;
  135. border-radius: 5px;
  136. font-weight: 600;
  137. font-size: 16px;
  138. }
  139. .input::placeholder {
  140. color: #B3B3B3;
  141. }
  142. }
  143. .desc {
  144. font-weight: 500;
  145. font-size: 13px;
  146. color: #888888;
  147. min-height: 40px;
  148. }
  149. .btn-wrapper {
  150. display: flex;
  151. justify-content: space-between;
  152. margin-top: 20px;
  153. .btn {
  154. width: 150px;
  155. height: 47px;
  156. display: flex;
  157. align-items: center;
  158. justify-content: center;
  159. font-weight: 500;
  160. font-size: 16px;
  161. box-sizing: border-box;
  162. border-radius: 1000px;
  163. cursor: pointer;
  164. }
  165. .cancel {
  166. border: 1px solid #CFCFCF;
  167. }
  168. .confirm {
  169. color: #fff;
  170. background: #1D9BF0;
  171. }
  172. }
  173. }
  174. }
  175. </style>