Last active
September 28, 2023 16:31
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is worth noting that @input is recommended over @keyup for this purpose to deal with mouse based events as well!