u-keyboard.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <template>
  2. <u-popup class="" :mask="mask" :maskCloseAble="maskCloseAble" mode="bottom" :popup="false" v-model="value" length="auto"
  3. :safeAreaInsetBottom="safeAreaInsetBottom" @close="popupClose" :zIndex="uZIndex">
  4. <slot />
  5. <view class="u-tooltip" v-if="tooltip">
  6. <view class="u-tooltip-item u-tooltip-cancel" hover-class="u-tooltip-cancel-hover" @tap="onCancel">
  7. {{cancelBtn ? cancelText : ''}}
  8. </view>
  9. <view v-if="showTips" class="u-tooltip-item u-tooltip-tips">
  10. {{tips ? tips : mode == 'number' ? '数字键盘' : mode == 'card' ? '身份证键盘' : '车牌号键盘'}}
  11. </view>
  12. <view v-if="confirmBtn" @tap="onConfirm" class="u-tooltip-item u-tooltips-submit" hover-class="u-tooltips-submit-hover">
  13. {{confirmBtn ? confirmText : ''}}
  14. </view>
  15. </view>
  16. <block >
  17. <u-car-keyboard :random="random" :abc="abc" @backspace="backspace" @change="change"></u-car-keyboard>
  18. </block>
  19. </u-popup>
  20. </template>
  21. <script>
  22. /**
  23. * keyboard 键盘
  24. * @description 此为uViw自定义的键盘面板,内含了数字键盘,车牌号键,身份证号键盘3中模式,都有可以打乱按键顺序的选项。
  25. * @tutorial https://www.uviewui.com/components/keyboard.html
  26. * @property {String} mode 键盘类型,见官网基本使用的说明(默认number)
  27. * @property {Boolean} dot-enabled 是否显示"."按键,只在mode=number时有效(默认true)
  28. * @property {Boolean} tooltip 是否显示键盘顶部工具条(默认true)
  29. * @property {String} tips 工具条中间的提示文字,见上方基本使用的说明,如不需要,请传""空字符
  30. * @property {Boolean} cancel-btn 是否显示工具条左边的"取消"按钮(默认true)
  31. * @property {Boolean} confirm-btn 是否显示工具条右边的"完成"按钮(默认true)
  32. * @property {Boolean} mask 是否显示遮罩(默认true)
  33. * @property {String} confirm-text 确认按钮的文字
  34. * @property {String} cancel-text 取消按钮的文字
  35. * @property {Number String} z-index 弹出键盘的z-index值(默认1075)
  36. * @property {Boolean} random 是否打乱键盘按键的顺序(默认false)
  37. * @property {Boolean} safe-area-inset-bottom 是否开启底部安全区适配(默认false)
  38. * @property {Boolean} mask-close-able 是否允许点击遮罩收起键盘(默认true)
  39. * @event {Function} change 按键被点击(不包含退格键被点击)
  40. * @event {Function} cancel 键盘顶部工具条左边的"取消"按钮被点击
  41. * @event {Function} confirm 键盘顶部工具条右边的"完成"按钮被点击
  42. * @event {Function} backspace 键盘退格键被点击
  43. * @example <u-keyboard mode="number" v-model="show"></u-keyboard>
  44. */
  45. export default {
  46. name: "u-keyboard",
  47. props: {
  48. // 键盘的类型,number-数字键盘,card-身份证键盘,car-车牌号键盘
  49. mode: {
  50. type: String,
  51. default: 'number'
  52. },
  53. abc: {
  54. type: Boolean,
  55. default: false
  56. },
  57. // 是否显示键盘的"."符号
  58. dotEnabled: {
  59. type: Boolean,
  60. default: true
  61. },
  62. // 是否显示顶部工具条
  63. tooltip: {
  64. type: Boolean,
  65. default: true
  66. },
  67. // 是否显示工具条中间的提示
  68. showTips: {
  69. type: Boolean,
  70. default: true
  71. },
  72. // 工具条中间的提示文字
  73. tips: {
  74. type: String,
  75. default: ''
  76. },
  77. // 是否显示工具条左边的"取消"按钮
  78. cancelBtn: {
  79. type: Boolean,
  80. default: true
  81. },
  82. // 是否显示工具条右边的"完成"按钮
  83. confirmBtn: {
  84. type: Boolean,
  85. default: true
  86. },
  87. // 是否打乱键盘按键的顺序
  88. random: {
  89. type: Boolean,
  90. default: false
  91. },
  92. // 是否开启底部安全区适配,开启的话,会在iPhoneX机型底部添加一定的内边距
  93. safeAreaInsetBottom: {
  94. type: Boolean,
  95. default: false
  96. },
  97. // 是否允许通过点击遮罩关闭键盘
  98. maskCloseAble: {
  99. type: Boolean,
  100. default: true
  101. },
  102. // 通过双向绑定控制键盘的弹出与收起
  103. value: {
  104. type: Boolean,
  105. default: false
  106. },
  107. // 是否显示遮罩,某些时候数字键盘时,用户希望看到自己的数值,所以可能不想要遮罩
  108. mask: {
  109. type: Boolean,
  110. default: true
  111. },
  112. // z-index值
  113. zIndex: {
  114. type: [Number, String],
  115. default: ''
  116. },
  117. // 取消按钮的文字
  118. cancelText: {
  119. type: String,
  120. default: '取消'
  121. },
  122. // 确认按钮的文字
  123. confirmText: {
  124. type: String,
  125. default: '确认'
  126. }
  127. },
  128. data() {
  129. return {
  130. //show: false
  131. }
  132. },
  133. computed: {
  134. uZIndex() {
  135. return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
  136. }
  137. },
  138. methods: {
  139. change(e) {
  140. this.$emit('change', e);
  141. },
  142. // 键盘关闭
  143. popupClose() {
  144. // 通过发送input这个特殊的事件名,可以修改父组件传给props的value的变量,也即双向绑定
  145. this.$emit('input', false);
  146. },
  147. // 输入完成
  148. onConfirm() {
  149. this.popupClose();
  150. this.$emit('confirm');
  151. },
  152. // 取消输入
  153. onCancel() {
  154. this.popupClose();
  155. this.$emit('cancel');
  156. },
  157. // 退格键
  158. backspace() {
  159. this.$emit('backspace');
  160. },
  161. // 关闭键盘
  162. // close() {
  163. // this.show = false;
  164. // },
  165. // // 打开键盘
  166. // open() {
  167. // this.show = true;
  168. // }
  169. }
  170. }
  171. </script>
  172. <style lang="scss" scoped>
  173. @import "../../libs/css/style.components.scss";
  174. .u-keyboard {
  175. position: relative;
  176. z-index: 1003;
  177. }
  178. .u-tooltip {
  179. @include vue-flex;
  180. justify-content: space-between;
  181. }
  182. .u-tooltip-item {
  183. color: #333333;
  184. flex: 0 0 33.333333%;
  185. text-align: center;
  186. padding: 20rpx 10rpx;
  187. font-size: 28rpx;
  188. }
  189. .u-tooltips-submit {
  190. text-align: right;
  191. flex-grow: 1;
  192. flex-wrap: 0;
  193. padding-right: 40rpx;
  194. color: $u-type-primary;
  195. }
  196. .u-tooltip-cancel {
  197. text-align: left;
  198. flex-grow: 1;
  199. flex-wrap: 0;
  200. padding-left: 40rpx;
  201. color: #888888;
  202. }
  203. .u-tooltips-submit-hover {
  204. color: $u-type-success;
  205. }
  206. .u-tooltip-cancel-hover {
  207. color: #333333;
  208. }
  209. </style>