hc-form-radio-group.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <div :class="isFocus?'is-focus':''" class="hc-form-radio-group-box"
  3. @keydown.shift.up="keyupShiftUp"
  4. @keydown.shift.down="keyupShiftDown"
  5. @keydown.shift.left="keyupShiftLeft"
  6. @keydown.shift.right="keyupShiftRight">
  7. <el-radio-group v-model="modelValues"
  8. :disabled="disabled"
  9. :keyname="keyname"
  10. :placeholder="placeholder"
  11. @change="radioGroupChange"
  12. >
  13. <slot></slot>
  14. </el-radio-group>
  15. <input :id="keyname" class="hc-radio-group-input" @blur="handleBlur" @focus="handleFocus"/>
  16. </div>
  17. </template>
  18. <script>
  19. export default {
  20. inheritAttrs: false,
  21. }
  22. </script>
  23. <script setup>
  24. import {ref, watch} from 'vue'
  25. import {ElRadioGroup, ElRadio} from 'element-plus'
  26. const props = defineProps({
  27. modelValue: {
  28. type: [String, Number, Boolean]
  29. },
  30. keyname: {
  31. type: String,
  32. default: ''
  33. },
  34. placeholder: {
  35. type: String,
  36. default: ''
  37. },
  38. disabled: {
  39. type: Boolean,
  40. default: false
  41. },
  42. })
  43. //变量
  44. const modelValues = ref(props.modelValue)
  45. //监听
  46. watch(() => [
  47. props.modelValue,
  48. ], ([val]) => {
  49. modelValues.value = val
  50. })
  51. //事件
  52. const emit = defineEmits(['update:modelValue', 'change', 'keydowns'])
  53. const radioGroupChange = (val) => {
  54. emit('update:modelValue', val)
  55. emit('change', val)
  56. }
  57. //向上
  58. const keyupShiftUp = (e) => {
  59. emit('keydowns', {type: 'up', name: {target: {id: props.keyname}}, key: props.keyname, e});
  60. }
  61. //向下
  62. const keyupShiftDown = (e) => {
  63. emit('keydowns', {type: 'down', name: {target: {id: props.keyname}}, key: props.keyname, e});
  64. }
  65. //向左
  66. const keyupShiftLeft = (e) => {
  67. emit('keydowns', {type: 'left', name: {target: {id: props.keyname}}, key: props.keyname, e});
  68. }
  69. //向右
  70. const keyupShiftRight = (e) => {
  71. emit('keydowns', {type: 'right', name: {target: {id: props.keyname}}, key: props.keyname, e});
  72. }
  73. //获得焦点
  74. const isFocus = ref(false)
  75. const handleFocus = () => {
  76. isFocus.value = true
  77. }
  78. //失去焦点
  79. const handleBlur = () => {
  80. isFocus.value = false
  81. }
  82. </script>
  83. <style lang="scss" scoped>
  84. .hc-form-radio-group-box {
  85. position: relative;
  86. width: 100%;
  87. height: initial;
  88. border-radius: 4px;
  89. transition: box-shadow 0.3s, background-color 0.3s;
  90. &.is-focus, &:hover {
  91. background-color: #eddac4;
  92. box-shadow: 0 0 0 1.5px var(--el-color-primary) inset;
  93. }
  94. .hc-radio-group-input {
  95. position: absolute;
  96. z-index: -1;
  97. right: 10px;
  98. width: 10px;
  99. }
  100. }
  101. </style>