ldc-loue/resources/ts/components/bo/InputTextAutoComponent.vue

98 lines
2.2 KiB
Vue
Raw Permalink Normal View History

2022-07-04 10:17:34 +00:00
<template>
2022-09-19 09:10:04 +00:00
<WordCounter :id="intId" />
2022-07-04 10:17:34 +00:00
<textarea
ref="textarea"
2022-09-19 09:10:04 +00:00
:id="intId"
:class="`inputTextAutoComponent ${intClass}`"
:name="intName"
:aria-describedby="intAriaDescribedby"
:placeholder="intPlaceholder"
:required="intRequired"
@input="onInput"
2022-07-04 10:17:34 +00:00
:json="'{}'"
2022-09-19 09:10:04 +00:00
v-model="intValue"
/>
2022-07-04 10:17:34 +00:00
</template>
<script lang="ts">
2022-09-19 09:10:04 +00:00
import { defineComponent, ref } from "vue";
2022-07-04 10:17:34 +00:00
import trans from "../../modules/trans";
import WordCounter from "./WordCounterComponent.vue";
const textarea = ref<HTMLTextAreaElement | null>(null);
export default defineComponent({
inheritAttrs: false,
mixins: [trans],
components: {
WordCounter,
},
setup() {
return { textarea };
},
beforeMount() {
if (!this.$attrs.json) {
throw new Error("this component requires json");
}
},
mounted() {
const json = String(this.$attrs.json ?? "{}"),
2022-09-19 09:10:04 +00:00
data = JSON.parse(json);
this.intId = String(data.id);
this.intClass = String(data.class);
this.intName = String(data.name);
this.intPlaceholder = String(data.placeholder);
this.intAriaDescribedby = String(data.ariaDescribedby);
this.intValue = String(data.value);
2022-07-04 10:17:34 +00:00
if (data.required) {
2022-09-19 09:10:04 +00:00
this.intRequired = true;
2022-07-04 10:17:34 +00:00
}
if (this.textarea) {
// Default input init
2022-09-19 09:10:04 +00:00
this.textarea.setAttribute("style", "height: 1rem;overflow-y: hidden;");
2022-07-04 10:17:34 +00:00
}
// * Init input
this.$nextTick(this.onInput);
},
data(): {
2022-09-19 09:10:04 +00:00
intId: string;
intClass: string;
intName: string;
intPlaceholder: string;
intAriaDescribedby: string;
intRequired: boolean;
intValue: string;
intTextarea: HTMLTextAreaElement | null;
2022-07-04 10:17:34 +00:00
} {
return {
2022-09-19 09:10:04 +00:00
intId: "",
intClass: "",
intName: "",
intPlaceholder: "",
intAriaDescribedby: "",
intRequired: false,
intValue: "",
intTextarea: null,
2022-07-04 10:17:34 +00:00
};
},
methods: {
onInput() {
const el = textarea.value;
if (!el) {
throw new Error("undefined textarea element");
}
// * Remove every new line char
el.value = el.value.replace(/[\n\r]/, "");
el.style.height = "auto";
el.style.height = `${el.scrollHeight}px`;
},
},
});
</script>
<style lang="scss">
.inputTextAutoComponent {
resize: none;
}
</style>