-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yan Heng
committed
Aug 7, 2023
1 parent
e2ba78e
commit ef75b67
Showing
1 changed file
with
71 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,78 @@ | ||
<script setup lang="ts" generic="T"> | ||
defineProps<{ | ||
items: T[]; | ||
selected: T; | ||
import { computed } from 'vue'; | ||
export type RadioGroupOption<T> = { | ||
value: T; | ||
label: string; | ||
}; | ||
const props = defineProps<{ | ||
modelValue: T; | ||
options: RadioGroupOption<T>[]; | ||
}>(); | ||
const emits = defineEmits<{ | ||
(event: 'update:modelValue', value: T): void; | ||
}>(); | ||
const selectedValue = computed<T>({ | ||
get() { | ||
return props.modelValue; | ||
}, | ||
set(value: T) { | ||
emits('update:modelValue', value); | ||
}, | ||
}); | ||
</script> | ||
|
||
<template> | ||
<div></div> | ||
<div class="radio-group"> | ||
<label v-for="(item, index) in options" :key="index" class="radio"> | ||
<input v-model="selectedValue" type="radio" :value="item.value" /> | ||
<div> | ||
<slot :item="item"></slot> | ||
</div> | ||
</label> | ||
</div> | ||
</template> | ||
|
||
<style scoped lang="scss"></style> | ||
<style scoped lang="scss"> | ||
.radio-group { | ||
width: 100%; | ||
display: grid; | ||
grid-template-rows: auto; | ||
grid-auto-flow: column; | ||
gap: calc(0.25rem * 1); | ||
justify-items: center; | ||
& > .radio { | ||
& > input { | ||
position: absolute; | ||
opacity: 0; | ||
} | ||
& > input:checked + div { | ||
background: #e3e2fe; | ||
} | ||
} | ||
} | ||
.radio { | ||
border-radius: 0.5rem; | ||
display: inline-flex; | ||
align-items: center; | ||
position: relative; | ||
cursor: pointer; | ||
user-select: none; | ||
-webkit-tap-highlight-color: transparent; | ||
div { | ||
width: 2.25rem; | ||
height: 2.25rem; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
border-radius: 0.5rem; | ||
} | ||
} | ||
</style> |