uv-keyboard-number.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <template>
  2. <view
  3. class="uv-keyboard"
  4. @touchmove.stop.prevent="noop"
  5. >
  6. <view
  7. class="uv-keyboard__button-wrapper"
  8. v-for="(item, index) in numList"
  9. :key="index"
  10. >
  11. <view
  12. class="uv-keyboard__button-wrapper__button"
  13. :style="[itemStyle(index)]"
  14. @tap="keyboardClick(item)"
  15. hover-class="uv-hover-class"
  16. :hover-stay-time="200"
  17. >
  18. <text class="uv-keyboard__button-wrapper__button__text">{{ item }}</text>
  19. </view>
  20. </view>
  21. <view
  22. class="uv-keyboard__button-wrapper"
  23. >
  24. <view
  25. class="uv-keyboard__button-wrapper__button uv-keyboard__button-wrapper__button--gray"
  26. hover-class="uv-hover-class"
  27. :hover-stay-time="200"
  28. @touchstart.stop="backspaceClick"
  29. @touchend="clearTimer"
  30. >
  31. <uv-icon
  32. name="backspace"
  33. color="#303133"
  34. size="28"
  35. ></uv-icon>
  36. </view>
  37. </view>
  38. </view>
  39. </template>
  40. <script>
  41. import mpMixin from '@/uni_modules/uv-ui-tools/libs/mixin/mpMixin.js'
  42. import mixin from '@/uni_modules/uv-ui-tools/libs/mixin/mixin.js'
  43. import props from './props.js';
  44. /**
  45. * keyboard 键盘组件
  46. * @description
  47. * @tutorial
  48. * @property {String} mode 键盘的类型,number-数字键盘,card-身份证键盘
  49. * @property {Boolean} dotDisabled 是否显示键盘的"."符号
  50. * @property {Boolean} random 是否打乱键盘按键的顺序
  51. * @event {Function} change 点击键盘触发
  52. * @event {Function} backspace 点击退格键触发
  53. * @example
  54. */
  55. export default {
  56. mixins: [mpMixin, mixin, props],
  57. emits: ['backspace','change'],
  58. data() {
  59. return {
  60. backspace: 'backspace', // 退格键内容
  61. dot: '.', // 点
  62. timer: null, // 长按多次删除的事件监听
  63. cardX: 'X' // 身份证的X符号
  64. };
  65. },
  66. computed: {
  67. // 键盘需要显示的内容
  68. numList() {
  69. let tmp = [];
  70. if (this.dotDisabled && this.mode == 'number') {
  71. if (!this.random) {
  72. return [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
  73. } else {
  74. return this.$uv.randomArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]);
  75. }
  76. } else if (!this.dotDisabled && this.mode == 'number') {
  77. if (!this.random) {
  78. return [1, 2, 3, 4, 5, 6, 7, 8, 9, this.dot, 0];
  79. } else {
  80. return this.$uv.randomArray([1, 2, 3, 4, 5, 6, 7, 8, 9, this.dot, 0]);
  81. }
  82. } else if (this.mode == 'card') {
  83. if (!this.random) {
  84. return [1, 2, 3, 4, 5, 6, 7, 8, 9, this.cardX, 0];
  85. } else {
  86. return this.$uv.randomArray([1, 2, 3, 4, 5, 6, 7, 8, 9, this.cardX, 0]);
  87. }
  88. }
  89. },
  90. // 按键的样式,在非乱序&&数字键盘&&不显示点按钮时,index为9时,按键占位两个空间
  91. itemStyle() {
  92. return index => {
  93. let style = {};
  94. if (this.mode == 'number' && this.dotDisabled && index == 9) style.width = '464rpx';
  95. return style;
  96. };
  97. },
  98. // 是否让按键显示灰色,只在非乱序&&数字键盘&&且允许点按键的时候
  99. btnBgGray() {
  100. return index => {
  101. if (!this.random && index == 9 && (this.mode != 'number' || (this.mode == 'number' && !this
  102. .dotDisabled))) return true;
  103. else return false;
  104. };
  105. },
  106. },
  107. created() {
  108. },
  109. methods: {
  110. // 点击退格键
  111. backspaceClick() {
  112. this.$emit('backspace');
  113. clearInterval(this.timer); //再次清空定时器,防止重复注册定时器
  114. this.timer = null;
  115. this.timer = setInterval(() => {
  116. this.$emit('backspace');
  117. }, 250);
  118. },
  119. clearTimer() {
  120. clearInterval(this.timer);
  121. this.timer = null;
  122. },
  123. // 获取键盘显示的内容
  124. keyboardClick(val) {
  125. // 允许键盘显示点模式和触发非点按键时,将内容转为数字类型
  126. if (!this.dotDisabled && val != this.dot && val != this.cardX) val = Number(val);
  127. this.$emit('change', val);
  128. }
  129. }
  130. };
  131. </script>
  132. <style lang="scss" scoped>
  133. $show-hover: 1;
  134. @import '@/uni_modules/uv-ui-tools/libs/css/variable.scss';
  135. @import '@/uni_modules/uv-ui-tools/libs/css/components.scss';
  136. @import '@/uni_modules/uv-ui-tools/libs/css/color.scss';
  137. $uv-number-keyboard-background-color:rgb(224, 228, 230) !default;
  138. $uv-number-keyboard-padding:8px 10rpx 8px 10rpx !default;
  139. $uv-number-keyboard-button-width:222rpx !default;
  140. $uv-number-keyboard-button-margin:4px 6rpx !default;
  141. $uv-number-keyboard-button-border-top-left-radius:4px !default;
  142. $uv-number-keyboard-button-border-top-right-radius:4px !default;
  143. $uv-number-keyboard-button-border-bottom-left-radius:4px !default;
  144. $uv-number-keyboard-button-border-bottom-right-radius:4px !default;
  145. $uv-number-keyboard-button-height: 90rpx!default;
  146. $uv-number-keyboard-button-background-color:#FFFFFF !default;
  147. $uv-number-keyboard-button-box-shadow:0 2px 0px #BBBCBE !default;
  148. $uv-number-keyboard-text-font-size:20px !default;
  149. $uv-number-keyboard-text-font-weight:500 !default;
  150. $uv-number-keyboard-text-color:$uv-main-color !default;
  151. $uv-number-keyboard-gray-background-color:rgb(200, 202, 210) !default;
  152. $uv-number-keyboard-uv-hover-class-background-color: #BBBCC6 !default;
  153. .uv-keyboard {
  154. @include flex;
  155. flex-direction: row;
  156. justify-content: space-around;
  157. background-color: $uv-number-keyboard-background-color;
  158. flex-wrap: wrap;
  159. padding: $uv-number-keyboard-padding;
  160. &__button-wrapper {
  161. box-shadow: $uv-number-keyboard-button-box-shadow;
  162. margin: $uv-number-keyboard-button-margin;
  163. border-top-left-radius: $uv-number-keyboard-button-border-top-left-radius;
  164. border-top-right-radius: $uv-number-keyboard-button-border-top-right-radius;
  165. border-bottom-left-radius: $uv-number-keyboard-button-border-bottom-left-radius;
  166. border-bottom-right-radius: $uv-number-keyboard-button-border-bottom-right-radius;
  167. &__button {
  168. width: $uv-number-keyboard-button-width;
  169. height: $uv-number-keyboard-button-height;
  170. background-color: $uv-number-keyboard-button-background-color;
  171. @include flex;
  172. justify-content: center;
  173. align-items: center;
  174. border-top-left-radius: $uv-number-keyboard-button-border-top-left-radius;
  175. border-top-right-radius: $uv-number-keyboard-button-border-top-right-radius;
  176. border-bottom-left-radius: $uv-number-keyboard-button-border-bottom-left-radius;
  177. border-bottom-right-radius: $uv-number-keyboard-button-border-bottom-right-radius;
  178. &__text {
  179. font-size: $uv-number-keyboard-text-font-size;
  180. font-weight: $uv-number-keyboard-text-font-weight;
  181. color: $uv-number-keyboard-text-color;
  182. }
  183. &--gray {
  184. background-color: $uv-number-keyboard-gray-background-color;
  185. }
  186. }
  187. }
  188. }
  189. .uv-hover-class {
  190. background-color: $uv-number-keyboard-uv-hover-class-background-color;
  191. }
  192. </style>