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

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

View file

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