Step-by-step guide to implementing a datepicker textfield with Vuetify 3

I'm currently exploring Vuetify 3 and aiming to implement a textfield that serves as a datepicker. For reference, you can find a similar example in the Vuetify 2 documentation here.

Unfortunately, the Vuetify 3 docs do not yet include an example like this here.

To address this gap, I started creating a prototype as showcased in this Playground

<template>
    <v-app>
      <v-container>
        <v-menu v-model="isMenuOpen" :close-on-content-click="false">
          <template v-slot:activator="{ props }">
            <v-text-field
              label="Selected date"
              :model-value="selectedDate"
              readonly
              v-bind="props"
            ></v-text-field>
          </template>
          <v-date-picker v-model="selectedDate"></v-date-picker>
        </v-menu>
      </v-container>
    </v-app>
  </template>

  <script setup>
    import { ref } from 'vue'

    const isMenuOpen = ref(false)
    const selectedDate = ref()
  </script>
  

I am looking for ways to streamline the datepicker by removing unnecessary elements such as:

  • the header section
  • the action bar at the bottom

My goal is to have the model update automatically whenever a date is selected.

Answer №1

Transformed it into a versatile DatePicker component:

DatePicker.vue

<template>
  <v-menu v-model="isMenuOpen" :close-on-content-click="false">
    <template v-slot:activator="{ props }">
      <v-text-field
        :label="label"
        :model-value="formattedDate"
        readonly
        v-bind="props"
        variant="solo"
        hide-details
      ></v-text-field>
    </template>
    <v-date-picker v-model="selectedDate" hide-actions title="" :color="color">
      <template v-slot:header></template>
    </v-date-picker>
  </v-menu>
</template>

<script setup>
import { ref, computed, watch, defineProps, defineEmits } from "vue";

const { label, color, modelValue } = defineProps([
  "label",
  "color",
  "modelValue",
]);
const emit = defineEmits("update:modelValue");

const isMenuOpen = ref(false);
const selectedDate = ref(modelValue);

const formattedDate = computed(() => {
  return selectedDate.value ? selectedDate.value.toLocaleDateString("en") : "";
});

watch(modelValue, (newDate) => {
  selectedDate.value = newDate;
});

