u-number-keyboard.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.js';
  42. import mpMixin from '../../libs/mixin/mpMixin.js';
  43. import mixin from '../../libs/mixin/mixin.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. data() {
  58. return {
  59. backspace: 'backspace', // 退格键内容
  60. dot: '.', // 点
  61. timer: null, // 长按多次删除的事件监听
  62. cardX: 'X' // 身份证的X符号
  63. };
  64. },
  65. computed: {
  66. // 键盘需要显示的内容
  67. numList() {
  68. let tmp = [];
  69. if (this.dotDisabled && this.mode == 'number') {
  70. if (!this.random) {
  71. return [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
  72. } else {
  73. return uni.$u.randomArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]);
  74. }
  75. } else if (!this.dotDisabled && this.mode == 'number') {
  76. if (!this.random) {
  77. return [1, 2, 3, 4, 5, 6, 7, 8, 9, this.dot, 0];
  78. } else {
  79. return uni.$u.randomArray([1, 2, 3, 4, 5, 6, 7, 8, 9, this.dot, 0]);
  80. }
  81. } else if (this.mode == 'card') {
  82. if (!this.random) {
  83. return [1, 2, 3, 4, 5, 6, 7, 8, 9, this.cardX, 0];
  84. } else {
  85. return uni.$u.randomArray([1, 2, 3, 4, 5, 6, 7, 8, 9, this.cardX, 0]);
  86. }
  87. }
  88. },
  89. // 按键的样式,在非乱序&&数字键盘&&不显示点按钮时,index为9时,按键占位两个空间
  90. itemStyle() {
  91. return index => {
  92. let style = {};
  93. if (this.mode == 'number' && this.dotDisabled && index == 9) style.width = '464rpx';
  94. return style;
  95. };
  96. },
  97. // 是否让按键显示灰色,只在非乱序&&数字键盘&&且允许点按键的时候
  98. btnBgGray() {
  99. return index => {
  100. if (!this.random && index == 9 && (this.mode != 'number' || (this.mode == 'number' && !this
  101. .dotDisabled))) return true;
  102. else return false;
  103. };
  104. },
  105. },
  106. created() {
  107. },
  108. emits: ["backspace", "change"],
  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. @import "../../libs/css/components.scss";
  134. $u-number-keyboard-background-color:rgb(224, 228, 230) !default;
  135. $u-number-keyboard-padding:8px 10rpx 8px 10rpx !default;
  136. $u-number-keyboard-button-width:222rpx !default;
  137. $u-number-keyboard-button-margin:4px 6rpx !default;
  138. $u-number-keyboard-button-border-top-left-radius:4px !default;
  139. $u-number-keyboard-button-border-top-right-radius:4px !default;
  140. $u-number-keyboard-button-border-bottom-left-radius:4px !default;
  141. $u-number-keyboard-button-border-bottom-right-radius:4px !default;
  142. $u-number-keyboard-button-height: 90rpx!default;
  143. $u-number-keyboard-button-background-color:#FFFFFF !default;
  144. $u-number-keyboard-button-box-shadow:0 2px 0px #BBBCBE !default;
  145. $u-number-keyboard-text-font-size:20px !default;
  146. $u-number-keyboard-text-font-weight:500 !default;
  147. $u-number-keyboard-text-color:$u-main-color !default;
  148. $u-number-keyboard-gray-background-color:rgb(200, 202, 210) !default;
  149. $u-number-keyboard-u-hover-class-background-color: #BBBCC6 !default;
  150. .u-keyboard {
  151. @include flex;
  152. flex-direction: row;
  153. justify-content: space-around;
  154. background-color: $u-number-keyboard-background-color;
  155. flex-wrap: wrap;
  156. padding: $u-number-keyboard-padding;
  157. &__button-wrapper {
  158. box-shadow: $u-number-keyboard-button-box-shadow;
  159. margin: $u-number-keyboard-button-margin;
  160. border-top-left-radius: $u-number-keyboard-button-border-top-left-radius;
  161. border-top-right-radius: $u-number-keyboard-button-border-top-right-radius;
  162. border-bottom-left-radius: $u-number-keyboard-button-border-bottom-left-radius;
  163. border-bottom-right-radius: $u-number-keyboard-button-border-bottom-right-radius;
  164. &__button {
  165. width: $u-number-keyboard-button-width;
  166. height: $u-number-keyboard-button-height;
  167. background-color: $u-number-keyboard-button-background-color;
  168. @include flex;
  169. justify-content: center;
  170. align-items: center;
  171. border-top-left-radius: $u-number-keyboard-button-border-top-left-radius;
  172. border-top-right-radius: $u-number-keyboard-button-border-top-right-radius;
  173. border-bottom-left-radius: $u-number-keyboard-button-border-bottom-left-radius;
  174. border-bottom-right-radius: $u-number-keyboard-button-border-bottom-right-radius;
  175. &__text {
  176. font-size: $u-number-keyboard-text-font-size;
  177. font-weight: $u-number-keyboard-text-font-weight;
  178. color: $u-number-keyboard-text-color;
  179. }
  180. &--gray {
  181. background-color: $u-number-keyboard-gray-background-color;
  182. }
  183. }
  184. }
  185. }
  186. .u-hover-class {
  187. background-color: $u-number-keyboard-u-hover-class-background-color;
  188. }
  189. </style>