Skip to content

Commit 6e8f10a

Browse files
lbogdanLeopoldthecoder
authored andcommitted
Input: add clearable (#8509)
* Added clearable to el-input. * Added en-US example. * Also fire change event on clear. * Used clearable shorthand; added clearable to attribute list. * Added styles.
1 parent 9998d13 commit 6e8f10a

File tree

4 files changed

+73
-9
lines changed

4 files changed

+73
-9
lines changed

examples/docs/en-US/input.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
input7: '',
1717
input8: '',
1818
input9: '',
19+
input10: '',
1920
textarea: '',
2021
textarea2: '',
2122
textarea3: '',
@@ -188,6 +189,29 @@ export default {
188189
```
189190
:::
190191

192+
### Clearable
193+
194+
::: demo Make the Input clearable with the `clearable` attribute.
195+
196+
```html
197+
<el-input
198+
placeholder="Please input"
199+
v-model="input10"
200+
clearable>
201+
</el-input>
202+
203+
<script>
204+
export default {
205+
data() {
206+
return {
207+
input10: ''
208+
}
209+
}
210+
}
211+
</script>
212+
```
213+
:::
214+
191215
### Input with icon
192216

193217
Add an icon to indicate input type.
@@ -639,6 +663,7 @@ Search data from server-side.
639663
|autofocus | same as `autofocus` in native input | boolean || false |
640664
|form | same as `form` in native input | string |||
641665
| label | label text | string |||
666+
| clearable | whether to show clear button | boolean || false |
642667

643668
### Input slots
644669

packages/input/src/input.vue

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
'el-input--prefix': $slots.prefix || prefixIcon,
1111
'el-input--suffix': $slots.suffix || suffixIcon
1212
}
13-
]">
13+
]"
14+
@mouseenter="hovering = true"
15+
@mouseleave="hovering = false"
16+
>
1417
<template v-if="type !== 'textarea'">
1518
<!-- 前置元素 -->
1619
<div class="el-input-group__prepend" v-if="$slots.prepend" tabindex="0">
@@ -40,14 +43,20 @@
4043
<!-- 后置内容 -->
4144
<span
4245
class="el-input__suffix"
43-
v-if="$slots.suffix || suffixIcon || validateState && needStatusIcon"
46+
v-if="$slots.suffix || suffixIcon || showClear || validateState && needStatusIcon"
4447
:style="suffixOffset">
4548
<span class="el-input__suffix-inner">
46-
<slot name="suffix"></slot>
47-
<i class="el-input__icon"
48-
v-if="suffixIcon"
49-
:class="suffixIcon">
50-
</i>
49+
<template v-if="!showClear">
50+
<slot name="suffix"></slot>
51+
<i class="el-input__icon"
52+
v-if="suffixIcon"
53+
:class="suffixIcon">
54+
</i>
55+
</template>
56+
<i v-else
57+
class="el-input__icon el-icon-circle-close el-input__clear"
58+
@click="clear"
59+
></i>
5160
</span>
5261
<i class="el-input__icon"
5362
v-if="validateState"
@@ -102,7 +111,9 @@
102111
currentValue: this.value,
103112
textareaCalcStyle: {},
104113
prefixOffset: null,
105-
suffixOffset: null
114+
suffixOffset: null,
115+
hovering: false,
116+
focused: false
106117
};
107118
},
108119
@@ -144,7 +155,11 @@
144155
},
145156
suffixIcon: String,
146157
prefixIcon: String,
147-
label: String
158+
label: String,
159+
clearable: {
160+
type: Boolean,
161+
default: false
162+
}
148163
},
149164
150165
computed: {
@@ -172,6 +187,9 @@
172187
},
173188
isGroup() {
174189
return this.$slots.prepend || this.$slots.append;
190+
},
191+
showClear() {
192+
return this.clearable && this.currentValue !== '' && (this.focused || this.hovering);
175193
}
176194
},
177195
@@ -197,6 +215,7 @@
197215
};
198216
},
199217
handleBlur(event) {
218+
this.focused = false;
200219
this.$emit('blur', event);
201220
if (this.validateEvent) {
202221
this.dispatch('ElFormItem', 'el.form.blur', [this.currentValue]);
@@ -221,6 +240,7 @@
221240
this.textareaCalcStyle = calcTextareaHeight(this.$refs.textarea, minRows, maxRows);
222241
},
223242
handleFocus(event) {
243+
this.focused = true;
224244
this.$emit('focus', event);
225245
},
226246
handleInput(event) {
@@ -252,6 +272,12 @@
252272
if (this.$slots[pendant]) {
253273
return { transform: `translateX(${place === 'suf' ? '-' : ''}${this.$el.querySelector(`.el-input-group__${pendant}`).offsetWidth}px)` };
254274
}
275+
},
276+
clear() {
277+
this.$emit('input', '');
278+
this.$emit('change', '');
279+
this.setCurrentValue('');
280+
this.focus();
255281
}
256282
},
257283

packages/theme-chalk/src/common/var.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ $--input-placeholder-color: $--color-text-placeholder !default;
329329
$--input-max-width: 314px !default;
330330

331331
$--input-hover-border: $--border-color-hover !default;
332+
$--input-clear-hover-color: $--color-text-secondary !default;
332333

333334
$--input-focus-border: $--color-primary !default;
334335
$--input-focus-fill: $--color-white !default;

packages/theme-chalk/src/input.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88
width: 100%;
99
@include scroll-bar;
1010

11+
& .el-input__clear {
12+
color: $--input-icon-color;
13+
font-size: $--input-font-size;
14+
line-height: 16px;
15+
cursor: pointer;
16+
transition: $--color-transition-base;
17+
18+
&:hover {
19+
color: $--input-clear-hover-color;
20+
}
21+
}
22+
1123
@include e(inner) {
1224
-webkit-appearance: none;
1325
background-color: $--input-fill;

0 commit comments

Comments
 (0)