1

The vuejs datepicker opens only when I click on its input field. I want it to pop up when I focus on its field by pressing tab from the previous field.

I looked for solutions on stackoverflow but could not find anything similar.

Here is my code:

const app = new Vue({
  el: '#app',
  components: {
  	vuejsDatepicker
  }
})
<div id="app">
  <input type="text" placeholder="Textbox" autofocus>
  <br><br>
  <vuejs-datepicker placeholder="Datepicker"></vuejs-datepicker>
</div>
<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/vuejs-datepicker"></script>

Whenever I press tab after writing something on the textbox, the focus goes to the datepicker field but the picker only pops up when I click on it using mouse. Whereas I want it to open everytime on focus. Can someone help please?

2

2 Answers 2

2

I had a look at the source.. The focus event is not bound so you may open an issue about that. But you can achieve the effect the following way:

<datepicker ref="dp1" @focusin.native="onfocusin"></datepicker>

And the method:

methods: {
    onfocusin(){
        setTimeout((ev)=>{
            this.$refs.dp1.isOpen || this.$refs.dp1.showCalendar(ev);
        }, 50)
    }
}

Another option is to modify the plugin a via mixin where you can modify the showCalendar method that currently toggles the visibility of the calendar to only allow it to open the calendar:

let myDatepicker = {
    mixins: [datepicker],

    methods: {
        showCalendar(){
            if(this.isOpen) return;

            return datepicker.showCalendar.apply(this, arguments)
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the response. This works well for focus but does not work properly on click. When I click on the datepicker field, the calendar pop up shows for 1 second and then hides.
You can move the logic into a method and make proper checks before trying to open the calendar.
0

I experienced some issues with the suggested mixin, I have modified it and provided a complete working example below.

If the user clicks or tabs to the field they will not see a flicker and the calendar will only open once.

<template>
    <toggleDatepicker
        ref="datepicker"
        @focusin.native="showCalendar"
        />    
</template>
<script>
    import Datepicker from 'vuejs-datepicker';
    let toggleDatepicker = {
        mixins: [Datepicker],
        methods: {
            showCalendar(){
                if(this.isOpen) return;
                return Datepicker.methods.showCalendar.apply(this);
            }
        }
    };

    export default {
        components: {toggleDatepicker},
        methods: {
            showCalendar() {
                this.$refs.datepicker.$children[0].$emit('showCalendar')
            },
        },

    }
</script>

This is based on:

  • vuejs-datepicker: ^1.6.2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.