@@ -12,34 +12,38 @@ const { values } = defineProps<{
1212 screenreaderTitle: TranslationInput ,
1313 values: T ,
1414}>()
15- const idFirstValue = useId ()
16- const idSecondValue = useId ()
15+ const id = useId ()
1716const name = useId ()
18- const modelValue = defineModel <typeof values [number ][' key' ]>()
17+ const modelValue = defineModel <typeof values [number ][' key' ]>({ required: true })
18+ const defaultValue = modelValue .value
1919const { t : $t } = useTranslation ()
20- const thumb = useTemplateRef (' thumb' )
20+ const thumbs = useTemplateRef (' thumb' )
2121const track = useTemplateRef (' track' )
2222
2323const moveThumb = () => {
24+ // console.log('👉', thumbs.value)
2425 const activeTrackItem = track .value ?.querySelector (' :has(input[type="radio"]:checked)' )
26+ const thumb = thumbs .value ?.[0 ]
2527 if (! activeTrackItem ) return
26- if (! thumb . value ) return
28+ if (! thumb ) return
2729
28- const { x : initialX } = thumb .value . getBoundingClientRect ()
30+ const { x : initialX } = thumb .getBoundingClientRect ()
2931 const { x } = activeTrackItem .getBoundingClientRect ()
3032
3133 // this should rather have been done via view transition api
3234 // but it currently lacks `firefox support`
3335 // and also there was a flickering issue
34- const animation = thumb .value . animate ([ {
36+ const animation = thumb .animate ([ {
3537 transform: ` translateX(${x - initialX }px) ` ,
3638 } ],
3739 { duration: 180 },
3840 )
3941 return animation .finished .then (() => {
40- activeTrackItem .appendChild (thumb .value ! )
42+ if (! thumb ) return
43+ activeTrackItem .appendChild (thumb )
4144 })
4245}
46+
4347watch (modelValue , () => {
4448 moveThumb ()
4549})
@@ -56,55 +60,36 @@ watch(modelValue, () => {
5660 v-bind =" $attrs"
5761 >
5862 <label
59- :for =" idFirstValue"
63+ v-for =" (value, index) in values"
64+ :key =" value.key"
65+ :for =" `${id}-${index}`"
6066 :class =" classList?.trackItem"
6167 class =" relative"
6268 >
6369 <span class =" relative z-10" >
6470 <slot
65- name =" first "
66- :label =" values[0 ]?.label"
71+ : name =" value.key "
72+ :label =" values[index ]?.label"
6773 >
68- {{ values[0 ]?.label }}
74+ {{ values[index ]?.label }}
6975 </slot >
7076 </span >
7177 <input
72- :id =" idFirstValue "
78+ :id =" `${id}-${index}` "
7379 v-model =" modelValue"
74- :value =" values[0 ]?.key"
80+ :value =" values[index ]?.key"
7581 type =" radio"
7682 class =" sr-only"
7783 :name
7884 >
7985 <span
86+ v-if =" value.key === defaultValue"
8087 ref =" thumb"
8188 class =" absolute inset-[0] z-0"
8289 aria-hidden =" true"
8390 :class =" classList?.thumb"
8491 />
8592 </label >
86- <label
87- class =" relative"
88- :class =" classList?.trackItem"
89- :for =" idSecondValue"
90- >
91- <span class =" relative z-10" >
92- <slot
93- name =" second"
94- :label =" values[1]?.label"
95- >
96- {{ values[1]?.label }}
97- </slot >
98- </span >
99- <input
100- :id =" idSecondValue"
101- v-model =" modelValue"
102- :value =" values[1]?.key"
103- class =" sr-only"
104- type =" radio"
105- :name
106- >
107- </label >
10893 </div >
10994 </fieldset >
11095</template >
0 commit comments