update Input clearable position with append slot

This commit is contained in:
梁灏 2021-09-07 15:05:47 +08:00
parent 674f7731c2
commit 79ed7e7979

View file

@ -2,7 +2,7 @@
<div :class="wrapClasses"> <div :class="wrapClasses">
<template v-if="type !== 'textarea'"> <template v-if="type !== 'textarea'">
<div :class="[prefixCls + '-group-prepend']" v-if="prepend" v-show="slotReady"><slot name="prepend"></slot></div> <div :class="[prefixCls + '-group-prepend']" v-if="prepend" v-show="slotReady"><slot name="prepend"></slot></div>
<i class="ivu-icon" :class="['ivu-icon-ios-close-circle', prefixCls + '-icon', prefixCls + '-icon-clear' , prefixCls + '-icon-normal']" v-if="clearable && currentValue && !itemDisabled" @click="handleClear"></i> <i class="ivu-icon" :class="['ivu-icon-ios-close-circle', prefixCls + '-icon', prefixCls + '-icon-clear' , prefixCls + '-icon-normal']" v-if="clearable && currentValue && !itemDisabled" @click="handleClear" :style="clearableStyles"></i>
<i class="ivu-icon" :class="['ivu-icon-' + icon, prefixCls + '-icon', prefixCls + '-icon-normal']" v-else-if="icon" @click="handleIconClick"></i> <i class="ivu-icon" :class="['ivu-icon-' + icon, prefixCls + '-icon', prefixCls + '-icon-normal']" v-else-if="icon" @click="handleIconClick"></i>
<i class="ivu-icon ivu-icon-ios-search" :class="[prefixCls + '-icon', prefixCls + '-icon-normal', prefixCls + '-search-icon']" v-else-if="search && enterButton === false" @click="handleSearch"></i> <i class="ivu-icon ivu-icon-ios-search" :class="[prefixCls + '-icon', prefixCls + '-icon-normal', prefixCls + '-search-icon']" v-else-if="search && enterButton === false" @click="handleSearch"></i>
<span class="ivu-input-suffix" v-else-if="showSuffix"><slot name="suffix"><i class="ivu-icon" :class="['ivu-icon-' + suffix]" v-if="suffix"></i></slot></span> <span class="ivu-input-suffix" v-else-if="showSuffix"><slot name="suffix"><i class="ivu-icon" :class="['ivu-icon-' + suffix]" v-if="suffix"></i></slot></span>
@ -204,7 +204,8 @@
slotReady: false, slotReady: false,
textareaStyles: {}, textareaStyles: {},
isOnComposition: false, isOnComposition: false,
showPassword: false showPassword: false,
clearableIconOffset: 0
}; };
}, },
computed: { computed: {
@ -279,6 +280,12 @@
} }
return (this.value || '').length; return (this.value || '').length;
},
clearableStyles () {
const style = {};
let offset = this.clearableIconOffset;
if (offset) style.transform = `translateX(-${offset}px)`;
return style;
} }
}, },
methods: { methods: {
@ -396,16 +403,31 @@
setTimeout(() => { setTimeout(() => {
this.$refs.input.setSelectionRange(len, len); this.$refs.input.setSelectionRange(len, len);
}, 0); }, 0);
},
handleCalcIconOffset () {
const $el = this.$el.querySelectorAll('.ivu-input-group-append')[0];
if ($el) {
this.clearableIconOffset = $el.offsetWidth;
} else {
this.clearableIconOffset = 0;
}
} }
}, },
watch: { watch: {
value (val) { value (val) {
this.setCurrentValue(val); this.setCurrentValue(val);
},
type () {
this.$nextTick(this.handleCalcIconOffset);
} }
}, },
mounted () { mounted () {
this.slotReady = true; this.slotReady = true;
this.resizeTextarea(); this.resizeTextarea();
this.handleCalcIconOffset();
},
updated () {
this.$nextTick(this.handleCalcIconOffset);
} }
}; };
</script> </script>