What is the process for activating the quasar timepicker once a user has selected a time?

The functionality of the timepicker in Quasar doesn't quite meet my expectations. I don't want to add another library just for this feature. The main issue I have is that it doesn't close automatically after selecting a time. I managed to figure out how to use the date picker, but the time picker is still a challenge. The closest solution I found was to check the v-model for a value. If there is a value, I close the picker. However, the problem with this approach is that I can't reopen the picker without clearing the input field.

Here's the current workaround I implemented:

<div id="q-app">
  <div class="q-pa-md">
      <div class="q-gutter-sm row">
        <q-input filled v-model="time" mask="time" :rules="['time']">
          <template v-slot:append>
            <q-icon name="access_time" class="cursor-pointer">
              <q-popup-proxy 
                ref="qTimeProxy" 
                transition-show="scale" 
                transition-hide="scale">
                <q-time 
                   v-model="time" 
                   v-if="!time ? () => $refs.qTimeProxy.hide() : null">
                </q-time>
              </q-popup-proxy>
            </q-icon>
          </template>
        </q-input>
      </div>
    </div>
</div>

This setup is not ideal and I'm looking for a way to improve it so I can reopen the picker even if a time is already selected, while ensuring that it closes after a new time is picked.

One major drawback is that this implementation does not consider AM/PM selection. Selecting a time without specifying AM or PM can be inefficient. Has anyone encountered this issue before?

For a live example, you can visit the PLAYGROUND

Thank you in advance

Answer №1

To make the q-time popover disappear, simply add an @input event on the element and then call a method to hide it.

<q-input
                filled
                v-model="time"
                mask="time"
                :rules="['time']"
                >
                <template v-slot:append>
                  <q-icon name="access_time" class="cursor-pointer">
                    <q-popup-proxy
                      ref="qTimeProxy"
                    >
                      <q-time
                        v-model="time"
                        @input="closeDialog"
                      ></q-time>
                    </q-popup-proxy>
                  </q-icon>
                </template>
              </q-input>


  methods:  {
    closeDialog () {
      this.$refs.qTimeProxy.hide()
    }
  }

For a functional demonstration, visit this CodePen link - https://codepen.io/Pratik__007/pen/yLyJwWq?editors=1010

Answer №2

Quasar V2.15.4 has introduced a new event called @update:model-value, replacing the previous @input event.

When handling this event, the callback function should have the following signature:

((value: string | null, details: {
    year: number;
    month: number;
    day: number;
    hour: number;
    minute: number;
    second: number;
    millisecond: number;
    changed: boolean;
}) => void)

The value parameter in the callback contains the string version of the selected time, while the details object provides a detailed breakdown of the model being updated. It is crucial to note that the @update:model-value event triggers before the actual model update, allowing for comparisons between the current model value and the details object to determine when to close the pop-up.

In the snippet below, as the user selects a minute value from the time-picker, the pop-up proxy will be hidden. This behavior can be easily modified to accommodate different scenarios based on other time components being affected.

Example:
    <template>
        <q-popup-proxy
         transition-show="scale"
         transition-hide="scale"
         ref="qTimeProxy"
         cover
        >
         <q-time
          mask="YYYY-MM-DD HH:mm"
          format24h
          v-model="dateVal"
          @update:model-value="timeUpdated"
         >
          <div class="row items-center justify-end">
           <q-btn v-close-popup label="Close" color="primary"/>
          </div>
         </q-time>
        </q-popup-proxy>
    </template>

    <script>
        const dateVal = ref();    
        const timeUpdated = (
              value: string | null,
              details: {
                minute: number;
              }
            ) => {
              // Close the time popup after selecting the minute
              var curDate = dateVal.value ? new Date(dateVal.value) : null;
              if (curDate?.getMinutes() !== details.minute) qTimeProxy.value?.hide();
          }
    </script>

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

Having trouble launching the emulator in VS Code for React Native?

I'm having trouble launching the android emulator on VS Code to run React-Native. I already have an emulator set up in Android Studio, but when I try to launch it, I get the error message: Error Failed to launch emulator. Reason: No emulators found as ...

AngularJS not swapping the data-url with the scope URL provided in {{ uploadUrl }}

