Skip to content

Commit

Permalink
feat(MdDatepicker): custom first day of a week
Browse files Browse the repository at this point in the history
new props `md-first-day-of-a-week` for custom first day of a week.

fix #1397
  • Loading branch information
VdustR committed Jan 17, 2018
1 parent d026496 commit f0788be
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
6 changes: 6 additions & 0 deletions docs/app/pages/Components/Datepicker/Datepicker.vue
Expand Up @@ -70,6 +70,12 @@
type: 'Boolean',
description: 'Override native browser pickers by changing type of input to text.',
defaults: 'true'
},
{
name: 'md-first-day-of-a-week',
type: 'Number|String',
description: 'The first day of a week. <code>0</code> stand for Sunday and <code>1</code> stand for Monday.',
defaults: '0'
}
]
}
Expand Down
@@ -1,6 +1,8 @@
<template>
<div>
<md-datepicker v-model="selectedDate" />
Start from Monday:
<md-datepicker v-model="selectedDate" md-first-day-of-a-week="1" />
</div>
</template>

Expand Down
5 changes: 3 additions & 2 deletions src/components/MdDatepicker/MdDatepicker.vue
Expand Up @@ -6,7 +6,7 @@
<slot />

<keep-alive>
<md-datepicker-dialog :md-date.sync="selectedDate" :md-disabled-dates="mdDisabledDates" v-if="showDialog" @md-closed="toggleDialog" />
<md-datepicker-dialog :md-date.sync="selectedDate" :md-disabled-dates="mdDisabledDates" :md-first-day-of-a-week="mdFirstDayOfAWeek" v-if="showDialog" @md-closed="toggleDialog" />
</keep-alive>

<md-overlay class="md-datepicker-overlay" md-fixed :md-active="showDialog" @click="toggleDialog" />
Expand Down Expand Up @@ -44,7 +44,8 @@
mdOverrideNative: {
type: Boolean,
default: true
}
},
mdFirstDayOfAWeek: {}
},
data: () => ({
showDialog: false,
Expand Down
25 changes: 22 additions & 3 deletions src/components/MdDatepicker/MdDatepickerDialog.vue
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 All @@ -127,7 +130,8 @@
},
props: {
mdDate: Date,
mdDisabledDates: [Array, Function]
mdDisabledDates: [Array, Function],
mdFirstDayOfAWeek: null
},
data: () => ({
currentDate: null,
Expand All @@ -139,6 +143,16 @@
availableYears: null
}),
computed: {
firstDayOfAWeek () {
// normalize
let firstDayOfAWeek = Number(this.mdFirstDayOfAWeek)
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 +179,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

0 comments on commit f0788be

Please sign in to comment.