Merge pull request #2882 from iview/pr/2867

Pr/2867
This commit is contained in:
Aresn 2018-01-22 18:09:19 +08:00 committed by GitHub
commit 342390e691
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 125 additions and 54 deletions

View file

@ -43,12 +43,12 @@
data () { data () {
return { return {
single: true, single: true,
phone: 'apple', phone: '',
button2: '北京', button2: '北京',
} };
}, },
methods: { methods: {
} }
} };
</script> </script>

View file

@ -1,5 +1,5 @@
<template> <template>
<div :class="classes"> <div :class="classes" :name="name">
<slot></slot> <slot></slot>
</div> </div>
</template> </template>
@ -9,6 +9,10 @@
const prefixCls = 'ivu-radio-group'; const prefixCls = 'ivu-radio-group';
let seed = 0;
const now = Date.now();
const getUuid = () => `ivuRadioGroup_${now}_${seed++}`;
export default { export default {
name: 'RadioGroup', name: 'RadioGroup',
mixins: [ Emitter ], mixins: [ Emitter ],
@ -30,6 +34,10 @@
vertical: { vertical: {
type: Boolean, type: Boolean,
default: false default: false
},
name: {
type: String,
default: getUuid
} }
}, },
data () { data () {
@ -56,12 +64,10 @@
}, },
methods: { methods: {
updateValue () { updateValue () {
const value = this.value;
this.childrens = findComponentsDownward(this, 'Radio'); this.childrens = findComponentsDownward(this, 'Radio');
if (this.childrens) { if (this.childrens) {
this.childrens.forEach(child => { this.childrens.forEach(child => {
child.currentValue = value == child.label; child.currentValue = this.value === child.label;
child.group = true; child.group = true;
}); });
} }
@ -76,6 +82,7 @@
}, },
watch: { watch: {
value () { value () {
this.currentValue = this.value;
this.updateValue(); this.updateValue();
} }
} }

View file

@ -1,20 +1,18 @@
<template> <template>
<label <label :class="wrapClasses">
:class="wrapClasses"
:tabindex="disabled ? -1 : 0"
@keyup.space="change">
<span :class="radioClasses"> <span :class="radioClasses">
<span :class="innerClasses"></span> <span :class="innerClasses"></span>
<input <input
type="radio" type="radio"
tabindex="-1"
:class="inputClasses" :class="inputClasses"
:disabled="disabled" :disabled="disabled"
:checked="currentValue" :checked="currentValue"
:name="name" :name="groupName"
@change="change" @change="change"
> @focus="onFocus"
</span><slot>{{ label }}</slot> @blur="onBlur">
</span>
<slot>{{ label }}</slot>
</label> </label>
</template> </template>
<script> <script>
@ -59,7 +57,10 @@
return { return {
currentValue: this.value, currentValue: this.value,
group: false, group: false,
parent: findComponentUpward(this, 'RadioGroup') groupName: this.name,
parent: findComponentUpward(this, 'RadioGroup'),
focusWrapper: false,
focusInner: false
}; };
}, },
computed: { computed: {
@ -70,7 +71,8 @@
[`${prefixCls}-group-item`]: this.group, [`${prefixCls}-group-item`]: this.group,
[`${prefixCls}-wrapper-checked`]: this.currentValue, [`${prefixCls}-wrapper-checked`]: this.currentValue,
[`${prefixCls}-wrapper-disabled`]: this.disabled, [`${prefixCls}-wrapper-disabled`]: this.disabled,
[`${prefixCls}-${this.size}`]: !!this.size [`${prefixCls}-${this.size}`]: !!this.size,
[`${prefixCls}-focus`]: this.focusWrapper
} }
]; ];
}, },
@ -84,18 +86,35 @@
]; ];
}, },
innerClasses () { innerClasses () {
return `${prefixCls}-inner`; return [
`${prefixCls}-inner`,
{
[`${prefixCls}-focus`]: this.focusInner
}
];
}, },
inputClasses () { inputClasses () {
return `${prefixCls}-input`; return `${prefixCls}-input`;
} }
}, },
mounted () { mounted () {
if (this.parent) this.group = true; if (this.parent) {
if (!this.group) { this.group = true;
this.updateValue(); if (this.name && this.name !== this.parent.name) {
} else { /* eslint-disable no-console */
if (console.warn) {
console.warn('[iview] Name does not match Radio Group name.');
}
/* eslint-enable no-console */
} else {
this.groupName = this.parent.name;
}
}
if (this.group) {
this.parent.updateValue(); this.parent.updateValue();
} else {
this.updateValue();
} }
}, },
methods: { methods: {
@ -107,30 +126,43 @@
const checked = event.target.checked; const checked = event.target.checked;
this.currentValue = checked; this.currentValue = checked;
let value = checked ? this.trueValue : this.falseValue; const value = checked ? this.trueValue : this.falseValue;
this.$emit('input', value); this.$emit('input', value);
if (this.group && this.label !== undefined) { if (this.group) {
this.parent.change({ if (this.label !== undefined) {
value: this.label, this.parent.change({
checked: this.value value: this.label,
}); checked: this.value
} });
if (!this.group) { }
} else {
this.$emit('on-change', value); this.$emit('on-change', value);
this.dispatch('FormItem', 'on-form-change', value); this.dispatch('FormItem', 'on-form-change', value);
} }
}, },
updateValue () { updateValue () {
this.currentValue = this.value === this.trueValue; this.currentValue = this.value === this.trueValue;
},
onBlur () {
this.focusWrapper = false;
this.focusInner = false;
},
onFocus () {
if (this.group && this.parent.type === 'button') {
this.focusWrapper = true;
} else {
this.focusInner = true;
}
} }
}, },
watch: { watch: {
value (val) { value (val) {
if (val !== this.trueValue && val !== this.falseValue) { if (val === this.trueValue || val === this.falseValue) {
this.updateValue();
} else {
throw 'Value should be trueValue or falseValue.'; throw 'Value should be trueValue or falseValue.';
} }
this.updateValue();
} }
} }
}; };

