Skip to content

Commit

Permalink
feat(MdDatepicker): custom first day of a week (#1409)
Browse files Browse the repository at this point in the history
* feat(MdDatepicker): custom first day of a week

new props `md-first-day-of-a-week` for custom first day of a week.

fix #1397

* Revert "feat(MdDatepicker): custom first day of a week"

This reverts commit f0788be.

* feat(MdDatepicker): custom first day of a week

global config `locale.firstDayOfAWeek`

fix #1397
  • Loading branch information
VdustR authored and marcosmoura committed Jan 23, 2018
1 parent a2cbc98 commit af0dc0a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<template>
<div>
<md-datepicker v-model="selectedDate" />
<md-field>
<label for="movie">First day of a week</label>
<md-select v-model="firstDayOfAWeek">
<md-option value="0">Sunday</md-option>
<md-option value="1">Monday</md-option>
</md-select>
<span class="md-helper-text">This config is global.</span>
</md-field>
</div>
</template>

Expand All @@ -9,6 +17,16 @@
name: 'BasicDatepicker',
data: () => ({
selectedDate: null
})
}),
computed: {
firstDayOfAWeek: {
get () {
return this.$material.locale.firstDayOfAWeek
},
set (val) {
this.$material.locale.firstDayOfAWeek = val
}
}
}
}
</script>
22 changes: 20 additions & 2 deletions src/components/MdDatepicker/MdDatepickerDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
<md-button class="md-dense md-datepicker-month-trigger" @click="currentView = 'month'">{{ currentMonthName }} {{ currentYear }}</md-button>

<div class="md-datepicker-week">
<span v-for="(day, index) in locale.shorterDays" :key="index">{{ day }}</span>
<span v-for="(day, index) in locale.shorterDays" :key="index" v-if="index >= firstDayOfAWeek">{{ day }}</span>
<span v-for="(day, index) in locale.shorterDays" :key="index" v-if="index < firstDayOfAWeek">{{ day }}</span>
</div>

<div class="md-datepicker-days">
<span class="md-datepicker-empty" v-for="day in firstDayOfMonth" :key="'day-empty-'+day"></span>
<span class="md-datepicker-empty" v-for="day in prefixEmptyDays" :key="'day-empty-'+day"></span>
<div class="md-datepicker-day" v-for="day in daysInMonth" :key="'day-'+day">
<span
class="md-datepicker-day-button"
Expand Down Expand Up @@ -109,6 +110,8 @@
import MdArrowLeftIcon from 'core/icons/MdArrowLeftIcon'
import MdDialog from 'components/MdDialog/MdDialog'
const daysInAWeek = 7
const getElements = (el, selector) => {
if (el && el.querySelector) {
return el.querySelectorAll(selector)
Expand Down Expand Up @@ -139,6 +142,16 @@
availableYears: null
}),
computed: {
firstDayOfAWeek () {
// normalize
let firstDayOfAWeek = Number(this.$material.locale.firstDayOfAWeek)
if (Number.isNaN(firstDayOfAWeek) || !Number.isFinite(firstDayOfAWeek)) {
return 0
}
firstDayOfAWeek = Math.floor(firstDayOfAWeek) % daysInAWeek
firstDayOfAWeek += firstDayOfAWeek < 0 ? daysInAWeek : 0
return firstDayOfAWeek
},
locale() {
return this.$material.locale;
},
Expand All @@ -165,6 +178,11 @@
firstDayOfMonth () {
return startOfMonth(this.currentDate).getDay()
},
prefixEmptyDays () {
let prefixEmptyDays = this.firstDayOfMonth - this.firstDayOfAWeek
prefixEmptyDays += prefixEmptyDays < 0 ? daysInAWeek : 0
return prefixEmptyDays
},
daysInMonth () {
return getDaysInMonth(this.currentDate)
},
Expand Down
3 changes: 2 additions & 1 deletion src/material.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const init = () => {
shorterDays: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'],
shorterMonths: ['J', 'F', 'M', 'A', 'M', 'Ju', 'Ju', 'A', 'Se', 'O', 'N', 'D']
shorterMonths: ['J', 'F', 'M', 'A', 'M', 'Ju', 'Ju', 'A', 'Se', 'O', 'N', 'D'],
firstDayOfAWeek: 0
}
})

Expand Down

0 comments on commit af0dc0a

Please sign in to comment.