2020-06-18 09:47:45 +00:00
|
|
|
<template>
|
|
|
|
<textarea
|
|
|
|
ref="textarea"
|
|
|
|
:placeholder="placeholder"
|
|
|
|
:value="value"
|
|
|
|
@input="onInput"
|
|
|
|
@focus="onFocus"
|
2020-11-26 18:47:55 +00:00
|
|
|
@keyup="onKeyup"
|
2020-06-18 09:47:45 +00:00
|
|
|
@blur="onBlur"
|
|
|
|
/>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2020-11-26 18:47:55 +00:00
|
|
|
const TYPING_INDICATOR_IDLE_TIME = 4000;
|
2020-06-18 09:47:45 +00:00
|
|
|
export default {
|
|
|
|
props: {
|
|
|
|
placeholder: {
|
|
|
|
type: String,
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
value: {
|
|
|
|
type: String,
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
minHeight: {
|
|
|
|
type: Number,
|
2020-10-18 18:02:22 +00:00
|
|
|
default: 2,
|
2020-06-18 09:47:45 +00:00
|
|
|
},
|
|
|
|
},
|
2020-11-26 18:47:55 +00:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
idleTimer: null,
|
|
|
|
};
|
|
|
|
},
|
2020-06-18 09:47:45 +00:00
|
|
|
watch: {
|
|
|
|
value() {
|
|
|
|
this.resizeTextarea();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
resizeTextarea() {
|
|
|
|
if (!this.value) {
|
|
|
|
this.$el.style.height = `${this.minHeight}rem`;
|
|
|
|
} else {
|
|
|
|
this.$el.style.height = `${this.$el.scrollHeight}px`;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onInput(event) {
|
|
|
|
this.$emit('input', event.target.value);
|
|
|
|
this.resizeTextarea();
|
|
|
|
},
|
2020-11-26 18:47:55 +00:00
|
|
|
resetTyping() {
|
|
|
|
this.$emit('typing-off');
|
|
|
|
this.idleTimer = null;
|
|
|
|
},
|
|
|
|
turnOffIdleTimer() {
|
|
|
|
if (this.idleTimer) {
|
|
|
|
clearTimeout(this.idleTimer);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onKeyup() {
|
|
|
|
if (!this.idleTimer) {
|
|
|
|
this.$emit('typing-on');
|
|
|
|
}
|
|
|
|
this.turnOffIdleTimer();
|
|
|
|
this.idleTimer = setTimeout(
|
|
|
|
() => this.resetTyping(),
|
|
|
|
TYPING_INDICATOR_IDLE_TIME
|
|
|
|
);
|
|
|
|
},
|
2020-06-18 09:47:45 +00:00
|
|
|
onBlur() {
|
2020-11-26 18:47:55 +00:00
|
|
|
this.turnOffIdleTimer();
|
|
|
|
this.resetTyping();
|
2020-06-18 09:47:45 +00:00
|
|
|
this.$emit('blur');
|
|
|
|
},
|
|
|
|
onFocus() {
|
|
|
|
this.$emit('focus');
|
|
|
|
},
|
|
|
|
focus() {
|
|
|
|
this.$refs.textarea.focus();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|