Vue3单选框(Radio)

可自定义设置以下属性:

  • 单选元素数据(options),必传,类型:Array<{label: string, value: any, disabled?: boolean}>

  • 是否禁用所有子单选器(disabled),默认false

  • 是否垂直排列(vertical),默认false

  • 当前选中的值(v-model:value),默认null

  • 多个单选框之间的间距(gap),单位px,默认8px

效果如下图:

①创建单选框组件Radio.vue 

<script setup lang="ts">
import { computed } from 'vue'

interface Option {
  label: string,
  value: any,
  disabled?: boolean // 是否禁用单个单选器
}
interface Props {
  options: Array<Option>, // 单选元素数据
  disabled?: boolean, // 是否禁用所有子单选器
  vertical?: boolean, // 是否垂直排列
  value?: any, // 当前选中的值(v-model)
  gap?: number // 多个单选框之间的间距,单位px
}
const props = withDefaults(defineProps<Props>(), {
  options: () => [],
  disabled: false,
  vertical: false,
  value: null,
  gap: 8
})
const sum = computed(() => {
  return props.options.length
})
const styleObject = computed(() => {
  if (props.vertical) {
    return {
      marginBottom: props.gap + 'px'
    }
  } else {
    return {
      marginRight: props.gap + 'px'
    }
  }
})
const emits = defineEmits(['update:value', 'change'])
function onClick (value: any) {
  if (value !== props.value) {
    emits('update:value', value)
    emits('change', value)
  }
}
</script>
<template>
  <div class="m-radio" :class="{'vertical': vertical}">
    <div
      class="m-radio-wrap"
      :class="{'disabled': disabled || option.disabled}"
      :style="sum !== index + 1 ? styleObject: ''"
      @click="(disabled || option.disabled) ? (e: Event) => e.preventDefault() : onClick(option.value)" v-for="(option, index) in options" :key="index">
      <span class="u-radio" :class="{'u-radio-checked': value === option.value }"></span>
      <span class="u-label">{
   
   { option.label }}</span>
    </div>
  </div>
</template>
<style lang="less" scoped>
.m-radio {
  display: inline-flex;
  height: 24px;
  .m-radio-wrap {
    color: #000000d9;
    font-size: 14px;
    height: 24px;
    line-height: 24px;
    cursor: pointer;
    &:hover {
      .u-radio {
        border-color: @themeColor;
      }
    }
    .u-radio {
      position: relative;
      display: inline-block;
      width: 14px;
      height: 14px;
      background: #fff;
      border: 1px solid #d9d9d9;
      border-radius: 50%;
      transition: all .3s;
      vertical-align: top;
      top: 4px;
      &:after {
        position: absolute;
        top: 50%;
        left: 50%;
        width: 16px;
        height: 16px;
        margin-top: -8px;
        margin-left: -8px;
        background-color: @themeColor;
        border-radius: 100%;
        transform: scale(0);
        opacity: 0;
        transition: all .3s cubic-bezier(.78,.14,.15,.86);
        content: "";
      }
    }
    .u-radio-checked {
      border-color: @themeColor;
      &:after {
        transform: scale(.5);
        opacity: 1;
      }
    }
    .u-label {
      padding: 0 8px;
      font-size: 16px;
      display: inline-block;
      line-height: 24px;
    }
  }
  .disabled {
    color: #00000040;
    cursor: not-allowed;
    &:hover {
      .u-radio {
        border-color: #d9d9d9;
      }
    }
    .u-radio {
      border-color: #d9d9d9;
      background-color: #f5f5f5;
      &:after {
        background-color: rgba(0, 0, 0, 0.2);
      }
    }
  }
}
.vertical {
  display: inline-block;
}
</style>

②在要使用的页面引入:

<script setup lang="ts">
import { Radio } from './Radio.vue'
import { ref, watch } from 'vue'

const options = ref([
      {
        label: '北京市',
        value: 1
      },
      {
        label: '上海市上海市上海市上海市',
        value: 2,
        disabled: true
      },
      {
        label: '郑州市',
        value: 3
      },
      {
        label: '纽约市纽约市纽约市纽约市',
        value: 4
      },
      {
        label: '旧金山',
        value: 5
      },
      {
        label: '悉尼市',
        value: 6
      },
      {
        label: '伦敦市',
        value: 7
      },
      {
        label: '巴黎市',
        value: 8
      }
    ])

const value = ref(1)
watch(value, (to) => {
  console.log('p to:', to)
})
function onChange (value: any) {
  console.log('change:', value)
}
</script>
<template>
  <div>
    <h2 class="mb10">Radio 单选框基本使用</h2>
    <p class="u-intro">用于在多个备选项中选中单个状态</p>
    <Radio
      :options="options"
      :gap="16"
      :vertical="false"
      @change="onChange"
      v-model:value="value" />
  </div>
</template>
<style lang="less" scoped>
</style>

猜你喜欢

转载自blog.csdn.net/Dandrose/article/details/129837510