vuetify icon indicating copy to clipboard operation
vuetify copied to clipboard

[Bug Report] add keyboard controls to v-datepicker

Open bigal41 opened this issue 8 years ago • 10 comments

Problem to solve

Currently user's need to use the mouse to select date. Which is not very quick and efficient.

Proposed solution

Use the arrow keys to move around the calendar when it is open. Press enter to select the date the user has currently selected.

bigal41 avatar Nov 16 '18 06:11 bigal41

This impacts keyboard accessibility. Is there any update on this?

robws avatar Feb 07 '20 22:02 robws

May sound outdated but we also have a need for this feature, we have forms to be filled repeatedly and some users prefer using keyboard only.

bob-lee avatar Mar 02 '20 19:03 bob-lee

I've changed this issue to refer only to datepicker and created a separate issue for time picker

jacekkarczmarczyk avatar Mar 02 '20 23:03 jacekkarczmarczyk

Meanwhile, I've wrapped it in a custom component and implemented basic keyboard navigation. Holding shift while pressing left or right, goes to previous or next month, respectively.

Note: This implementation uses moment to simplify date manipulation.

DateField.vue

<template>
  <div ref="container">
    <v-menu
      :attach="$refs.container"
      transition="scale-transition"
      full-width
      min-width="290px"
      :close-on-content-click="false"
      v-model="showMenu"
    >
      <template #activator="{on}">
        <v-text-field
          ref="textField"
          prepend-icon="mdi-calendar"
          label="Date"
          readonly
          :value="value"
          v-on="on"
          @keydown.up="up"
          @keydown.down="down"
          @keydown.left="left"
          @keydown.right="right"
          @keydown.enter="dateSelected"
          @focus="$emit('focus')"
        ></v-text-field>
      </template>

      <v-date-picker
        ref="datePicker"
        no-title
        scrollable
        :value="value"
        @click:date="dateSelected"
        @change="$emit('input', $event)"
      ></v-date-picker>
    </v-menu>
  </div>
</template>
<script>
import moment from 'moment'

export default {
  props: {
    value: {
      type: String,
      required: true
    },

    show: {
      type: Boolean,
      default: false
    }
  },

  data() {
    return {
      showMenu: false
    }
  },

  watch: {
    show: {
      immediate: true,

      handler(show) {
        show = show === true || show === false ? show : false

        this.toggleMenu(show, false)
      }
    }
  },

  methods: {
    toggleMenu(show = null, emit = true) {
      show = show !== null ? show : !this.showMenu

      if (show === this.showMenu) {
        return
      }

      this.showMenu = show

      if (emit) {
        this.$emit('update:show', show)
      }
    },

    dateSelected() {
      this.toggleMenu(false)
      this.$emit('date-selected')
    },

    // keyboard navigation

    pickModified(modifier) {
      this.$refs.datePicker.dateClick(
        modifier(moment(this.$refs.datePicker.inputDate)).format('YYYY-MM-DD')
      )
    },

    up() {
      this.pickModified(m => m.subtract(1, 'weeks'))
    },

    down() {
      this.pickModified(m => m.add(1, 'weeks'))
    },

    left(e) {
      this.pickModified(m =>
        e.shiftKey ? m.subtract(1, 'month') : m.subtract(1, 'days')
      )
    },

    right(e) {
      this.pickModified(m =>
        e.shiftKey ? m.add(1, 'month') : m.add(1, 'days')
      )
    }
  }
}
</script>

pdcmoreira avatar Apr 03 '20 22:04 pdcmoreira

This is ridiculous how little Vuetify cares about accessibility. Same issue as the VSelect in that keyboard arrow commands don't work out of the box.

Pls fix

martinmckenna avatar Jun 04 '20 16:06 martinmckenna

I now feel like keyboard support may be a separate functionality. In my case, I made text field editable and handle blur event where I assign date picker model, that way I managed to add keyboard support with data picker intact.

<v-menu
  v-model="myDateShown"
  ...
>
  <template v-slot:activator="{ on }">
    <v-text-field
      v-model="keyboardEntered"
      @blur="handleBlur"
      v-on="on"
    ></v-text-field>
  </template>
  <v-date-picker
    v-model="myObject.myDate"
    @input="myDateShown = false"
  ></v-date-picker>
</v-menu>
    handleBlur(e) {
      this.myObject.myDate = // assign parsed value from this.keyboardEntered
      setTimeout(() => this.myDateShown = false, 100)
    }

bob-lee avatar Jun 08 '20 22:06 bob-lee

Hi, thanks for all of the improvements already. I'm just wondering if navigation with arrow keys on dates, months and years is something that is on the pipeline?

RiqueBR avatar Jul 01 '20 17:07 RiqueBR

Yes, planned for 3.0, nothing set in stone though

jacekkarczmarczyk avatar Jul 01 '20 17:07 jacekkarczmarczyk

Note also that formatting together with reasonable label / message cues and an editable <v-text-field> can help the user fill in dates using the keyboard, even without full keyboard navigability of the calendar dropdown.

candu avatar Oct 08 '20 13:10 candu

Saving this for reference to the native date picker: https://codepen.io/johnjleider/pen/wvWbmdj?editors=101

johnleider avatar Nov 19 '20 19:11 johnleider