Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(VDatePicker): example with week selection #20859

Closed

Conversation

J-Sek
Copy link
Contributor

@J-Sek J-Sek commented Jan 11, 2025

Description

I was asked about it on Discord and initially implemented it without Date Adapter. It was pure luck that I encountered a bug related to time zones and their DST variants. Other developers could benefit from example that utilizes useDate() to showcase clean and bug-free implementation.

Markup:

<template>
  <v-container>
    <v-row justify="space-around">
      <div>
        <v-date-picker
          :model-value="dates"
          title="Week selection"
          show-adjacent-months
          show-week
          @update:model-value="selectWeek($event)"
        >
          <template v-slot:header>
            <v-date-picker-header>
              {{ headerText }}
            </v-date-picker-header>
          </template>
        </v-date-picker>
        <div class="mt-3 d-flex justify-center">
          <v-btn :disabled="!dates.length" @click="dates = []">Clear</v-btn>
        </div>
      </div>
    </v-row>
  </v-container>
</template>

<script setup>
  import { computed, ref } from 'vue'
  import { useDate } from 'vuetify'

  const adapter = useDate()

  const dates = ref([])

  const headerText = computed(() => {
    if (dates.value.length < 2) return 'Select week'

    const firstWeekDay = adapter.startOfWeek(dates.value[0])
    const firstYearDay = adapter.startOfYear(dates.value[0])

    const firstDayDiff = adapter.getDiff(firstWeekDay, firstYearDay, 'days')
    const weekOffset = firstYearDay.getDay() === 0 ? 1 : 0

    return `${firstWeekDay.getFullYear()} Week ${weekOffset + Math.ceil(firstDayDiff / 7)}`
  })

  function selectWeek (date) {
    const firstDay = adapter.startOfWeek(date)
    dates.value = Array.from({ length: 7 })
      .map((_, i) => adapter.addDays(firstDay, i))
  }
</script>

@J-Sek
Copy link
Contributor Author

J-Sek commented Jan 15, 2025

Closed in favor of #20867

@J-Sek J-Sek closed this Jan 15, 2025
@J-Sek J-Sek deleted the docs/vdatepicker-week-selection branch January 16, 2025 11:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant