vue-material/src/components/mdInputContainer/mdInputContainer.vue

75 lines
1.9 KiB
Vue
Raw Normal View History

2016-07-26 22:55:29 +00:00
<template>
2016-07-27 04:55:32 +00:00
<div class="md-input-container" :class="classes">
2016-07-26 22:55:29 +00:00
<slot></slot>
2016-07-27 22:17:22 +00:00
<span class="md-count" v-if="enableCounter">{{ inputLength }} / {{ counterLength }}</span>
2016-07-27 22:17:22 +00:00
<md-button class="md-icon-button md-toggle-password" @click="togglePasswordType" v-if="mdHasPassword">
<md-icon>{{ showPassword ? 'visibility_off' : 'visibility' }}</md-icon>
</md-button>
2016-07-26 22:55:29 +00:00
</div>
</template>
<style lang="scss" src="./mdInputContainer.scss"></style>
<script>
export default {
2016-07-27 04:55:32 +00:00
props: {
mdInline: Boolean,
2016-07-27 22:17:22 +00:00
mdDisabled: Boolean,
mdHasPassword: Boolean
2016-07-27 04:55:32 +00:00
},
2016-07-26 22:55:29 +00:00
data() {
return {
2016-11-12 16:00:15 +00:00
value: '',
2016-07-27 22:17:22 +00:00
input: false,
inputType: false,
showPassword: false,
enableCounter: false,
2016-08-10 04:36:20 +00:00
mdHasSelect: false,
counterLength: 0,
inputLength: 0
2016-07-26 22:55:29 +00:00
};
},
2016-11-12 16:00:15 +00:00
computed: {
classes() {
return {
'md-input-inline': this.mdInline,
'md-has-password': this.mdHasPassword,
'md-has-select': this.mdHasSelect,
'md-has-value': Boolean(this.value)
};
}
},
2016-07-27 22:17:22 +00:00
methods: {
togglePasswordType() {
if (this.input.tagName.toLowerCase() === 'input') {
2016-11-12 16:00:15 +00:00
if (this.inputType === 'password') {
2016-07-27 22:17:22 +00:00
this.input.type = 'text';
this.showPassword = true;
} else {
this.input.type = 'password';
this.showPassword = false;
}
2016-08-01 05:45:40 +00:00
this.input.focus();
2016-07-27 22:17:22 +00:00
}
2016-09-07 20:08:38 +00:00
},
setValue(value) {
this.value = value;
2016-07-27 22:17:22 +00:00
}
},
2016-08-31 22:20:23 +00:00
mounted() {
2016-08-10 04:36:20 +00:00
this.input = this.$el.querySelector('input') || this.$el.querySelector('textarea') || this.$el.querySelector('select');
if (!this.input) {
this.$destroy();
2016-08-10 04:36:20 +00:00
throw new Error('Missing input/select/textarea inside md-input-container');
}
2016-07-27 22:17:22 +00:00
this.inputType = this.input.type;
2016-07-26 22:55:29 +00:00
}
};
</script>