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> </template>
<script> <script>
import { selectAll } from 'src/directives/directive-select-all'
export default { export default {
name: 'ModalTaskName', name: 'ModalTaskName',
directives: { directives: {
selectAll: { 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 () { mounted () {
this.$nextTick(() => { 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() this.$refs.name.$refs.input.focus()
}) })
} }

View File

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