u-keyboard.vue 6.2 KB

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