14.03 Custom Directive - Make it Global!

This commit is contained in:
Fredrick W Warren 2021-02-11 09:36:16 -08:00
parent 3057951d6a
commit e2f0100f36
3 changed files with 20 additions and 13 deletions

View File

@ -14,28 +14,20 @@
</template>
<script>
import { selectAll } from 'src/directives/directive-select-all'
export default {
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()
}
})
}
}
selectAll
},
props: [
'name'
],
mounted () {
this.$nextTick(() => {
// content of form does not populate before first render
// input.value is null till after first render
// defer autofocus till field is populated
this.$refs.name.$refs.input.focus()
})
}

View File

@ -2,6 +2,7 @@
<q-input
outlined
v-model="searchField"
v-select-all
class="col"
label="Search" >
<template v-slot:append>
@ -17,9 +18,13 @@
<script>
import { mapState, mapActions } from 'vuex'
import { selectAll } from 'src/directives/directive-select-all'
export default {
name: 'Search',
directives: {
selectAll
},
computed: {
...mapState('tasks', ['search']),
searchField: {

View File

@ -0,0 +1,10 @@
export const selectAll = {
inserted (el) {
const input = el.querySelector('.q-field__native')
input.addEventListener('focus', () => {
if (input.value.length) {
input.select()
}
})
}
}