index.vue 3.3 KB

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