watch(selectedDate, (newDate) => {
  emit("update:modelValue", newDate);
});
</script>
<style>
.v-overlay__content:has(> .v-date-picker) {
  min-width: auto!important;
}
.v-picker-title {
  padding: 0 !important;
}
</style>`

Parent.vue

<date-picker
label="Start Date"
v-model="startDate"
color="primary"
></date-picker>

Answer №2

One way to achieve this is by utilizing a computed property:

 const formattedDate = computed(() => {
    return selectedDate.value.toLocaleDateString('en-US')
  })

Then, you can bind the formattedDate as the model-value in your v-text-field component.

Answer №3

  • To hide the action bar, simply use the hide-action boolean prop

    <v-date-picker hide-actions>
    
  • If you want to remove the header, which is a named slot, provide an empty template within the component

    <v-date-picker>
        <template v-slot:header></template>
    </v-date-picker>
    
  • You can also eliminate the title by passing an empty string as the prop value

    <v-date-picker title="">
    

If you wish to get rid of title, header, and actions altogether, use this format for the date picker:

<v-date-picker hide-actions title="">
    <template v-slot:header></template>
</v-date-picker>

For a working example, check out this demo

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

Creating a central navigation menu for a website

Currently, I am working on incorporating a menu in the center of a webpage (please note that this is not a top navigation menu). Here is the initial setup: https://i.sstatic.net/3arql.png Users are able to click on various menu items to access different ...

Vue - unable to display component CSS classes in application when using class-style bindings

Just diving into Vue and starting with class-style binding syntax. I'm facing an issue where the CSS classes for header and footer that I've defined are not displaying, even though I've referenced them in the component tags. Can't seem ...

How to delete a specific key-value pair from an object in React.js

There is an object stored in the state: this.state = { selectedValue: {} } Now, I am adding a property to this object as follows: if (e.currentTarget.checked) { this.setState({ selectedType: { ...this.state.selectedType, ...

Vue dynamic components fail to re-render within a v-for loop upon data changes

I've created a dynamic table component that allows me to modify columns by changing their ordering, adding new ones, or removing existing ones. The structure of my table body is as follows: <tr v-for="row in data" :key="row.id"& ...

Sending a parameter to a different function (on a separate webpage)

At the start of my webpage, there are two radio buttons on the first page, each with its own value. Upon clicking a link to move to the second page, a for loop is activated to grab the value of the selected button. The script has been tested and works as e ...

Handling errors in XMLHttpRequest using Axios in React JS

I am currently utilizing the REACT-JS framework for my FRONT-END development: Below is the action I am calling from REDUX-REACT export function UserLogin(values) { var headers = { 'Access-Control-Allow-Origin': '*', ...

Angular filter within a nested ng-repeat loop

I've encountered an issue with nested filtering in Angular, where two filters are dependent on each other. What I'm trying to achieve is the following: <div ng-repeat="g in groups | filter:groupFilter"> ... <tr ng-repeat="c in g.co ...

A distinctive noise is heard when hovering over multiple instances of a div

I'm trying to implement a feature where a unique sound plays when hovering over a specific div element with a particular class (.trigger). However, I am encountering an issue where multiple instances of this div class result in the same sound being pl ...

Looking to create a toggle button to show more or show less dynamic API data in a React component?

I have a specific requirement for my web page where I have multiple tabs, each of which displays dynamic data obtained from an API. The data is constantly changing on a daily basis. Within each tab, there is a rectangular box with a fixed height, and below ...

Vuetify 3 Spacing System

I need assistance in creating a v-row without gutters (with a red background) in Vuetify 3. I want it to span the entire width of the screen without any spaces in between. Can anyone provide guidance on this? <template> <v-container fluid> ...

The peculiar formatting issue with the jQuery datepicker that arises when adding more months

When adjusting the numberOfMonths parameter to a higher value such as 6, I noticed that the datepicker display appears unusual with the background extending further than expected. Take a look at my demonstration on jsfiddle: http://jsfiddle.net/zw3z2vjh/ ...

display a div positioned relatively next to another div

I'm having trouble displaying a textbox next to another div on my webpage. Instead of appearing next to the div, it is showing up below it. The textbox needs to have an absolute position. You can see the issue in action by checking out this demo. Than ...

Redux state not reflecting changes until second click

My redux store has a simple boolean setup to track whether a sidebar is expanded or not. However, I'm encountering an issue where, even though the default value is false, clicking the toggle button outputs false first. Ideally, if it's initially ...

viewing vue documentation from your local machine

I was attempting to run the Vue.js documentation offline (locally) and encountered an issue. It had worked previously, but now I'm unsure of what went wrong. I followed the steps outlined in this post: here The basic steps are: Install hexo-cli gl ...

What is the advantage of utilizing AngularJs .constant() over simply declaring a JavaScript const variable?

Currently, I am involved in a project using AngularJS where we have implemented a .constant() provider to define some fundamental details that are utilized throughout the entire project. An example of this would be specifying a cookie name like so: .const ...

Can you pass an array as a prop to a Vue.js component?

I am working on a specialized input component named 'field', which has a set of predefined props listed as: props: ['value','cols','label','group','name'] Since 'field' essentially wr ...

How to efficiently transfer data between Node and Angular 7 using Electron

After setting up an Angular 7 application running on http://localhost:4200, I developed a Node JS application responsible for authenticating users on Facebook, accessible at http://localhost:3000. The callback redirection functions correctly within the No ...

Ways to pass scope between the same controller multiple times

There is a unique scenario in which I have a controller appearing in 2 different locations on a page. This arrangement is necessary for specific reasons. To illustrate, the simplified HTML structure is as follows: <aside ng-if="aside.on" ng-controller ...

jQuery does not have the capability to add or remove classes

I am currently using Vue.js within Laravel, and in the watch action, I am utilizing jQuery. I am puzzled as to why hasClass is functioning correctly, but addClass, removeClass, and toggleClass are not working as expected. Here is a snippet of my HTML cod ...

What is the best way to acquire the href value from this source?

Looking to extract the dynamic value "3 Sent" from the html snippet provided. How can this be achieved? <ul class="nav nav-tabs some-tabs"> <li class="active"> <a href="#accepted" data-toggle="tab">1 Accepted</ ...