Skip to content

Commit ec1f050

Browse files
d3radicatedmarcosmoura
authored andcommitted
add md-clearable to md-input-container (vuematerial#473)
* Add md-clearable to md-input-container * Add docs for md-clearable * Fix deprecated click event for clear button
1 parent 77a3ecb commit ec1f050

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

docs/src/pages/components/Input.vue

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
<md-table-cell><code>Boolean</code></md-table-cell>
2929
<md-table-cell>Show a button to toggle the password visibility. Default <code>false</code></md-table-cell>
3030
</md-table-row>
31+
32+
<md-table-row>
33+
<md-table-cell>md-clearable</md-table-cell>
34+
<md-table-cell><code>Boolean</code></md-table-cell>
35+
<md-table-cell>Show a button to clear the input. Default <code>false</code></md-table-cell>
36+
</md-table-row>
3137
</md-table-body>
3238
</md-table>
3339

@@ -176,6 +182,11 @@
176182
<label>Disabled</label>
177183
<md-input disabled></md-input>
178184
</md-input-container>
185+
186+
<md-input-container md-clearable>
187+
<label>Clearable</label>
188+
<md-input v-model="initialValue"></md-input>
189+
</md-input-container>
179190
</form>
180191
</div>
181192

@@ -211,6 +222,11 @@
211222
&lt;label&gt;Disabled&lt;/label&gt;
212223
&lt;md-input disabled&gt;&lt;/md-input&gt;
213224
&lt;/md-input-container&gt;
225+
226+
&lt;md-input-container md-clearable&gt;
227+
&lt;label&gt;Clearable&lt;/label&gt;
228+
&lt;md-input v-model=&quot;initialValue&quot;&gt;&lt;/md-input&gt;
229+
&lt;/md-input-container&gt;
214230
&lt;/form&gt;
215231
</code-block>
216232

src/components/mdInputContainer/mdInputContainer.scss

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,24 @@ $input-size: 32px;
215215
}
216216
}
217217

218+
&.md-clearable {
219+
&.md-input-focused .md-clear-input {
220+
color: rgba(#000, .54);
221+
}
222+
223+
.md-clear-input {
224+
margin: 0;
225+
position: absolute;
226+
right: 0;
227+
bottom: -2px;
228+
color: rgba(#000, .38);
229+
230+
.md-ink-ripple {
231+
color: rgba(#000, .87);
232+
}
233+
}
234+
}
235+
218236
&.md-input-invalid {
219237
.md-error {
220238
opacity: 1;

src/components/mdInputContainer/mdInputContainer.vue

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<md-button class="md-icon-button md-toggle-password" @click.native="togglePasswordType" v-if="mdHasPassword">
88
<md-icon>{{ showPassword ? 'visibility_off' : 'visibility' }}</md-icon>
99
</md-button>
10+
11+
<md-button class="md-icon-button md-clear-input" @click.native="clearInput" v-if="mdClearable && hasValue">
12+
<md-icon>clear</md-icon>
13+
</md-button>
1014
</div>
1115
</template>
1216

@@ -20,7 +24,8 @@
2024
name: 'md-input-container',
2125
props: {
2226
mdInline: Boolean,
23-
mdHasPassword: Boolean
27+
mdHasPassword: Boolean,
28+
mdClearable: Boolean
2429
},
2530
mixins: [theme],
2631
data() {
@@ -51,6 +56,7 @@
5156
return {
5257
'md-input-inline': this.mdInline,
5358
'md-has-password': this.mdHasPassword,
59+
'md-clearable': this.mdClearable,
5460
'md-has-select': this.hasSelect,
5561
'md-has-file': this.hasFile,
5662
'md-has-value': this.hasValue,
@@ -78,6 +84,13 @@
7884
this.input.focus();
7985
}
8086
},
87+
clearInput() {
88+
if (this.isInput()) {
89+
this.input.value = '';
90+
this.setValue(this.input.value);
91+
this.$emit('input', this.input.value);
92+
}
93+
},
8194
setValue(value) {
8295
this.value = value;
8396
}

0 commit comments

Comments
 (0)