Skip to content

Commit

Permalink
feat: 新增RadioGroup组件
Browse files Browse the repository at this point in the history
  • Loading branch information
Yan Heng committed Aug 7, 2023
1 parent e2ba78e commit ef75b67
Showing 1 changed file with 71 additions and 5 deletions.
76 changes: 71 additions & 5 deletions main/src/components/RadioGroup.vue
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>

0 comments on commit ef75b67

Please sign in to comment.