View file

@ -3,10 +3,16 @@
@radio-inner-prefix-cls: ~"@{radio-prefix-cls}-inner"; @radio-inner-prefix-cls: ~"@{radio-prefix-cls}-inner";
@radio-group-button-prefix-cls: ~"@{radio-group-prefix-cls}-button"; @radio-group-button-prefix-cls: ~"@{radio-group-prefix-cls}-button";
.@{radio-prefix-cls}-focus {
box-shadow: 0 0 0 2px fade(@primary-color, 20%);
z-index: 1;
}
.@{radio-group-prefix-cls} { .@{radio-group-prefix-cls} {
display: inline-block; display: inline-block;
font-size: @font-size-small; font-size: @font-size-small;
vertical-align: middle; vertical-align: middle;
//outline: none;
&-vertical{ &-vertical{
.@{radio-prefix-cls}-wrapper { .@{radio-prefix-cls}-wrapper {
display: block; display: block;
@ -28,19 +34,14 @@
&-disabled{ &-disabled{
cursor: @cursor-disabled; cursor: @cursor-disabled;
} }
outline: 0; //outline: none;
&:focus {
& .@{radio-inner-prefix-cls} {
box-shadow: 0 0 0 2px fade(@primary-color, 20%);
}
}
} }
.@{radio-prefix-cls} { .@{radio-prefix-cls} {
display: inline-block; display: inline-block;
margin-right: 4px; margin-right: 4px;
white-space: nowrap; white-space: nowrap;
outline: none; //outline: none;
position: relative; position: relative;
line-height: 1; line-height: 1;
vertical-align: middle; vertical-align: middle;
@ -177,7 +178,7 @@ span.@{radio-prefix-cls} + * {
height: @btn-circle-size; height: @btn-circle-size;
line-height: @btn-circle-size - 2px; line-height: @btn-circle-size - 2px;
margin: 0; margin: 0;
padding: 0 16px; padding: 0 16px - 1px;
font-size: @font-size-small; font-size: @font-size-small;
color: @btn-default-color; color: @btn-default-color;
transition: all @transition-time ease-in-out; transition: all @transition-time ease-in-out;
@ -185,26 +186,37 @@ span.@{radio-prefix-cls} + * {
border: 1px solid @border-color-base; border: 1px solid @border-color-base;
border-left: 0; border-left: 0;
background: #fff; background: #fff;
position: relative;
> span { > span {
margin-left: 0; margin-left: 0;
} }
&:before { &:before, &:after {
content: ''; content: '';
display: block;
position: absolute; position: absolute;
width: 1px; width: 1px;
height: 100%; height: 100%;
left: -1px; left: -1px;
top: 0;
background: @border-color-base; background: @border-color-base;
visibility: hidden; //visibility: hidden;
transition: all @transition-time ease-in-out; transition: all @transition-time ease-in-out;
} }
&:after{
height: @btn-circle-size + 4px;
left: -1px;
top: -3px;
background: fade(@primary-color, 20%);
opacity: 0;
}
&:first-child { &:first-child {
border-radius: @btn-border-radius 0 0 @btn-border-radius; border-radius: @btn-border-radius 0 0 @btn-border-radius;
border-left: 1px solid @border-color-base; border-left: 1px solid @border-color-base;
&:before { &:before, &:after {
display: none; display: none;
} }
} }
@ -225,10 +237,6 @@ span.@{radio-prefix-cls} + * {
} }
} }
&:focus {
box-shadow: 0 0 0 2px fade(@primary-color, 20%);
}
.@{radio-prefix-cls}-inner, .@{radio-prefix-cls}-inner,
input { input {
opacity: 0; opacity: 0;
@ -241,25 +249,41 @@ span.@{radio-prefix-cls} + * {
border-color: @primary-color; border-color: @primary-color;
color: @primary-color; color: @primary-color;
box-shadow: -1px 0 0 0 @primary-color; box-shadow: -1px 0 0 0 @primary-color;
z-index: 1;
&:before{
background: @primary-color;
opacity: 0.1;
}
&.@{radio-prefix-cls}-focus{
box-shadow: -1px 0 0 0 @primary-color, 0 0 0 2px fade(@primary-color, 20%);
transition: all @transition-time ease-in-out;
&:after{
left: -3px;
top: -3px;
opacity: 1;
background: fade(@primary-color, 20%);
}
&:first-child{
box-shadow: 0 0 0 2px fade(@primary-color, 20%);
}
}
&:first-child { &:first-child {
border-color: @primary-color; border-color: @primary-color;
box-shadow: none!important; box-shadow: none;
} }
&:hover { &:hover {
border-color: tint(@primary-color, 20%); border-color: tint(@primary-color, 20%);
box-shadow: -1px 0 0 0 tint(@primary-color, 20%); //box-shadow: -1px 0 0 0 tint(@primary-color, 20%);
color: tint(@primary-color, 20%); color: tint(@primary-color, 20%);
} }
&:focus {
box-shadow: 0 0 0 2px fade(@primary-color, 20%)!important;
}
&:active { &:active {
border-color: shade(@primary-color, 5%); border-color: shade(@primary-color, 5%);
box-shadow: -1px 0 0 0 shade(@primary-color, 5%); //box-shadow: -1px 0 0 0 shade(@primary-color, 5%);
color: shade(@primary-color, 5%); color: shade(@primary-color, 5%);
} }
} }
@ -294,6 +318,9 @@ span.@{radio-prefix-cls} + * {
height: @btn-circle-size-large; height: @btn-circle-size-large;
line-height: @btn-circle-size-large - 2px; line-height: @btn-circle-size-large - 2px;
font-size: @font-size-base; font-size: @font-size-base;
&:after{
height: @btn-circle-size-large + 4px;
}
} }
.@{radio-group-button-prefix-cls}.@{radio-group-prefix-cls}-small .@{radio-prefix-cls}-wrapper{ .@{radio-group-button-prefix-cls}.@{radio-group-prefix-cls}-small .@{radio-prefix-cls}-wrapper{
@ -301,6 +328,11 @@ span.@{radio-prefix-cls} + * {
line-height: @btn-circle-size-small - 2px; line-height: @btn-circle-size-small - 2px;
padding: 0 12px; padding: 0 12px;
font-size: @font-size-small; font-size: @font-size-small;
&:after{
height: @btn-circle-size-small + 4px;
}
&:first-child { &:first-child {
border-radius: @btn-border-radius-small 0 0 @btn-border-radius-small; border-radius: @btn-border-radius-small 0 0 @btn-border-radius-small;
} }