tn-keyboard.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <template>
  2. <view v-if="value" class="tn-keyboard-class tn-keyboard">
  3. <tn-popup
  4. v-model="value"
  5. mode="bottom"
  6. :popup="false"
  7. length="auto"
  8. :mask="mask"
  9. :maskCloseable="maskCloseable"
  10. :safeAreaInsetBottom="safeAreaInsetBottom"
  11. :zIndex="elZIndex"
  12. @close="popupClose"
  13. >
  14. <view>
  15. <slot></slot>
  16. </view>
  17. <!-- 提示信息 -->
  18. <view v-if="tooltip" class="tn-keyboard__tooltip">
  19. <view
  20. v-if="cancelBtn"
  21. class="tn-keyboard__tooltip__item tn-keyboard__tooltip__cancel"
  22. hover-class="tn-keyboard__tooltip__cancel--hover"
  23. :hover-stay-time="150"
  24. @tap="onCancel"
  25. >
  26. {{ cancelBtn ? cancelText : ''}}
  27. </view>
  28. <view v-if="showTips" class="tn-keyboard__tooltip__item tn-keyboard__tooltip__tips">
  29. {{ tips ? tips : mode === 'number' ? '数字键盘' : mode === 'card' ? '身份证键盘' : '车牌号码键盘'}}
  30. </view>
  31. <view
  32. v-if="confirmBtn"
  33. class="tn-keyboard__tooltip__item tn-keyboard__tooltip__confirm"
  34. hover-class="tn-keybord__tooltip__confirm--hover"
  35. :hover-stay-time="150"
  36. @tap="onConfirm"
  37. >
  38. {{ confirmBtn ? confirmText : ''}}
  39. </view>
  40. </view>
  41. <!-- 键盘内容 -->
  42. <block v-if="mode === 'number' || mode === 'card'">
  43. <tn-number-keyboard :mode="mode" :dotEnabled="dotEnabled" :randomEnabled="randomEnabled" @change="change" @backspace="backspaceClick"></tn-number-keyboard>
  44. </block>
  45. <block v-if="mode === 'car'">
  46. <tn-car-keyboard :randomEnabled="randomEnabled" :switchEnMode="switchEnMode" @change="change" @backspace="backspaceClick"></tn-car-keyboard>
  47. </block>
  48. </tn-popup>
  49. </view>
  50. </template>
  51. <script>
  52. export default {
  53. name: 'tn-keyboard',
  54. props: {
  55. // 控制键盘弹出收回
  56. value: {
  57. type: Boolean,
  58. default: false
  59. },
  60. // 键盘类型
  61. // number - 数字键盘 card - 身份证键盘 car - 车牌号码
  62. mode: {
  63. type: String,
  64. default: 'number'
  65. },
  66. // 当mode = number时,是否显示'.'符号
  67. dotEnabled: {
  68. type: Boolean,
  69. default: true
  70. },
  71. // 是否打乱顺序
  72. randomEnabled: {
  73. type: Boolean,
  74. default: false
  75. },
  76. // 当mode = car,设置键盘中英文状态
  77. switchEnMode: {
  78. type: Boolean,
  79. default: false
  80. },
  81. // 显示顶部工具条
  82. tooltip: {
  83. type: Boolean,
  84. default: true
  85. },
  86. // 是否显示提示信息
  87. showTips: {
  88. type: Boolean,
  89. default: true
  90. },
  91. // 提示文字
  92. tips: {
  93. type: String,
  94. default: ''
  95. },
  96. // 是否显示取消按钮
  97. cancelBtn: {
  98. type: Boolean,
  99. default: true
  100. },
  101. // 是否显示确认按钮
  102. confirmBtn: {
  103. type: Boolean,
  104. default: true
  105. },
  106. // 取消按钮文字
  107. cancelText: {
  108. type: String,
  109. default: '取消'
  110. },
  111. // 确认按钮文字
  112. confirmText: {
  113. type: String,
  114. default: '确认'
  115. },
  116. // 是否开启底部安全区适配,开启的话,会在iPhoneX机型底部添加一定的内边距
  117. safeAreaInsetBottom: {
  118. type: Boolean,
  119. default: false
  120. },
  121. // 是否可以通过点击遮罩进行关闭
  122. maskCloseable: {
  123. type: Boolean,
  124. default: true
  125. },
  126. // 是否显示遮罩
  127. mask: {
  128. type: Boolean,
  129. default: true
  130. },
  131. // z-index
  132. zIndex: {
  133. type: Number,
  134. default: 0
  135. }
  136. },
  137. computed: {
  138. elZIndex() {
  139. return this.zIndex ? this.zIndex : this.$t.zIndex.popup
  140. }
  141. },
  142. data() {
  143. return {
  144. }
  145. },
  146. methods: {
  147. change(e) {
  148. this.$emit('change', e)
  149. },
  150. // 关闭键盘
  151. popupClose() {
  152. // 修改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. backspaceClick() {
  167. this.$emit('backspace')
  168. }
  169. }
  170. }
  171. </script>
  172. <style lang="scss" scoped>
  173. .tn-keyboard {
  174. position: relative;
  175. &__tooltip {
  176. display: flex;
  177. flex-direction: row;
  178. justify-content: space-between;
  179. &__item {
  180. color: $tn-font-color;
  181. flex: 0 0 33.3333333333%;
  182. text-align: center;
  183. font-size: 28rpx;
  184. padding: 20rpx 10rpx;
  185. }
  186. &__cancel {
  187. text-align: left;
  188. flex-grow: 1;
  189. flex-wrap: 0;
  190. padding-left: 40rpx;
  191. color: $tn-content-color;
  192. &--hover {
  193. color: $tn-font-color;
  194. }
  195. }
  196. &__confirm {
  197. text-align: right;
  198. flex-grow: 1;
  199. flex-wrap: 0;
  200. padding-right: 40rpx;
  201. color: $tn-main-color;
  202. &--hover {
  203. color: $tn-color-blue;
  204. }
  205. }
  206. }
  207. }
  208. </style>