Commit 75d248e1 authored by baiyaaaaa's avatar baiyaaaaa Committed by 杨奕

support radio button custom style

parent 64f1caea
...@@ -137,5 +137,7 @@ change | triggers when the bound value changes | the label value of the chosen r ...@@ -137,5 +137,7 @@ change | triggers when the bound value changes | the label value of the chosen r
---- | ---- | ---- | ---- | ---- ---- | ---- | ---- | ---- | ----
label | the value of radio | string/number | — | — label | the value of radio | string/number | — | —
disabled | whether radio is disabled | boolean | — | false disabled | whether radio is disabled | boolean | — | false
fill | border and background color when button is active | string | — | #20a0ff |
text-color | font color when button is active | string | — | #ffffff |
...@@ -138,3 +138,5 @@ ...@@ -138,3 +138,5 @@
|---------- |-------- |---------- |------------- |-------- | |---------- |-------- |---------- |------------- |-------- |
| label | Radio 的 value | string,number | — | — | | label | Radio 的 value | string,number | — | — |
| disabled | 是否禁用 | boolean | — | false | | disabled | 是否禁用 | boolean | — | false |
| fill | 按钮激活时的填充色和边框色 | string | — | #20a0ff |
| text-color | 按钮激活时的文本颜色 | string | — | #ffffff |
...@@ -23,6 +23,13 @@ ...@@ -23,6 +23,13 @@
set(newValue) { set(newValue) {
this.$parent.$emit('input', newValue); this.$parent.$emit('input', newValue);
} }
},
activeStyle() {
return {
backgroundColor: this.$parent.fill,
borderColor: this.$parent.fill,
color: this.$parent.textColor
};
} }
} }
}; };
...@@ -43,7 +50,7 @@ ...@@ -43,7 +50,7 @@
v-model="value" v-model="value"
:name="name" :name="name"
:disabled="disabled"> :disabled="disabled">
<span class="el-radio-button__inner"> <span class="el-radio-button__inner" :style="value === label ? activeStyle : null">
<slot></slot> <slot></slot>
<template v-if="!$slots.default">{{label}}</template> <template v-if="!$slots.default">{{label}}</template>
</span> </span>
......
...@@ -10,7 +10,15 @@ ...@@ -10,7 +10,15 @@
props: { props: {
value: [String, Number], value: [String, Number],
size: String size: String,
fill: {
type: String,
default: '#20a0ff'
},
textColor: {
type: String,
default: '#fff'
}
}, },
watch: { watch: {
value(value) { value(value) {
......
...@@ -160,15 +160,6 @@ ...@@ -160,15 +160,6 @@
z-index: -1; z-index: -1;
left: -999px; left: -999px;
&:checked {
& + .el-radio-button__inner {
z-index: 1;
background-color: var(--color-primary);
border-color: @background-color;
color: #fff;
}
}
&:disabled { &:disabled {
& + .el-radio-button__inner { & + .el-radio-button__inner {
color: var(--button-disabled-color); color: var(--button-disabled-color);
......
...@@ -76,28 +76,52 @@ describe('Radio', () => { ...@@ -76,28 +76,52 @@ describe('Radio', () => {
}); });
}, 50); }, 50);
}); });
it('radio button', done => { describe('Radio', () => {
vm = createVue({ it('create', done => {
template: ` vm = createVue({
<el-radio-group v-model="radio"> template: `
<el-radio-button :label="3" ref="radio1">备选项</el-radio-button> <el-radio-group v-model="radio">
<el-radio-button :label="6" ref="radio2">备选项</el-radio-button> <el-radio-button :label="3" ref="radio1">备选项</el-radio-button>
<el-radio-button :label="9">备选项</el-radio-button> <el-radio-button :label="6" ref="radio2">备选项</el-radio-button>
</el-radio-group> <el-radio-button :label="9">备选项</el-radio-button>
`, </el-radio-group>
data() { `,
return { data() {
radio: 3 return {
}; radio: 3
} };
}, true); }
expect(vm.$refs.radio1.$el.classList.contains('is-active')).to.be.true; }, true);
let radio = vm.$refs.radio2; expect(vm.$refs.radio1.$el.classList.contains('is-active')).to.be.true;
radio.$el.click(); let radio = vm.$refs.radio2;
vm.$nextTick(_ => { radio.$el.click();
expect(radio.$el.classList.contains('is-active')).to.be.true; vm.$nextTick(_ => {
expect(vm.radio === 6).to.be.true; expect(radio.$el.classList.contains('is-active')).to.be.true;
done(); expect(vm.radio === 6).to.be.true;
done();
});
});
it('custom color', done => {
vm = createVue({
template: `
<el-radio-group v-model="radio" fill="#000" text-color="#ff0">
<el-radio-button :label="3" ref="radio1">备选项</el-radio-button>
<el-radio-button :label="6" ref="radio2">备选项</el-radio-button>
<el-radio-button :label="9">备选项</el-radio-button>
</el-radio-group>
`,
data() {
return {
radio: 3
};
}
}, true);
vm.$nextTick(_ => {
expect(vm.$refs.radio1.activeStyle.backgroundColor).to.equal('#000');
expect(vm.$refs.radio1.activeStyle.borderColor).to.equal('#000');
expect(vm.$refs.radio1.activeStyle.color).to.equal('#ff0');
done();
});
}); });
}); });
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment