1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| <template> <div ref="iptRef" class="j-ipt-box" :style="{width: width}"> <el-input v-model.trim="curValue" :placeholder="curPlaceholder" @change="handleChange" @focus="isFocus=true" @blur="isFocus=false" > <i slot="suffix" v-show="curValue" class="el-input__icon el-icon-error" @click="handleClear"></i> <el-tooltip slot="suffix" effect="light" v-show="isTips && !curValue"> <div slot="content"> <h5 style="marginBottom:8px">已启用通配符</h5> <p style="marginBottom:3px" v-for="tip in curTips" :key="tip" >{{tip}}</p> </div> <i class="el-input__icon el-icon-warning"></i> </el-tooltip> </el-input> <b :class="['search', isFocus?'is-focus':'']" :style="{top: bTop}"> <i class="el-input__icon el-icon-search" @click="handleSearch"></i> </b> </div> </template> <script> export default { name: 'ElSearchInput', props: { value: { type: [String, Number], default: '', }, placeholder: { type: String, default: '请输入内容', }, width: { type: String, default: '280px' }, tips: { type: Array, default: () => { return ['1、%替代一个或多个字符'] } }, isTips: { type: Boolean, default: false } }, watch: { value(val) { this.curValue = val }, placeholder(val) { this.curPlaceholder = val }, tips(val) { this.curTips = val }, }, data() { return { isFocus: false, curPlaceholder: this.placeholder, curValue: this.value, curTips: this.tips, bTop: '0px' }; }, methods: { handleChange(val) { this.$emit('input', val) },
handleClear() { this.curValue = '' this.handleChange('') },
handleSearch() { this.$emit('on-search',this.curValue) },
// 获取初始化输入框外层高度,动态定位右侧搜索部分 initTop() { this.$nextTick(() => { let height = this.$refs.iptRef.offsetHeight this.bTop = (height - 30) / 2 + 'px' }) } }, created() { this.initTop() } }; </script> <style lang="scss" scoped> .j-ipt-box { position: relative; padding-right: 30px; /deep/.el-input__inner { border-radius: 6px; border-top-right-radius: 0; border-bottom-right-radius: 0; padding-left: 8px; height: 30px; line-height: 30px; } .el-input--medium .el-input__icon { line-height: 32px; cursor: pointer; }
.search { width: 30px; height: 30px; line-height: 30px; text-align: center; box-sizing: border-box; border: 1px solid #D6D6D6; border-top-right-radius: 6px; border-bottom-right-radius: 6px; border-left: none; position: absolute; top: 0; right: 0; .el-icon-search { line-height: 30px; color: #4260db; font-size: 13px; cursor: pointer; } }
.is-focus { border-color: #4260db; }
/deep/.el-input__inner:focus { border-color: #4260db; } } </style>
|