Switch: Add `allowFocus` prop & focus/blur events (#7494)

* Table: Add `important` rule to `col-resize` cursor

* Table: Add `important` rule to `col-resize` cursor

* Update table-header.js

* [Switch]: Add `allowFocus` prop + blur/focus events

+ Add new `allowFocus` prop
+ Add event emitters for focus & blur events
+ Set `input` $ref `.focus()` on switch click event

* [Switch]: Add styling for when `allowFocus: true`

+ Add styling for when `allowFocus` prop is set to true.
+ Add styling for when input is focused

* [Switch]: Update docs
pull/7505/head
Brad Adams 2017-10-16 09:52:10 +02:00 committed by 杨奕
parent 17c94d7410
commit 6d311ed6d3
4 changed files with 49 additions and 3 deletions

View File

@ -122,10 +122,13 @@ on-value | switch value when in `on` state | boolean / string / number | — |
off-value | switch value when in `off` state | boolean / string / number | — | false off-value | switch value when in `off` state | boolean / string / number | — | false
on-color | background color when in `on` state | string | — | #20A0FF on-color | background color when in `on` state | string | — | #20A0FF
off-color | background color when in `off` state | string | — | #C0CCDA off-color | background color when in `off` state | string | — | #C0CCDA
name| input name of Switch | string | — | — name | input name of Switch | string | — | —
allow-focus | allow `.focus()` & `.blur()` methods on the `input` $ref | boolean | — | false
### Events ### Events
Event Name | Description | Parameters Event Name | Description | Parameters
---- | ----| ---- ---- | ----| ----
change | triggers when value changes | value after changing change | triggers when value changes | (value: Boolean)
blur | triggers on blur (if `allowFocus` is `true`) | (event: Event)
focus | triggers on focus (if `allowFocus` is `true`) | (event: Event)

View File

@ -3,14 +3,17 @@
<div class="el-switch__mask" v-show="disabled"></div> <div class="el-switch__mask" v-show="disabled"></div>
<input <input
class="el-switch__input" class="el-switch__input"
:class="{ 'allow-focus': allowFocus }"
type="checkbox" type="checkbox"
@change="handleChange" @change="handleChange"
@focus="handleFocus"
@blur="handleBlur"
ref="input" ref="input"
:name="name" :name="name"
:true-value="onValue" :true-value="onValue"
:false-value="offValue" :false-value="offValue"
:disabled="disabled"> :disabled="disabled">
<span class="el-switch__core" ref="core" :style="{ 'width': coreWidth + 'px' }"> <span class="el-switch__core" ref="core" :style="{ 'width': coreWidth + 'px' }" @click="setFocus">
<span class="el-switch__button" :style="{ transform }"></span> <span class="el-switch__button" :style="{ transform }"></span>
</span> </span>
<transition name="label-fade"> <transition name="label-fade">
@ -85,6 +88,10 @@
name: { name: {
type: String, type: String,
default: '' default: ''
},
allowFocus: {
type: Boolean,
default: false
} }
}, },
data() { data() {
@ -131,6 +138,22 @@
let newColor = this.checked ? this.onColor : this.offColor; let newColor = this.checked ? this.onColor : this.offColor;
this.$refs.core.style.borderColor = newColor; this.$refs.core.style.borderColor = newColor;
this.$refs.core.style.backgroundColor = newColor; this.$refs.core.style.backgroundColor = newColor;
},
setFocus() {
// set focus on input
if (this.allowFocus) {
this.$refs.input.focus();
}
},
handleBlur(event) {
if (this.allowFocus) {
this.$emit('blur', event);
}
},
handleFocus(event) {
if (this.allowFocus) {
this.$emit('focus', event);
}
} }
}, },
mounted() { mounted() {

View File

@ -400,6 +400,7 @@
--switch-width: 46px; --switch-width: 46px;
--switch-height: 22px; --switch-height: 22px;
--switch-button-size: 16px; --switch-button-size: 16px;
--switch-focus-border: var(--color-primary);
/* Dialog /* Dialog
-------------------------- */ -------------------------- */

View File

@ -26,6 +26,8 @@
display: inline-block; display: inline-block;
font-size: var(--switch-font-size); font-size: var(--switch-font-size);
cursor: pointer; cursor: pointer;
z-index: 2;
@m left { @m left {
i { i {
left: 6px; left: 6px;
@ -48,6 +50,22 @@
@e input { @e input {
display: none; display: none;
&.allow-focus {
z-index: 0;
display: initial;
position: absolute;
left: 0;
top: 0;
outline: none;
opacity: 0;
&:focus {
+ .el-switch__core {
box-shadow: 0 0 2px var(--switch-focus-border);
}
}
}
} }
@e core { @e core {
@ -62,6 +80,7 @@
background: var(--switch-off-color); background: var(--switch-off-color);
cursor: pointer; cursor: pointer;
transition: border-color .3s, background-color .3s; transition: border-color .3s, background-color .3s;
z-index: 1;
& .el-switch__button { & .el-switch__button {
position: absolute 0 * * 0; position: absolute 0 * * 0;