Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Last active September 28, 2023 16:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffreyWay/d108d03ed6b66d106d2b02389b6469e4 to your computer and use it in GitHub Desktop.
Save JeffreyWay/d108d03ed6b66d106d2b02389b6469e4 to your computer and use it in GitHub Desktop.
Learn Vue 3: Step By Step, Episode 21 - "Refactor to defineProps and defineEmits" - https://laracasts.com/series/learn-vue-3-step-by-step/episodes/21
<script setup>
import TabbableTextarea from "@/components/TabbableTextarea.vue";
import { ref } from "vue";
let comment = ref('initial textarea value');
</script>
<template>
<main>
<form>
<TabbableTextarea v-model="comment" style="width: 100%; height: 300px;" />
</form>
</main>
</template>
<script setup>
defineProps({
modelValue: String
});
let emit = defineEmits(['update:modelValue']);
function onTabPress(e) {
let textarea = e.target;
let val = textarea.value,
start = textarea.selectionStart,
end = textarea.selectionEnd;
textarea.value = val.substring(0, start) + "\t" + val.substring(end);
textarea.selectionStart = textarea.selectionEnd = start + 1;
}
</script>
<template>
<textarea
@keydown.tab.prevent="onTabPress"
@keyup="emit('update:modelValue', $event.target.value)"
v-text="modelValue" />
</template>
@Zerosan
Copy link

Zerosan commented Aug 19, 2022

It is worth noting that @input is recommended over @keyup for this purpose to deal with mouse based events as well!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment