Input: add show-password props (#13966)

pull/14437/head
Hao Peng 2019-02-21 12:30:38 +08:00 committed by iamkun
parent e8e271a8ef
commit 423d97825f
1 changed files with 30 additions and 4 deletions

View File

@ -8,7 +8,7 @@
'el-input-group--append': $slots.append, 'el-input-group--append': $slots.append,
'el-input-group--prepend': $slots.prepend, 'el-input-group--prepend': $slots.prepend,
'el-input--prefix': $slots.prefix || prefixIcon, 'el-input--prefix': $slots.prefix || prefixIcon,
'el-input--suffix': $slots.suffix || suffixIcon || clearable 'el-input--suffix': $slots.suffix || suffixIcon || clearable || showPassword
} }
]" ]"
@mouseenter="hovering = true" @mouseenter="hovering = true"
@ -24,7 +24,7 @@
v-if="type !== 'textarea'" v-if="type !== 'textarea'"
class="el-input__inner" class="el-input__inner"
v-bind="$attrs" v-bind="$attrs"
:type="type" :type="showPassword ? (passwordVisible ? 'text': 'password') : type"
:disabled="inputDisabled" :disabled="inputDisabled"
:readonly="readonly" :readonly="readonly"
:autocomplete="autoComplete || autocomplete" :autocomplete="autoComplete || autocomplete"
@ -50,7 +50,7 @@
<!-- 后置内容 --> <!-- 后置内容 -->
<span <span
class="el-input__suffix" class="el-input__suffix"
v-if="$slots.suffix || suffixIcon || showClear || validateState && needStatusIcon"> v-if="$slots.suffix || suffixIcon || showClear || showPassword || validateState && needStatusIcon">
<span class="el-input__suffix-inner"> <span class="el-input__suffix-inner">
<template v-if="!showClear"> <template v-if="!showClear">
<slot name="suffix"></slot> <slot name="suffix"></slot>
@ -63,6 +63,17 @@
class="el-input__icon el-icon-circle-close el-input__clear" class="el-input__icon el-icon-circle-close el-input__clear"
@click="clear" @click="clear"
></i> ></i>
<template v-if="!showVisible">
<slot name="suffix"></slot>
<i class="el-input__icon"
v-if="suffixIcon"
:class="suffixIcon">
</i>
</template>
<i v-else
class="el-input__icon el-icon-view el-input__clear"
@click="handlePasswordVisible"
></i>
</span> </span>
<i class="el-input__icon" <i class="el-input__icon"
v-if="validateState" v-if="validateState"
@ -126,7 +137,8 @@
textareaCalcStyle: {}, textareaCalcStyle: {},
hovering: false, hovering: false,
focused: false, focused: false,
isOnComposition: false isOnComposition: false,
passwordVisible: false
}; };
}, },
@ -169,6 +181,10 @@
type: Boolean, type: Boolean,
default: false default: false
}, },
showPassword: {
type: Boolean,
default: false
},
tabindex: String tabindex: String
}, },
@ -207,6 +223,12 @@
!this.readonly && !this.readonly &&
this.nativeInputValue && this.nativeInputValue &&
(this.focused || this.hovering); (this.focused || this.hovering);
},
showVisible() {
return this.showPassword &&
!this.inputDisabled &&
!this.readonly &&
(!!this.nativeInputValue || this.focused);
} }
}, },
@ -326,6 +348,10 @@
this.$emit('change', ''); this.$emit('change', '');
this.$emit('clear'); this.$emit('clear');
}, },
handlePasswordVisible() {
this.passwordVisible = !this.passwordVisible;
this.focus();
},
getInput() { getInput() {
return this.$refs.input || this.$refs.textarea; return this.$refs.input || this.$refs.textarea;
} }