Skip to content

Commit af0dc0a

Browse files
VdustRmarcosmoura
authored andcommitted
feat(MdDatepicker): custom first day of a week (#1409)
* 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
1 parent a2cbc98 commit af0dc0a

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
<template>
22
<div>
33
<md-datepicker v-model="selectedDate" />
4+
<md-field>
5+
<label for="movie">First day of a week</label>
6+
<md-select v-model="firstDayOfAWeek">
7+
<md-option value="0">Sunday</md-option>
8+
<md-option value="1">Monday</md-option>
9+
</md-select>
10+
<span class="md-helper-text">This config is global.</span>
11+
</md-field>
412
</div>
513
</template>
614

@@ -9,6 +17,16 @@
917
name: 'BasicDatepicker',
1018
data: () => ({
1119
selectedDate: null
12-
})
20+
}),
21+
computed: {
22+
firstDayOfAWeek: {
23+
get () {
24+
return this.$material.locale.firstDayOfAWeek
25+
},
26+
set (val) {
27+
this.$material.locale.firstDayOfAWeek = val
28+
}
29+
}
30+
}
1331
}
1432
</script>

src/components/MdDatepicker/MdDatepickerDialog.vue

+20-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@
3131
<md-button class="md-dense md-datepicker-month-trigger" @click="currentView = 'month'">{{ currentMonthName }} {{ currentYear }}</md-button>
3232

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

3738
<div class="md-datepicker-days">
38-
<span class="md-datepicker-empty" v-for="day in firstDayOfMonth" :key="'day-empty-'+day"></span>
39+
<span class="md-datepicker-empty" v-for="day in prefixEmptyDays" :key="'day-empty-'+day"></span>
3940
<div class="md-datepicker-day" v-for="day in daysInMonth" :key="'day-'+day">
4041
<span
4142
class="md-datepicker-day-button"
@@ -109,6 +110,8 @@
109110
import MdArrowLeftIcon from 'core/icons/MdArrowLeftIcon'
110111
import MdDialog from 'components/MdDialog/MdDialog'
111112
113+
const daysInAWeek = 7
114+
112115
const getElements = (el, selector) => {
113116
if (el && el.querySelector) {
114117
return el.querySelectorAll(selector)
@@ -139,6 +142,16 @@
139142
availableYears: null
140143
}),
141144
computed: {
145+
firstDayOfAWeek () {
146+
// normalize
147+
let firstDayOfAWeek = Number(this.$material.locale.firstDayOfAWeek)
148+
if (Number.isNaN(firstDayOfAWeek) || !Number.isFinite(firstDayOfAWeek)) {
149+
return 0
150+
}
151+
firstDayOfAWeek = Math.floor(firstDayOfAWeek) % daysInAWeek
152+
firstDayOfAWeek += firstDayOfAWeek < 0 ? daysInAWeek : 0
153+
return firstDayOfAWeek
154+
},
142155
locale() {
143156
return this.$material.locale;
144157
},
@@ -165,6 +178,11 @@
165178
firstDayOfMonth () {
166179
return startOfMonth(this.currentDate).getDay()
167180
},
181+
prefixEmptyDays () {
182+
let prefixEmptyDays = this.firstDayOfMonth - this.firstDayOfAWeek
183+
prefixEmptyDays += prefixEmptyDays < 0 ? daysInAWeek : 0
184+
return prefixEmptyDays
185+
},
168186
daysInMonth () {
169187
return getDaysInMonth(this.currentDate)
170188
},

src/material.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const init = () => {
1515
shorterDays: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
1616
months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
1717
shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'],
18-
shorterMonths: ['J', 'F', 'M', 'A', 'M', 'Ju', 'Ju', 'A', 'Se', 'O', 'N', 'D']
18+
shorterMonths: ['J', 'F', 'M', 'A', 'M', 'Ju', 'Ju', 'A', 'Se', 'O', 'N', 'D'],
19+
firstDayOfAWeek: 0
1920
}
2021
})
2122

0 commit comments

Comments
 (0)