u-keyboard.vue 6.8 KB

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