index.vue 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <template>
  2. <view :class="`wd-key-wrapper ${wider ? 'wd-key-wrapper--wider' : ''}`" @touchstart="onTouchStart" @touchmove="onTouchMove" @touchend="onTouchEnd">
  3. <view :class="keyClass">
  4. <wd-loading custom-class="wd-key__loading-icon" v-if="props.loading" />
  5. <template v-if="type === 'delete'">
  6. <template v-if="text">
  7. {{ text }}
  8. </template>
  9. <wd-icon v-else custom-class="wd-key__icon" name="keyboard-delete" size="22px"></wd-icon>
  10. </template>
  11. <template v-else-if="type === 'extra'">
  12. <template v-if="text">
  13. {{ text }}
  14. </template>
  15. <wd-icon v-else custom-class="wd-key__icon" name="keyboard-collapse" size="22px"></wd-icon>
  16. </template>
  17. <template v-else>{{ text }}</template>
  18. </view>
  19. </view>
  20. </template>
  21. <script lang="ts">
  22. export default {
  23. name: 'wd-key',
  24. options: {
  25. virtualHost: true,
  26. addGlobalClass: true,
  27. styleIsolation: 'shared'
  28. }
  29. }
  30. </script>
  31. <script lang="ts" setup>
  32. import { computed, ref } from 'vue'
  33. import { useTouch } from '../../composables/useTouch'
  34. import { keyProps } from './types'
  35. const props = defineProps(keyProps)
  36. const emit = defineEmits(['press'])
  37. const touch = useTouch()
  38. const active = ref<boolean>(false)
  39. const keyClass = computed(() => {
  40. return `wd-key ${props.large ? 'wd-key--large' : ''} ${props.type === 'delete' ? 'wd-key--delete' : ''} ${
  41. props.type === 'close' ? 'wd-key--close' : ''
  42. }`
  43. })
  44. function onTouchStart(event: TouchEvent) {
  45. touch.touchStart(event)
  46. active.value = true
  47. }
  48. function onTouchMove(event: TouchEvent) {
  49. touch.touchMove(event)
  50. if (touch.direction.value) {
  51. active.value = false
  52. }
  53. }
  54. function onTouchEnd() {
  55. if (active.value) {
  56. active.value = false
  57. emit('press', props.text, props.type)
  58. }
  59. }
  60. </script>
  61. <style lang="scss">
  62. @import './index.scss';
  63. </style>