从零实现Vue的组件库(十四)- RadioGroup 实现
栏目: JavaScript · 发布时间: 6年前
内容简介:代码实例地址:RadioGroup 实例代码地址:
-
radio与radioGroup之间的联动关系、数据绑定关系,使得radio可以单独使用或者组合; - 利用插槽可以方便扩展
radio。
1. 实例
代码
<!-- 基础用法 -->
<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 实现》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 从零实现Vue的组件库(二)-Slider组件实现
- Vue自定义组件(简单实现一个自定义组件)
- 实现一个沙漏⏳组件
- angular 实现下拉列表组件
- 从零实现Vue的组件库(五)- Breadcrumb 实现
- 从零实现Vue的组件库(十)- Select 实现
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
我看电商2(双色)
黄若 / 电子工业出版社 / 2016-6 / 39.00元
《我看电商2》是行业畅销书《我看电商》的续集。 《我看电商》自出版以来,连续印刷14 次,受到业界人士和广大读者的高度好评。《我看电商2》承续作者一贯的风格,以行业观察、经验分享为出发点,重点分析了过去一年中国电商界的最新动态与趋势,包括双11点评、京东关闭拍拍、上市公司私有化等。 电子商务是我国近年来发展最快的新兴行业之一,作者作为这个行业的长老级领军人物,善于思考,长于实操。《我看......一起来看看 《我看电商2(双色)》 这本书的介绍吧!