"Once the initial date has been selected, v-Calendar's datepicker allows for setting a

Is there a way to trigger an event for the date range picker of v-calendar after the first date is picked or prevent the inputs from adding the dates until both dates have been selected?

Here is the Vue component I have:

new Vue({
  el: "#app",
  data() {
    return {
      range: {
        start: null,
        end: null
      }
    };
  },
  methods: {
    handleBlur(event) {
      if (event.currentTarget.value === '') {
        event.currentTarget.parentNode.classList.remove("entered");
      }
    },
    handleFocus(event) {
      event.currentTarget.parentNode.classList.add("entered");
    },
    moveLabels() {
      changeClass(this.$refs.filterDateForm);
      changeClass(this.$refs.filterDateTo);
    }
  }
});


function changeClass(input) {
  if (input.value === '') {
    input.parentNode.classList.remove("entered");
  } else {
    input.parentNode.classList.add("entered");
  }
}
@import url 'https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="681e450b09040d060c091a285a465b465c">[email protected]</a>/lib/v-calendar.min.css';
.filter__date-range-holder {
  display: flex;
  justify-content: space-between;
  width: 95%;
}

.filter__date-range-column {
  width: calc(50% - 15px);
}

.form__row {
  position: relative;
  margin: 1.5em 0;
  background: white;
}

.form__control {
  width: 100%;
  border: 1px solid grey;
  font-size: 1rem;
  line-height: 1.5rem;
  color: black;
  padding: 0.75em;
  background: transparent;
}

.invalid .form__control {
  border-color: red;
  outline-color: red;
}

.form__control:focus {
  border-radius: 0;
}

.form__label {
  display: inline-block;
  position: absolute;
  top: 50%;
  left: calc(0.75em + 1px);
  transform: translateY(-50%);
  z-index: 1;
  color: black;
  background: white;
  transition: all 0.25s ease-in-out;
  pointer-events: none;
}

.entered .form__label {
  top: 0;
  left: 0.5rem;
  font-size: 0.6875rem;
  line-height: 0.6875rem;
  padding: 0.2em;
}

.invalid .form__label {
  color: red;
}
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5620233316647860786762">[email protected]</a>/dist/vue.js"></script>
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d8aef5bbb9b4bdb6bcb9aa98eaf6ebf6ec">[email protected]</a>/lib/v-calendar.umd.min.js"></script>
<div id='app'>
  <v-date-picker v-model="range" :popover="{ visibility: 'focus' }" is-range @input="moveLabels">
    <template #default="{ inputValue, inputEvents }">
          <div class="filter__date-range-holder">
            <div class="filter__date-range-column">
              <div class="form__row filter__date-range-row">
                <label class="form__label filter__date-range-label" for="filter-date-from">From</label>
                <input id="filter-date-from" ref="filterDateForm" type="text" name="from" class="form__control form__control--textbox" :value="inputValue.start" v-on="inputEvents.start" @focus="handleFocus" @blur="handleBlur">
              </div>
            </div>
            <div class="filter__date-range-column">
              <div class="form__row filter__date-range-row">
                <label class="form__label filter__date-range-label" for="filter-date-to">To</label>
                <input id="filter-date-to" ref="filterDateTo" type="text" name="to" class="form__control form__control--textbox" :value="inputValue.end" v-on="inputEvents.start" @focus="handleFocus" @blur="handleBlur"gt;
              </div>
            </div>
          </div>
        </template>
  </v-date-picker>
</div>

When using the date range picker, once the first date is selected, both inputs are updated with the selected date and the label overlaps the value. I've tried using the @input event of the date picker and watched the range variable to no avail. These events only trigger once both dates have been selected, so I can only move my labels after the second date is chosen.

Additionally, I attempted adding a @change event to the inputs, but since the value is updated via JavaScript, the change event is not detected.

Answer №1

After much troubleshooting, I was able to resolve this issue by taking the following steps:

  1. Implemented an @input event to properly handle the selection of a date range
  2. Added a @dayclick event to apply the entered class when a day is selected
  3. Included a timeout in the handleBlur method to ensure the blur animation did not start prematurely
  4. Utilized a mutation observer to determine when the calendar closes and remove the entered class if a valid date range is not selected

new Vue({
  // Vue component script goes here
});
/* CSS styles go here */
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/v-calendar/lib/v-calendar.umd.min.js"></script>
<div id='app'>
  <div class="filter__overlay">
    <v-date-picker v-model="range" :popover="{ visibility: 'focus' }" is-range @input="handleCalendarBlur" @dayclick="handleCalendarClick">
      <template #default="{ inputValue, inputEvents }">
          <div class="filter__date-range-holder">
            <div class="filter__date-range-column">
              <div class="form__row filter__date-range-row">
                <label class="form__label filter__date-range-label" for="filter-date-from">From</label>
                <input id="filter-date-from" ref="filterDateForm" type="text" name="from" class="form__control form__control--textbox" :value="inputValue.start" v-on="inputEvents.start" @focus="handleFocus" @blur="handleBlur">
              </div>
            </div>
            <div class="filter__date-range-column">
              <div class="form__row filter__date-range-row">
                <label class="form__label filter__date-range-label" for="filter-date-to">To</label>
                <input id="filter-date-to" ref="filterDateTo" type="text" name="to" class="form__control form__control--textbox" :value="inputValue.end" v-on="inputEvents.start" @focus="handleFocus" @blur="handleBlur">
              </div>
            </div>
          </div>
        </template>
    </v-date-picker>
  </div>
