123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <template>
- <view
- class="fs-radio-button"
- :class="[
- selected ? checkedColorType : 'fs-radio-button-default',
- { 'fs-radio-button-radius': radius, 'fs-radio-button-round': round },
- buttonSize
- ]"
- :style="{ color: checkedColor }"
- @click="handleToggle"
- >
- {{ label }}
- <slot />
- </view>
- </template>
- <script>
- /**
- * 单选框组件
- * @description 单选框组件
- * @property {String} label 文本
- * @property {null} value 标识符(必须传)
- * @property {String} size = [mini | small | medium] 按钮大小
- * @property {String} checkedColor 选中颜色
- * @property {String} checkedColorType = [primary | danger | warning | info | success] 选中颜色类型
- */
- export default {
- name: 'fs-radio-button'
- }
- </script>
- <script setup>
- import { inject, reactive, watch, ref } from 'vue'
- const props = defineProps({
- label: String,
- value: {
- type: null,
- required: true
- },
- checkedColor: String,
- checkedColorType: String,
- size: {
- type: String,
- validator(value) {
- return ['mini', 'small', 'medium'].includes(value)
- }
- },
- checked: Boolean
- })
- const radioGroup = inject('radioGroup')
- const { radius, round } = radioGroup
- const checkedColorType = props.checkedColorType || radioGroup.checkedColorType
- const checkedColor = props.checkedColor || radioGroup.checkedColor
- const buttonSize = props.size || radioGroup.size
- let selected = ref(props.checked)
- watch(
- () => props.checked,
- val => {
- selected.value = val
- }
- )
- radioGroup.updateChildren({
- selected,
- value: props.value
- })
- const handleToggle = () => {
- radioGroup.updateValue(props.value)
- }
- </script>
- <style lang="scss" scoped>
- .fs-radio-button {
- padding: 10rpx 30rpx;
- white-space: nowrap;
- border: 2rpx solid currentColor;
- margin-right: 20rpx;
- margin-bottom: 20rpx;
- &-default {
- color: #999999;
- }
- &-radius {
- border-radius: var(--radius);
- }
- &-round {
- border-radius: 60rpx;
- }
- &.medium {
- padding: 8rpx 25rpx;
- font-size: 13px;
- }
- &.small {
- padding: 6rpx 20rpx;
- font-size: 12px;
- }
- &.mini {
- padding: 2rpx 15rpx;
- font-size: 11px;
- }
- }
- </style>
|