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

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

内容简介:代码实例地址:RadioGroup 实例代码地址:
RadioGroup 组件的难点在于:
  • radio 与 radioGroup 之间的联动关系、数据绑定关系,使得 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 组件,它是由 label 和 input 两部分构成,主要区分两个状态, 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 实现》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

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

Dive Into Python 3

Dive Into Python 3

Mark Pilgrim / Apress / 2009-11-6 / USD 44.99

Mark Pilgrim's Dive Into Python 3 is a hands-on guide to Python 3 (the latest version of the Python language) and its differences from Python 2. As in the original book, Dive Into Python, each chapter......一起来看看 《Dive Into Python 3》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

SHA 加密
SHA 加密

SHA 加密工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换