14.02 Custom Directive - Select All Text in Input when Clicked

This commit is contained in:
Fredrick W Warren 2021-02-11 09:01:03 -08:00
parent b2f481d4b0
commit 3057951d6a

View File

@ -6,7 +6,7 @@
@input="$emit('update:name', $event)" @input="$emit('update:name', $event)"
:rules="[val => !!val || 'Field is required']" :rules="[val => !!val || 'Field is required']"
clearable clearable
autofocus v-select-all
ref="name" ref="name"
label="Task name" label="Task name"
class="col" /> class="col" />
@ -16,10 +16,29 @@
<script> <script>
export default { export default {
name: 'ModalTaskName', name: 'ModalTaskName',
directives: {
selectAll: {
// add callback to
inserted (el) {
const input = el.querySelector('.q-field__native')
// could also use this.$refs.name.$refs.input
input.addEventListener('focus', () => {
if (input.value.length) {
input.select()
}
})
}
}
},
props: [ props: [
'name' 'name'
] ],
mounted () {
this.$nextTick(() => {
// content of form does not populate before first render
this.$refs.name.$refs.input.focus()
})
}
} }
</script> </script>