index.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <template>
  2. <div class="hc-page-box">
  3. <HcCard title="测试">
  4. <el-button type="primary" @click="hideClick">设置焦点</el-button>
  5. <el-input type="text"
  6. id="a1"
  7. v-model="value1"
  8. @keyup.shift.up="keyupShiftUp"
  9. @keyup.shift.down="keyupShiftDown"
  10. />
  11. <el-date-picker
  12. v-model="value2"
  13. type="date" id="a2"
  14. popper-class="hc-table-form-date-picker hc-form-id-a2"
  15. @keydown.shift.up="keyupShiftUp"
  16. @keydown.shift.down="keyupShiftDown"
  17. placeholder="Pick a day"/>
  18. <el-date-picker
  19. id="a3"
  20. v-model="value3"
  21. type="date"
  22. popper-class="hc-table-form-date-picker hc-form-id-a3"
  23. @keydown.shift.up="keyupShiftUp"
  24. @keydown.shift.down="keyupShiftDown"
  25. placeholder="Pick a day"/>
  26. <el-select
  27. id="a4"
  28. v-model="value4"
  29. placeholder="Select"
  30. @keyup.shift.up="keyupShiftUp"
  31. @keyup.shift.down="keyupShiftDown">
  32. <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"/>
  33. </el-select>
  34. </HcCard>
  35. </div>
  36. </template>
  37. <script setup>
  38. import {nextTick, onMounted, ref} from "vue";
  39. //import { getRandom } from "vue-utils-plus"
  40. const value1 = ref('')
  41. const value2 = ref('')
  42. const value3 = ref('')
  43. const value4 = ref('')
  44. const options = [
  45. {value: 'Option1', label: 'Option1',},
  46. {value: 'Option2', label: 'Option2',},
  47. {value: 'Option3', label: 'Option3',},
  48. ]
  49. const hideClick = () => {
  50. document.getElementById('xxxxx').focus();
  51. }
  52. onMounted(() => {
  53. let poppers = document.getElementsByClassName('hc-table-form-date-picker')
  54. for (let i = 0; i < poppers.length; i++) {
  55. const classList = poppers[i].getAttribute('class')
  56. const key = classList.split('-form-id-')[1]
  57. let panels = poppers[i].getElementsByClassName('el-picker-panel__content')
  58. for (let x = 0; x < panels.length; x++) {
  59. panels[x].addEventListener("keydown", e => {
  60. e.stopPropagation()
  61. console.log(e.key)
  62. if (e.key === 'ArrowUp') {
  63. keyupShiftUp({target: {id: key}})
  64. } else if (e.key === 'ArrowDown') {
  65. keyupShiftDown({target: {id: key}})
  66. } else if (e.key === 'ArrowLeft') {
  67. //keyupShiftLeft(e)
  68. } else if (e.key === 'ArrowRight') {
  69. //keyupShiftRight(e)
  70. }
  71. }, {
  72. capture: true
  73. });
  74. }
  75. }
  76. })
  77. const keys = ['a1', 'a2', 'a3', 'a4']
  78. //上
  79. const keyupShiftUp = ({target}) => {
  80. const key = target.id
  81. const index = keys.findIndex(id => id === key)
  82. if (index > 0) {
  83. let keyId = keys[index - 1]
  84. if (keyId) {
  85. document.getElementById(keyId).focus();
  86. }
  87. }
  88. }
  89. //下
  90. const keyupShiftDown = ({target}) => {
  91. const key = target.id
  92. const index = keys.findIndex(id => id === key)
  93. const keyLength = keys.length -1
  94. if (keyLength > index) {
  95. let keyId = keys[index + 1]
  96. if (keyId) {
  97. document.getElementById(keyId).focus();
  98. }
  99. }
  100. }
  101. </script>