Merge pull request #2868 from Xotic750/checkbox_tabindex

Checkbox w3c
This commit is contained in:
Aresn 2018-01-22 11:19:05 +08:00 committed by GitHub
commit 23646cc575
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 33 deletions

View file

@ -37,7 +37,7 @@
tags: [], tags: [],
testValue1: null, testValue1: null,
testValue2: null testValue2: null
} };
}, },
mounted () { mounted () {
setTimeout(() => { setTimeout(() => {
@ -51,8 +51,8 @@
{ {
label: '西瓜' label: '西瓜'
} }
] ];
}, 1000); }, 1000);
} }
} };
</script> </script>

View file

@ -6,6 +6,7 @@
<script> <script>
import { findComponentsDownward, oneOf } from '../../utils/assist'; import { findComponentsDownward, oneOf } from '../../utils/assist';
import Emitter from '../../mixins/emitter'; import Emitter from '../../mixins/emitter';
const prefixCls = 'ivu-checkbox-group'; const prefixCls = 'ivu-checkbox-group';
export default { export default {
@ -45,10 +46,9 @@
}, },
methods: { methods: {
updateModel (update) { updateModel (update) {
const value = this.value;
this.childrens = findComponentsDownward(this, 'Checkbox'); this.childrens = findComponentsDownward(this, 'Checkbox');
if (this.childrens) { if (this.childrens) {
const { value } = this;
this.childrens.forEach(child => { this.childrens.forEach(child => {
child.model = value; child.model = value;

View file

@ -1,33 +1,28 @@
<template> <template>
<label <label :class="wrapClasses">
:class="wrapClasses"
@keydown.space.prevent="$el.click()"
:tabindex="disabled ? -1 : 0">
<span :class="checkboxClasses"> <span :class="checkboxClasses">
<span :class="innerClasses"></span> <span :class="innerClasses"></span>
<input <input
v-if="group" v-if="group"
type="checkbox" type="checkbox"
tabindex="-1"
:class="inputClasses" :class="inputClasses"
:disabled="disabled" :disabled="disabled"
:value="label" :value="label"
v-model="model" v-model="model"
:name="name" :name="name"
@change="change" @change="change"
@focus="$el.focus()" @focus="onFocus"
> @blur="onBlur">
<input <input
v-if="!group" v-else
type="checkbox" type="checkbox"
tabindex="-1"
:class="inputClasses" :class="inputClasses"
:disabled="disabled" :disabled="disabled"
:checked="currentValue" :checked="currentValue"
:name="name" :name="name"
@change="change" @change="change"
@focus="$el.focus()" @focus="onFocus"
> @blur="onBlur">
</span> </span>
<slot><span v-if="showSlot">{{ label }}</span></slot> <slot><span v-if="showSlot">{{ label }}</span></slot>
</label> </label>
@ -80,7 +75,8 @@
currentValue: this.value, currentValue: this.value,
group: false, group: false,
showSlot: true, showSlot: true,
parent: findComponentUpward(this, 'CheckboxGroup') parent: findComponentUpward(this, 'CheckboxGroup'),
focusInner: false
}; };
}, },
computed: { computed: {
@ -106,7 +102,12 @@
]; ];
}, },
innerClasses () { innerClasses () {
return `${prefixCls}-inner`; return [
`${prefixCls}-inner`,
{
[`${prefixCls}-focus`]: this.focusInner
}
];
}, },
inputClasses () { inputClasses () {
return `${prefixCls}-input`; return `${prefixCls}-input`;
@ -114,12 +115,15 @@
}, },
mounted () { mounted () {
this.parent = findComponentUpward(this, 'CheckboxGroup'); this.parent = findComponentUpward(this, 'CheckboxGroup');
if (this.parent) this.group = true; if (this.parent) {
if (!this.group) { this.group = true;
}
if (this.group) {
this.parent.updateModel(true);
} else {
this.updateModel(); this.updateModel();
this.showSlot = this.$slots.default !== undefined; this.showSlot = this.$slots.default !== undefined;
} else {
this.parent.updateModel(true);
} }
}, },
methods: { methods: {
@ -131,7 +135,7 @@
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) { if (this.group) {
@ -143,14 +147,21 @@
}, },
updateModel () { updateModel () {
this.currentValue = this.value === this.trueValue; this.currentValue = this.value === this.trueValue;
},
onBlur () {
this.focusInner = false;
},
onFocus () {
this.focusInner = true;
} }
}, },
watch: { watch: {
value (val) { value (val) {
if (val !== this.trueValue && val !== this.falseValue) { if (val === this.trueValue || val === this.falseValue) {
this.updateModel();
} else {
throw 'Value should be trueValue or falseValue.'; throw 'Value should be trueValue or falseValue.';
} }
this.updateModel();
} }
} }
}; };

View file

@ -1,13 +1,18 @@
.checkboxFn(@checkbox-prefix-cls: ~"@{css-prefix}checkbox") { .checkboxFn(@checkbox-prefix-cls: ~"@{css-prefix}checkbox") {
@checkbox-inner-prefix-cls: ~"@{checkbox-prefix-cls}-inner"; @checkbox-inner-prefix-cls: ~"@{checkbox-prefix-cls}-inner";
.@{checkbox-prefix-cls}-focus {
box-shadow: 0 0 0 2px fade(@primary-color, 20%);
z-index: 1;
}
// 普通状态 // 普通状态
.@{checkbox-prefix-cls} { .@{checkbox-prefix-cls} {
display: inline-block; display: inline-block;
vertical-align: middle; vertical-align: middle;
white-space: nowrap; white-space: nowrap;
cursor: pointer; cursor: pointer;
outline: none; //outline: none;
line-height: 1; line-height: 1;
position: relative; position: relative;
@ -236,13 +241,7 @@
font-size: @font-size-small; font-size: @font-size-small;
display: inline-block; display: inline-block;
margin-right: 8px; margin-right: 8px;
outline: 0; //outline: none;
&:focus,
&:active {
& .@{checkbox-prefix-cls}-inner {
box-shadow: 0 0 0 2px fade(@primary-color, 20%);
}
}
&-disabled{ &-disabled{
cursor: @cursor-disabled; cursor: @cursor-disabled;