vue实现自定义radio组件

效果展示

在这里插入图片描述

父组件中使用

<radio-group v-model="radioIschecked">
          <radio label="个人" value="1"></radio>
          <radio label="收藏" value="2"></radio>
</radio-group>

Radio.vue

<template>
  <div class="radio-wrap">
    <div class="left" :class="isChecked?'box-click':''" ref="box" @click="handleClick">
      <transition name="fade">
        <div class="circle" v-show="isChecked"></div>
      </transition>
    </div>
    <div class="right">{{label}}</div>
  </div>
</template>
<script lang='ts'>
import { Component, Vue, Watch, Prop } from "vue-property-decorator";
@Component({
  components: {}
})
export default class extends Vue {
  private isChecked: boolean = false;
  @Prop()
  private label!: string;
  @Prop()
  private value!: string;
  private handleClick() {
    this.isChecked = !this.isChecked;
    if (this.isChecked) {
      this.$parent.$emit("radioChange", this.value);
    }
  }
  private checkIsActive(value) {
    if (this.value === value) {
      this.isChecked = true;
    } else {
      this.isChecked = false;
    }
  }
}
</script>
<style scoped lang="scss">
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.2s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
  opacity: 0;
}
.radio-wrap {
  height: 24px;
  display: inline-block;
  vertical-align: center;
  margin-bottom: 5px;
  .left {
    height: 24px;
    width: 24px;
    border-radius: 50%;
    background-color: #ffffff;
    display: inline-block;
    border: 1.2px solid #cccccc;
    box-sizing: border-box;
    .circle {
      margin-left: 9px;
      margin-top: 9px;
      border-radius: 50%;
      width: 6px;
      height: 6px;
      background-color: #ffffff;
    }
  }
  .left:hover {
    cursor: pointer;
  }
  .right {
    margin-left: 5px;
    padding-right: 10px;
    display: inline;
    vertical-align: top; // 内联元素默认的vertical-align为baseline。对象的内容与基线对齐。如果我们在right插入文字。则为出现left right不水平对齐的情况
    line-height: 24px;
    color: #66757f; // 在mounted
    font-size: 15px;
  }
  .box-click {
    background-color: #1da1f2;
    border: 0.5px solid #cccccc;
  }
}
</style>

RadioGroup.vue

<template>
  <div class="radio-group-wrap">
    <slot></slot>
  </div>
</template>
<script lang='ts'>
import { Component, Vue, Watch, Prop } from "vue-property-decorator";
@Component({
  components: {}
})
export default class extends Vue {
  @Prop()
  private value!: string;
  private dispatch(value) {
    // 调用所有子组件的checkIsActive方法
    this.$children.forEach(item => {
      const temp: any = item; // ts还是不好用。。。
      temp.checkIsActive(value);
    });
  }
  private radioChange(value) {
    this.$emit("input", value);
    this.dispatch(value);
  }
  private mounted() {
    if (this.value) {
      this.dispatch(this.value);
    }
    this.$on("radioChange", this.radioChange);
  }
}
</script>
<style scoped lang="scss">
</style>

这里script部分是用ts写的,你可以改成js的语法。template和style部分不用改。

Vue自定义一个Radio组件可以提高用户界面的美观性和一致性,同时也能根据实际需求调整功能。以下是创建一个基本自定义Radio组件的步骤和考虑因素: 1. 创建Radio组件的结构:使用Vue单文件组件(.vue)来定义你的Radio组件,包括模板(template)、脚本(script)和样式(style)部分。 2. 定义模板:模板中可以包含一个`<input type="radio">`元素,并且通过`:id`和`:name`来绑定其标识和名称。同时,可以使用`<label>`元素包裹显示的文本,通过`for`属性关联`<input>`的`id`。 3. 使用v-model进行数据绑定:通过Vue的`v-model`指令将Radio组件的选中状态与Vue实例的某个数据属性绑定。这样可以实现双向数据绑定,当Radio组件状态改变时,Vue实例的数据也会相应变化。 4. 样式定制:使用CSS来定义Radio组件的样式,包括未选中和选中时的不同状态。可以使用Vue的动态类绑定(例如`v-bind:class`)来根据组件的状态动态应用样式。 5. 处理Radio组:当需要多个Radio选项构成一个选择组时,确保它们共享同一个`v-model`绑定的值,同时使用`:value`属性来区分各个选项的值。 一个简单的Vue自定义Radio组件示例可能如下所示: ```vue <template> <div class="custom-radio"> <input type="radio" :id="radioId" :name="radioName" :value="value" v-model="radioModel"> <label :for="radioId">{{ label }}</label> </div> </template> <script> export default { props: { value: { type: [String, Number], required: true }, label: { type: String, required: true }, radioId: { type: String, required: true }, radioName: { type: String, required: true }, radioModel: { type: [String, Number], required: true } } }; </script> <style scoped> .custom-radio input[type="radio"] { /* 自定义radio输入框的样式 */ } .custom-radio label { /* 自定义label的样式 */ } </style> ``` 在使用自定义Radio组件时,你可以像这样使用它: ```vue <custom-radio v-model="selected" :label="男" :value="1" :radioId="'maleRadio'" :radioName="'gender'"></custom-radio> <custom-radio v-model="selected" :label="女" :value="2" :radioId="'femaleRadio'" :radioName="'gender'"></custom-radio> ``` 通过以上步骤和代码,你可以在Vue项目中创建和使用自定义Radio组件
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值