MyRadioButton.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <template>
  2. <div class="my-radio-button" :class="{ active: isActive }" @click="handleClick">
  3. <span style="font-size: 14px">{{ label }}</span>
  4. </div>
  5. </template>
  6. <script setup>
  7. import { ref, inject } from 'vue'
  8. // 注入来自父组件的数据
  9. const group = inject('MyRadioGroupContext')
  10. const clickType = ref('noClickType')
  11. const title = ref('')
  12. // 判断是否是当前选中项
  13. // const isActive = computed(() => props.value === group.value)
  14. const isActive = ref(false)
  15. const props = defineProps({
  16. value: {
  17. required: true
  18. },
  19. label: {
  20. type: String,
  21. default: ''
  22. },
  23. index: {
  24. type: Number
  25. }
  26. })
  27. // 监听 group.value 的变化
  28. watch(
  29. () => group.value.value,
  30. (newVal, oldVal) => {
  31. // console.log('group.value changed:', newVal, oldVal)
  32. isActive.value = newVal === props.value
  33. }
  34. )
  35. const setData = (data) => {
  36. title.value = data
  37. }
  38. const getData = (data) => {
  39. return title.value
  40. }
  41. const setClick = () => {
  42. clickType.value = 'clickType'
  43. }
  44. const setNoClick = () => {
  45. clickType.value = 'noClickType'
  46. }
  47. const handleClick = () => {
  48. console.log(
  49. '点击了 子布局',
  50. ' isActive.value ',
  51. isActive.value,
  52. ' props.value ',
  53. props.value,
  54. ' group.value ',
  55. group.value
  56. )
  57. if (!isActive.value) {
  58. console.log(
  59. '具体数值',
  60. group.value
  61. )
  62. group.changeValue(props.value)
  63. }
  64. }
  65. onMounted(() => {
  66. // if (props.index == 0) {
  67. // isActive.value = true
  68. // }
  69. })
  70. </script>
  71. <style scoped>
  72. .clickType {
  73. border-radius: 20px;
  74. background: rgb(45, 130, 226);
  75. display: flex;
  76. align-items: center;
  77. justify-content: center;
  78. min-height: 50px;
  79. min-width: 100px;
  80. }
  81. .clickType span {
  82. color: white;
  83. }
  84. .noClickType {
  85. display: flex;
  86. align-items: center;
  87. justify-content: center;
  88. min-height: 50px;
  89. min-width: 100px;
  90. }
  91. .noClickType span {
  92. color: black;
  93. }
  94. .my-radio-button {
  95. padding-left: 15px;
  96. padding-right: 15px;
  97. display: flex;
  98. justify-content: center;
  99. align-items: center;
  100. height: 30px;
  101. margin: 1px;
  102. //border: 1px solid #ffffff00;
  103. //border-radius: 5px;
  104. /* border: 1px solid #d9d9d9; */
  105. /* border-radius: 4px; */
  106. cursor: pointer;
  107. transition: all 0.2s ease;
  108. }
  109. .my-radio-button.active {
  110. //background-color: #1890ff;
  111. color: #1890ff;
  112. //border-color: #1890ff;
  113. //border: 1px solid #d9d9d9;
  114. //border-radius: 30px;
  115. }
  116. </style>