2016-08-01 05:45:40 +00:00
|
|
|
<template>
|
|
|
|
|
<textarea
|
|
|
|
|
class="md-input"
|
|
|
|
|
:disabled="disabled"
|
|
|
|
|
@invalid="onInvalid"
|
|
|
|
|
@valid="onValid"
|
|
|
|
|
@focus="onFocus"
|
|
|
|
|
@blur="onBlur"
|
|
|
|
|
@input="onInput"></textarea>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
import autosize from 'autosize';
|
|
|
|
|
|
|
|
|
|
let invalidClass = 'md-input-invalid';
|
|
|
|
|
let disabledClass = 'md-input-disabled';
|
2016-08-02 05:05:32 +00:00
|
|
|
let requiredClass = 'md-input-required';
|
2016-08-02 05:35:33 +00:00
|
|
|
let placeholderClass = 'md-input-placeholder';
|
2016-08-01 05:45:40 +00:00
|
|
|
let focusedClass = 'md-input-focused';
|
|
|
|
|
let hasValueClass = 'md-has-value';
|
|
|
|
|
|
|
|
|
|
let manageDisabledClass = (state, scope) => {
|
|
|
|
|
if (state) {
|
|
|
|
|
scope.add(disabledClass);
|
|
|
|
|
} else {
|
|
|
|
|
scope.remove(disabledClass);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-08-02 05:05:32 +00:00
|
|
|
let manageRequiredClass = (required, scope) => {
|
|
|
|
|
if (required) {
|
|
|
|
|
scope.add(requiredClass);
|
|
|
|
|
} else {
|
|
|
|
|
scope.remove(requiredClass);
|
|
|
|
|
}
|
2016-08-02 04:20:35 +00:00
|
|
|
};
|
|
|
|
|
|
2016-08-02 05:35:33 +00:00
|
|
|
let managePlaceholderClass = (placeholder, scope) => {
|
|
|
|
|
if (placeholder) {
|
|
|
|
|
scope.add(placeholderClass);
|
|
|
|
|
} else {
|
|
|
|
|
scope.remove(placeholderClass);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-08-01 05:45:40 +00:00
|
|
|
let manageHasValueClass = function(value, scope) {
|
|
|
|
|
if (value.length > 0) {
|
|
|
|
|
scope.add(hasValueClass);
|
|
|
|
|
} else {
|
|
|
|
|
scope.remove(hasValueClass);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-08-02 05:05:32 +00:00
|
|
|
let manageMaxlength = (length, parent) => {
|
|
|
|
|
parent.enableCounter = length > 0;
|
|
|
|
|
parent.counterLength = length;
|
|
|
|
|
};
|
|
|
|
|
|
2016-08-01 05:45:40 +00:00
|
|
|
export default {
|
|
|
|
|
props: {
|
2016-08-02 04:20:35 +00:00
|
|
|
type: String,
|
|
|
|
|
disabled: Boolean,
|
2016-08-02 05:05:32 +00:00
|
|
|
required: Boolean,
|
2016-08-02 05:35:33 +00:00
|
|
|
maxlength: String,
|
|
|
|
|
placeholder: String
|
2016-08-01 05:45:40 +00:00
|
|
|
},
|
|
|
|
|
data() {
|
|
|
|
|
return {
|
|
|
|
|
parentClasses: this.$parent.$el.classList
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
watch: {
|
|
|
|
|
disabled(disabled) {
|
|
|
|
|
manageDisabledClass(disabled, this.parentClasses);
|
2016-08-02 04:20:35 +00:00
|
|
|
},
|
2016-08-02 05:05:32 +00:00
|
|
|
required(required) {
|
|
|
|
|
manageRequiredClass(required, this.parentClasses);
|
|
|
|
|
},
|
2016-08-02 05:35:33 +00:00
|
|
|
placeholder(placeholder) {
|
|
|
|
|
managePlaceholderClass(placeholder, this.parentClasses);
|
|
|
|
|
},
|
2016-08-02 04:20:35 +00:00
|
|
|
maxlength(maxlength) {
|
|
|
|
|
manageMaxlength(maxlength, this.$parent);
|
2016-08-01 05:45:40 +00:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
methods: {
|
|
|
|
|
onInvalid() {
|
|
|
|
|
this.parentClasses.add(invalidClass);
|
|
|
|
|
},
|
|
|
|
|
onValid() {
|
|
|
|
|
this.parentClasses.remove(invalidClass);
|
|
|
|
|
},
|
|
|
|
|
onFocus() {
|
|
|
|
|
this.parentClasses.add(focusedClass);
|
|
|
|
|
},
|
|
|
|
|
onBlur() {
|
|
|
|
|
this.parentClasses.remove(focusedClass);
|
|
|
|
|
manageHasValueClass(this.$el.value, this.parentClasses);
|
|
|
|
|
},
|
|
|
|
|
onInput() {
|
|
|
|
|
manageHasValueClass(this.$el.value, this.parentClasses);
|
2016-08-02 05:06:24 +00:00
|
|
|
this.$parent.inputLength = this.$el.value.length;
|
2016-08-01 05:45:40 +00:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
ready() {
|
2016-08-02 04:20:35 +00:00
|
|
|
if (!this.$parent.$el.classList.contains('md-input-container')) {
|
|
|
|
|
this.$destroy();
|
|
|
|
|
|
|
|
|
|
throw new Error('You should wrap the md-textarea in a md-input-container');
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-01 05:45:40 +00:00
|
|
|
manageDisabledClass(this.disabled, this.parentClasses);
|
2016-08-02 05:05:32 +00:00
|
|
|
manageRequiredClass(this.required, this.parentClasses);
|
2016-08-02 05:35:33 +00:00
|
|
|
managePlaceholderClass(this.placeholder, this.parentClasses);
|
2016-08-01 05:45:40 +00:00
|
|
|
manageHasValueClass(this.$el.value, this.parentClasses);
|
2016-08-02 04:20:35 +00:00
|
|
|
manageMaxlength(this.maxlength, this.$parent);
|
2016-08-01 05:45:40 +00:00
|
|
|
|
|
|
|
|
if (!this.$el.getAttribute('rows')) {
|
|
|
|
|
this.$el.setAttribute('rows', '1');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
autosize(this.$el);
|
|
|
|
|
},
|
|
|
|
|
beforeDestroy() {
|
|
|
|
|
autosize.destroy(this.$el);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
</script>
|