3
0

u-number-keyboard.vue 6.3 KB

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