u-keyboard.vue 6.7 KB

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