1
1
<script setup lang="ts" generic =" T " >
2
- import { computed } from ' vue' ;
2
+ import { computed , provide } from ' vue' ;
3
+
4
+ import { RadioCtxKey } from ' @/constants/injection-keys' ;
3
5
4
6
export type RadioGroupOption <T > = {
5
7
value: T ;
@@ -8,10 +10,10 @@ export type RadioGroupOption<T> = {
8
10
9
11
const props = defineProps <{
10
12
modelValue: T ;
11
- options: RadioGroupOption <T >[];
12
13
}>();
13
14
14
15
const emits = defineEmits <{
16
+ (event : ' change' , value : T ): void ;
15
17
(event : ' update:modelValue' , value : T ): void ;
16
18
}>();
17
19
@@ -20,19 +22,19 @@ const selectedValue = computed<T>({
20
22
return props .modelValue ;
21
23
},
22
24
set(value : T ) {
25
+ emits (' change' , value );
23
26
emits (' update:modelValue' , value );
24
27
},
25
28
});
29
+
30
+ provide (RadioCtxKey , {
31
+ modelValue: selectedValue ,
32
+ });
26
33
</script >
27
34
28
35
<template >
29
36
<div class =" radio-group" >
30
- <label v-for =" (item, index) in options" :key =" index" class =" radio" >
31
- <input v-model =" selectedValue" type =" radio" :value =" item.value" />
32
- <div >
33
- <slot :item =" item" ></slot >
34
- </div >
35
- </label >
37
+ <slot ></slot >
36
38
</div >
37
39
</template >
38
40
@@ -44,35 +46,5 @@ const selectedValue = computed<T>({
44
46
grid-auto-flow : column ;
45
47
gap : calc (0.25rem * 1 );
46
48
justify-items : center ;
47
-
48
- & > .radio {
49
- & > input {
50
- position : absolute ;
51
- opacity : 0 ;
52
- }
53
-
54
- & > input :checked + div {
55
- background : #e3e2fe ;
56
- }
57
- }
58
- }
59
-
60
- .radio {
61
- border-radius : 0.5rem ;
62
- display : inline-flex ;
63
- align-items : center ;
64
- position : relative ;
65
- cursor : pointer ;
66
- user-select : none ;
67
- -webkit-tap-highlight-color : transparent ;
68
-
69
- div {
70
- width : 2.25rem ;
71
- height : 2.25rem ;
72
- display : flex ;
73
- justify-content : center ;
74
- align-items : center ;
75
- border-radius : 0.5rem ;
76
- }
77
49
}
78
50
</style >
0 commit comments