</div>

It is worth noting that this solution may differ from the one implemented on my website, as the behavior varies slightly. However, this revised approach seems to maintain the selected dates in the inputs more effectively.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Divide a nested list into individual lists

I am working on a navigation menu with nested lists and I need to split the nested lists using jQuery while keeping the original headings intact. Can anyone help me achieve this? Below is the HTML code: <ul id="bigList"> <li><a href="#"& ...

Utilizing the power of jQuery's $.each method to dynamically generate HTML select options within an AJAX

I am working with a bootstrap modal that includes a form which requires data from the database. To retrieve this data, I am using PHP as shown below: public function get_view_for_inspection_report_belum_eor(){ $q = $this->inspection->get_view_fo ...

Utilizing regular expressions on a URI parameter in JavaScript

I implemented an ajax function that retrieves three images (PORTRAIT, SQUARE, and LANDSCAPE) from a JSON response: $.ajax({ url: link, method: 'GET', headers: { "Authorization": authToken ...

Passing arrow functions for input validation rules to the Stancil component in Vue

I am attempting to implement an arrow function as input rules, similar to the setup in Vuetify at https://vuetifyjs.com/en/api/v-input/#rules. My approach involves passing rules within an array using the following code: <body> <div id="ap ...

The idea of a webpage completely powered by Ajax

I'm currently in the process of creating a compact website that utilizes full ajax requests for features such as login and registration, all done asynchronously. However, I've come across an issue where if a user decides to refresh the site, any ...

Error occurs when attempting to filter data through input text pasting in Angular

Currently, I am working on a web application that utilizes the Angular framework and angularfire2. The issue I am encountering is related to the data filter functionality not functioning correctly when pasting copied text. Interestingly, it works perfectly ...

Is it possible to have an Iframe that contains content but no src attribute?

During the process of troubleshooting jQuery code on their website (using the Chrome Developer Toolbar), I came across an interesting observation. I found that their examples were embedded within an Iframe: Here, for instance, there is a sample displayed ...

Tips for labeling subplots in PLOTLY JS

Looking for some guidance on adding titles to plots in Plotly JS. I've checked out the documentation but couldn't find anything helpful. Any tips or suggestions would be greatly appreciated! ...

Exploring content within a nested directory on AWS S3

I'm currently experimenting with an ajax request in order to retrieve all image files from a specific subfolder within my S3 bucket. Even though I have set the subfolder to public access using the dropdown menu (view image for reference), I keep enco ...

Is there a way to display the overall count of items in ReCharts?

I'm curious about how to access the additional data items within the 'payload' field of recharts when using material-ui. Despite my efforts to find relevant sources, I have not come across any references pertaining to accessing other group n ...

Having trouble with script tag not loading content in Next.js, even though it works perfectly fine in React

Currently, I am attempting to utilize a widget that I have developed in ReactJS by utilizing script tags as shown below- React Implementation import React from "react"; import { Helmet } from "react-helmet"; const Dust = () => { ...

If the width is below 767, the previous code will not be effective in jQuery

Challenge: How can I ensure that the code within the if statement only executes when the screen width exceeds 767px, and does not work if it is less than 767px? Using jQuery: $(document).ready(function() { $(window).resize(function() { if ($( ...

A guide on cropping and uploading images using ejs and Node.JS

Currently I am utilizing JQuery to crop an image. <link href="/public/css/jquery.Jcrop.min.css" rel="stylesheet" type="text/css" /> <!-- add scripts --> <script src="http://code.jquery.com/jquery-1.9.0.js"></script& ...

The animation is not updating quickly enough

I'm working on a navigation bar that sticks to the top of the page. As the user scrolls down, I want the bar to shrink, and when they scroll back up, it should return to its original size. Issue: The problem I'm facing is that when the user quic ...

Storing the current date() in JSON format using AngularJS

I am currently working on saving various data, including the current date, in a JSON file stored in LocalStorage. While I have been successful in saving the data, the date is saved in the ISO 8601 format: [{"date":"2014-10-13T17:55:32.953Z"}] This format ...

Verify that each interface in an array includes all of its respective fields - Angular 8

I've recently created a collection of typed interfaces, each with optional fields. I'm wondering if there is an efficient method to verify that all interfaces in the array have their fields filled. Here's the interface I'm working wit ...

Manipulating CSS animations through JQuery

Currently, my animation is triggered by a :hover event. However, I would like to change it so that the animation starts when a button is clicked. Can someone guide me on how to manipulate the CSS using JQuery? //CSS .mouth{ top: 268px; left: 273px; ...

Long-term responsibilities in Node.js

Currently, I have a node.js server that is communicating between a net socket and a python socket. The flow is such that when a user sends an asynchronous ajax request with data, the node server forwards it to Python, receives the processed data back, and ...

Issues with using a personalized font in a Stenciljs project

Looking for guidance on implementing a custom font in my Stenciljs app. I have the otf file, unsure if an npm package is necessary. Here's my code: filestructure: -src --components --assets ---Anurti-Regular.tiff ---Anurti-Regular.ttf friends-l ...

Difficulty arises when trying to deploy a React application on a stationary Nginx server

I'm encountering issues trying to deploy my React app on a remote server. My Nginx configuration looks like this: server { listen 80; listen [::]:80; server_name xxx.xxx.x.x; root /var/www/my-site; inde ...