从零实现Vue的组件库(十四)- RadioGroup 实现

栏目: JavaScript · 发布时间: 5年前

内容简介:代码实例地址:RadioGroup 实例代码地址:
RadioGroup 组件的难点在于:
  • radioradioGroup 之间的联动关系、数据绑定关系,使得 radio 可以单独使用或者组合;
  • 利用插槽可以方便扩展 radio

1. 实例

从零实现Vue的组件库(十四)- RadioGroup 实现

代码

<!-- 基础用法 -->
<fat-radio v-model="otherValue" :value="1">
    <fat-hover-tip type="right-center">
        <template slot="hover-part">あらがき ゆい</template>
    <template slot="tip-part">
        <img src="/static/img/gakki.jpg" style="width: 100px" alt="示意图">
    </template>
    </fat-hover-tip>
</fat-radio>
<fat-radio v-model="otherValue" :value="2">
    <fat-hover-tip type="right-center">
        <template slot="hover-part">石原さとみ</template>
        <template slot="tip-part">
            <img
            src="/static/img/u=4046980169,286278015&fm=26&gp=0.jpg"
            style="width: 100px"
            alt="示意图"
            >
        </template>
    </fat-hover-tip>
</fat-radio>
<!-- 禁用状态 -->
<fat-radio-group v-model="anotherValue">
    <fat-radio :value="1">备选项</fat-radio>
    <fat-radio :value="2" disabled>备选项</fat-radio>
</fat-radio-group>
复制代码

实例地址:RadioGroup 实例

代码地址: Github UI-Library

2. 原理

Radio

首先单独实现 Radio 组件,它是由 labelinput 两部分构成,主要区分两个状态, checked 以及 disabled

<template>
  <label
    class="radio"
    :class="[
        { 'is-checked': value === model },
        { 'is-disabled': isDisabled }
    ]"
    @click.stop="handleClick"
  >
    <!-- checked 时展示被选中状态 -->
    <span class="radio-inner"></span>
    <input
      class="f-hide"
      type="radio"
      :disabled="isDisabled"
      v-bind="$attrs"
      :value="model"
      @click.stop
    >
    <!-- 提示文案(可插入组件) -->
    <slot></slot>
  </label>
</template>

<script>
export default {
    props: {
        value: {
            type: [String, Number],
            required: true
        },
        disabled: {
            type: [Boolean],
            default: false
        },
        propValue: {
            type: [String, Number]
        }
    },
    model: {
        prop: "propValue",
        event: "select"
    },
    computed: {
        isGroup() {
            return this.$parent.$options._componentTag === "fat-radio-group";
        },
        isDisabled() {
            return this.$parent.disabled || this.disabled;
        },
        model: {
            get() {
                return this.isGroup ? this.$parent.value : this.propValue;
            },
            set(newValue) {
                this.isGroup ? this.$parent.$emit("select", newValue) : this.$emit("select", newValue);
            }
        }
    },
    methods: {
        handleClick(event) {
            !this.isDisabled && (this.model = this.value);
        }
    }
};
</script>
复制代码

由于需要实现 Radio 可以单独使用的,所以不采用 provide / inject api,而是利用 this.$parent.$options._componentTag 判断,当前组件是否为 Group 。同时实现数据的传递

model: {
    get() {
        return this.isGroup ? this.$parent.value : this.propValue;
    },
    set(newValue) {
        this.isGroup ? this.$parent.$emit("select", newValue) : this.$emit("select", newValue);
    }
}
复制代码

同样,也是依据 isGroup 进行区分,来选择是 $emit 父组件还是子组件。

RadioGroup

则是在 Radio 的外部包裹一层 Group 用于,实现组的概念

<template>
  <div :class="[
            'radio-group'
        ]" name="radio-group">
    <slot></slot>
  </div>
</template>
<script>
export default {
    name: "radio-group",
    props: {
        value: { type: [String, Number] },
        disabled: { type: Boolean }
    },
    model: {
        prop: "value",
        event: "select"
    },
    watch: {
        value(newValue) {
        this.$emit("change", newValue);
    }
  }
};
复制代码

以上所述就是小编给大家介绍的《从零实现Vue的组件库(十四)- RadioGroup 实现》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

精通JavaScript+jQuery

精通JavaScript+jQuery

曾顺 编著 / 人民邮电出版社 / 2008-9 / 59.00元

随着Ajax技术的不断风靡,其核心技术JavaScript越来越受到人们的关注,各种JavaScript的框架层出不穷。jQuery作为JavaScript框架的优秀代表,为广大开发者提供了诸多便利。 本书从介绍JavaScript的基础知识开始,围绕标准Web的各项技术予以展开,通过大量实例对JavaScript、CSS、DOM、Ajax等 Web关键技术进行深入浅出的分析,主要内容包括J......一起来看看 《精通JavaScript+jQuery》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

在线进制转换器
在线进制转换器

各进制数互转换器

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试