Currently, I am utilizing jQuery fileupload to send a file to a specific URL. Within my controller, the code snippet below demonstrates how I generate the URL for uploading: uploadService.getUploadURL($scope.projectId).then(function (url) { $scope.up ...

What could be causing the asynchronous function to return a pending promise even after using the await keyword?

I seem to be overlooking something as I cannot comprehend why my promise does not resolve. I have simplified the code to this basic example: ... console.log("before"); const promise = second(); console.log("after"); console.l ...

Difficulty with Angular JS` ng-repeat functionality

I attempted to create a sample similar to the ones found on https://angularjs.org/ Here is the JS fiddle I put together. Could someone provide assistance? What seems to be the issue? The HTML code is as follows: <h2> To Do </h2> <div ng- ...

Enhancing UI Design with Styled-Components and Material Components using Media Queries

Struggling to grasp media queries with MUI and styled components. Take a look at the following syntax using styled-components: Syntax 1: const Video = styled.video` width: 860px; @media ${device.mobileSM} { width: 90%; } `; Additionally, there ...

Utilize AngularFire to generate a new User and invoke the factory function to showcase notifications

I recently started working with AngularJS and wanted to integrate it with AngularFire/Firebase for storing project data. I created a factory that manages notifications to be displayed in a centralized location. Here is the structure of my factory: myApp.f ...

Is there a method to use media queries to dynamically switch JavaScript files based on the user's device?

I've been working on optimizing the mobile layout of my website, and while I have successfully implemented a media query with a different stylesheet for mobile devices, I'm struggling to determine if it's feasible to also load a separate Jav ...

What is the best way to determine the quantity of elements received from an ajax request?

Here's the method I am currently using to count the number of li elements returned from an AJAX call: $.post('@Url.Action("actionName", "controller")', function (data) { $('#notificationCounter').html($(data).find('li&a ...

Leveraging webpack for requiring modules in the browser

I've been attempting to utilize require('modules') in the browser with webpack for the past couple of days, but I could achieve the same functionality with browserify in just 5 minutes... Below is my custom webpack.config.js file: var webp ...

When attempting to install Vue Js, I encounter the following npm error

I recently set up Node.js using Snap on my Parrot OS system. However, I've noticed that the node_modules folder is missing after /usr/local/lib When running npm install -g @vue/cli, I encountered this error: npm WARN deprecated @hapi/[email pro ...

Retrieve every video on React.js channel from YouTube

Currently, I am working on integrating react-youtube into my React App with the goal of accessing all videos from a specific YouTube channel. The challenge I am facing is that I need to display these videos as thumbnails, exactly how they are listed in the ...

When using ReactJS, hovering over a row in a table should trigger a change in the background color of the corresponding row in another table with the same index

I need to create a feature where hovering over a row in the first table will highlight a corresponding row in the second table. The highlighting should be based on the index of the hovered row. Here is an example: <div> <table> < ...

What's the Hold-Up with IntersectionObserver in Brackets?

As a novice in the world of web development, I have been utilizing Brackets as my main tool. Recently, I've encountered a few hurdles specifically related to javascript. One issue I'm facing is trying to implement lazy loading on a div containing ...

Bringing a JavaScript function into a TypeScript file results in it being treated as a namespace

Trying to bring a vanilla JavaScript function into a TypeScript file within a React Native app has presented some challenges. The import process goes smoothly when working with JS, but switching to TS triggers the error message: Cannot use namespace &apos ...

Exploring the intricacies of extracting nested JSON data in TypeScript

Can someone help me with this issue? https://example.com/2KFsR.png When I try to access addons, I only see [] but the web console indicates that addons are present. This is my JSON structure: https://example.com/5NGeD.png I attempted to use this code: ...

Clicking the checkbox does not synchronize with the table row

Is there a way to activate the button when the user selects the checkbox directly? Currently, the button only becomes enabled when the user clicks on a table row that is clickable. var btn = $("#btn"); btn.attr("disabled","disabled"); $("#table tbo ...

Exploring the intricacies of Implementing Chromecast Networks, with a curious nod towards potentially mirroring it with

In my current project, I am aiming to replicate the functionality of a Chromecast on a Roku device. To achieve this, I need to first discover the Roku using UDP and then send an HTTP POST request to control it. During a recent developer fest where I learn ...

Determine whether a request is for CSS or JavaScript when using NodeJS and Express

My routing configuration includes the following setup: app.get('*', function (req, res, next) { req.xhr ? next() : res.render('layout/layout'); }); The idea behind this is to return the base layout if the request is not an XMLHttp ...

Will there ever be a possibility in the future to compile from D 2.0 to Javascript?

An experienced C++ programmer (that's me) is venturing into other programming languages and considering the value of learning more about D 2.0. The clean, fresh rewrite of D has caught my eye with its pragmatic and wise choices. Now, I'm eager to ...

Ways to identify when the socket has been opened by the client?

When utilizing socket.io on the client browser side, is there a way to identify when the socket connection has been successfully opened? I am also interested in monitoring other standard messages such as errors and disconnections. In comparison to the Web ...