Skip to content

Commit 01e6f93

Browse files
committed
feat(Calendar, DatePicker, DateRangePicker): show weeks numbers
1 parent 50e198a commit 01e6f93

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

js/src/calendar.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ const Default = {
6060
selectAdjacementDays: false,
6161
selectEndDate: false,
6262
showAdjacementDays: true,
63+
showWeekNumber: false,
6364
startDate: null,
64-
weekdayFormat: 2
65+
weekdayFormat: 2,
66+
weekNumbersLabel: null
6567
}
6668

6769
const DefaultType = {
@@ -77,8 +79,10 @@ const DefaultType = {
7779
selectAdjacementDays: 'boolean',
7880
selectEndDate: 'boolean',
7981
showAdjacementDays: 'boolean',
82+
showWeekNumber: 'boolean',
8083
startDate: '(date|string|null)',
81-
weekdayFormat: '(number|string)'
84+
weekdayFormat: '(number|string)',
85+
weekNumbersLabel: '(string|null)'
8286
}
8387

8488
/**
@@ -333,19 +337,26 @@ class Calendar extends BaseComponent {
333337
const monthDetails = getMonthDetails(year, month, this._config.firstDayOfWeek)
334338
const listOfMonths = createGroupsInArray(getMonthsNames(this._config.locale), 4)
335339
const listOfYears = createGroupsInArray(getYears(date.getFullYear()), 4)
336-
const weekDays = monthDetails[0]
340+
const weekDays = monthDetails[0].days
337341

338342
const calendarTable = document.createElement('table')
339343
calendarTable.innerHTML = `
340344
${this._view === 'days' ? `
341345
<thead>
342346
<tr>
347+
${this._config.showWeekNumber ?
348+
`<th class="calendar-cell">
349+
<div class="calendar-header-cell-inner">
350+
${this._config.weekNumbersLabel ?? ''}
351+
</div>
352+
</th>` : ''
353+
}
343354
${weekDays.map(({ date }) => (
344355
`<th class="calendar-cell">
345356
<div class="calendar-header-cell-inner">
346357
${typeof this._config.weekdayFormat === 'string' ?
347-
date.toLocaleDateString(this._config.locale, { weekday: this._config.weekdayFormat }) :
348-
date
358+
date.toLocaleDateString(this._config.locale, { weekday: this._config.weekdayFormat }) :
359+
date
349360
.toLocaleDateString(this._config.locale, { weekday: 'long' })
350361
.slice(0, this._config.weekdayFormat)}
351362
</div>
@@ -355,7 +366,13 @@ class Calendar extends BaseComponent {
355366
</thead>` : ''}
356367
<tbody>
357368
${this._view === 'days' ? monthDetails.map(week => (
358-
`<tr>${week.map(({ date, month }) => (
369+
`<tr class="calendar-row">
370+
${this._config.showWeekNumber ?
371+
`<td class="calendar-cell week-number">
372+
<div class="calendar-cell-inner">${week.weekNumber}</div>
373+
</td>` : ''
374+
}
375+
${week.days.map(({ date, month }) => (
359376
month === 'current' || this._config.showAdjacementDays ?
360377
`<td class="calendar-cell ${this._dayClassNames(date, month)}">
361378
<div class="calendar-cell-inner day" data-coreui-date="${date}">

js/src/util/calendar.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ const getTrailingDays = (year, month, leadingDays, monthDays) => {
124124
return dates
125125
}
126126

127+
export const getDayNumber = date =>
128+
Math.ceil((date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24)
129+
130+
export const getWeekNumber = date => Math.ceil(getDayNumber(date) / 7)
131+
127132
export const getMonthDetails = (year, month, firstDayOfWeek) => {
128133
const daysPrevMonth = getLeadingDays(year, month, firstDayOfWeek)
129134
const daysThisMonth = getMonthDays(year, month)
@@ -132,10 +137,16 @@ export const getMonthDetails = (year, month, firstDayOfWeek) => {
132137
const weeks = []
133138
for (const [index, day] of days.entries()) {
134139
if (index % 7 === 0 || weeks.length === 0) {
135-
weeks.push([])
140+
weeks.push({
141+
days: []
142+
})
143+
}
144+
145+
if ((index + 1) % 7 === 0) {
146+
weeks[weeks.length - 1].weekNumber = Math.ceil(getDayNumber(day.date) / 7)
136147
}
137148

138-
weeks[weeks.length - 1].push(day)
149+
weeks[weeks.length - 1].days.push(day)
139150
}
140151

141152
return weeks

scss/_calendar.scss

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
--#{$prefix}calendar-cell-range-hover-bg: #{$calendar-cell-range-hover-bg};
2828
--#{$prefix}calendar-cell-range-hover-border-color: #{$calendar-cell-range-hover-border-color};
2929
--#{$prefix}calendar-cell-today-color: #{$calendar-cell-today-color};
30+
--#{$prefix}calendar-cell-week-number-color: #{$calendar-cell-week-number-color};
3031
// scss-docs-end calendar-css-vars
3132

3233
font-weight: initial;
@@ -120,13 +121,17 @@
120121
padding: 1px 0;
121122
text-align: center;
122123

123-
&:not(.disabled):not(.next):not(.previous):hover .calendar-cell-inner,
124+
&:not(.disabled):not(.next):not(.previous):not(.week-number):hover .calendar-cell-inner,
124125
&.clickable:hover .calendar-cell-inner {
125126
color: var(--#{$prefix}calendar-cell-hover-color);
126127
cursor: pointer;
127128
background-color: var(--#{$prefix}calendar-cell-hover-bg);
128129
}
129130

131+
&.week-number {
132+
color: var(--#{$prefix}calendar-cell-disabled-color);
133+
}
134+
130135
&.today .calendar-cell-inner {
131136
color: var(--#{$prefix}calendar-cell-today-color);
132137
}

scss/_variables.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2330,6 +2330,8 @@ $calendar-cell-range-hover-bg: rgba(var(--#{$prefix}primary-rgb),
23302330
$calendar-cell-range-hover-border-color: var(--#{$prefix}primary) !default;
23312331

23322332
$calendar-cell-today-color: var(--#{$prefix}$danger) !default;
2333+
2334+
$calendar-cell-week-number-color: var(--#{$prefix}tertiary-color) !default;
23332335
// scss-docs-end calendar-variables
23342336

23352337
// Date Picker

0 commit comments

Comments
 (0)