Skip to content

Commit 038a490

Browse files
committed
refactor(Calendar): add new options, improve syntax
1 parent 3b2a3b0 commit 038a490

File tree

3 files changed

+43
-29
lines changed

3 files changed

+43
-29
lines changed

js/src/calendar.js

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
/* eslint-disable multiline-ternary */
2-
/* eslint-disable no-mixed-operators */
3-
/* eslint-disable indent */
1+
/* eslint-disable indent, multiline-ternary */
42
/**
53
* --------------------------------------------------------------------------
6-
* CoreUI (v4.1.0): calendar.js
7-
* Licensed under MIT (https://coreui.io/license)
4+
* CoreUI PRO (v4.2.0-beta.2): calendar.js
5+
* License (https://coreui.io/pro/license-new/)
86
* --------------------------------------------------------------------------
97
*/
108

@@ -16,7 +14,7 @@ import {
1614
getMonthDetails,
1715
getMonthsNames,
1816
getYears,
19-
isDisabled,
17+
isDateDisabled,
2018
isDateInRange,
2119
isDateSelected,
2220
isLastDayOfMonth,
@@ -54,13 +52,13 @@ const Default = {
5452
disabledDates: null,
5553
endDate: null,
5654
firstDayOfWeek: 1,
57-
locale: navigator.language,
55+
locale: 'default',
5856
maxDate: null,
5957
minDate: null,
6058
range: true,
6159
startDate: null,
6260
selectEndDate: false,
63-
weekdayLength: 2
61+
weekdayFormat: 2
6462
}
6563

6664
const DefaultType = {
@@ -74,7 +72,7 @@ const DefaultType = {
7472
range: 'boolean',
7573
startDate: '(date|string|null)',
7674
selectEndDate: 'boolean',
77-
weekdayLength: 'number'
75+
weekdayFormat: '(number|string)'
7876
}
7977

8078
/**
@@ -158,7 +156,7 @@ class Calendar extends BaseComponent {
158156

159157
EventHandler.on(this._element, 'click', '.btn-double-prev', event => {
160158
event.preventDefault()
161-
this._modifyCalendarDate(-1)
159+
this._modifyCalendarDate(this._view === 'years' ? -10 : -1)
162160
})
163161

164162
EventHandler.on(this._element, 'click', '.btn-next', event => {
@@ -168,7 +166,7 @@ class Calendar extends BaseComponent {
168166

169167
EventHandler.on(this._element, 'click', '.btn-double-next', event => {
170168
event.preventDefault()
171-
this._modifyCalendarDate(1)
169+
this._modifyCalendarDate(this._view === 'years' ? 10 : 1)
172170
})
173171

174172
EventHandler.on(this._element, 'click', '.btn-month', event => {
@@ -209,9 +207,11 @@ class Calendar extends BaseComponent {
209207

210208
this._calendarDate = d
211209

212-
EventHandler.trigger(this._element, EVENT_CALENDAR_DATE_CHANGE, {
213-
date: d
214-
})
210+
if (this._view === 'days') {
211+
EventHandler.trigger(this._element, EVENT_CALENDAR_DATE_CHANGE, {
212+
date: d
213+
})
214+
}
215215

216216
this._updateCalendar()
217217
}
@@ -233,7 +233,7 @@ class Calendar extends BaseComponent {
233233
}
234234

235235
_selectDate(date) {
236-
if (isDisabled(date, this._config.minDate, this._config.maxDate, this._config.disabledDates)) {
236+
if (isDateDisabled(date, this._config.minDate, this._config.maxDate, this._config.disabledDates)) {
237237
return
238238
}
239239

@@ -254,7 +254,7 @@ class Calendar extends BaseComponent {
254254
}
255255

256256
_createCalendar() {
257-
const { firstDayOfWeek, locale, weekdayLength } = this._config
257+
const { firstDayOfWeek, locale, weekdayFormat } = this._config
258258
const year = this._calendarDate.getFullYear()
259259
const month = this._calendarDate.getMonth()
260260

@@ -266,9 +266,9 @@ class Calendar extends BaseComponent {
266266
<button class="btn btn-transparent btn-sm btn-double-prev">
267267
<span class="calendar-nav-icon calendar-nav-icon-double-prev"></span>
268268
</button>
269-
<button class="btn btn-transparent btn-sm btn-prev">
269+
${this._view === 'days' ? `<button class="btn btn-transparent btn-sm btn-prev">
270270
<span class="calendar-nav-icon calendar-nav-icon-prev"></span>
271-
</button>
271+
</button>` : ''}
272272
</div>
273273
<div class="calendar-nav-date">
274274
<button class="btn btn-transparent btn-sm btn-month">
@@ -279,9 +279,9 @@ class Calendar extends BaseComponent {
279279
</button>
280280
</div>
281281
<div class="calendar-nav-next">
282-
<button class="btn btn-transparent btn-sm btn-next">
282+
${this._view === 'days' ? `<button class="btn btn-transparent btn-sm btn-next">
283283
<span class="calendar-nav-icon calendar-nav-icon-next"></span>
284-
</button>
284+
</button>` : ''}
285285
<button class="btn btn-transparent btn-sm btn-double-next">
286286
<span class="calendar-nav-icon calendar-nav-icon-double-next"></span>
287287
</button>
@@ -301,7 +301,11 @@ class Calendar extends BaseComponent {
301301
${weekDays.map(({ date }) => (
302302
`<th class="calendar-cell">
303303
<div class="calendar-header-cell-inner">
304-
${date.toLocaleDateString(locale, { weekday: 'long' }).slice(0, weekdayLength)}
304+
${typeof weekdayFormat === 'string' ?
305+
date.toLocaleDateString(locale, { weekday: weekdayFormat }) :
306+
date
307+
.toLocaleDateString(locale, { weekday: 'long' })
308+
.slice(0, weekdayFormat)}
305309
</div>
306310
</th>`
307311
)).join('')}
@@ -311,17 +315,16 @@ class Calendar extends BaseComponent {
311315
${this._view === 'days' ? monthDetails.map(week => (
312316
`<tr>${week.map(({ date, month }) => (
313317
`<td class="calendar-cell ${this._dayClassNames(date, month)}">
314-
<div
315-
class="calendar-cell-inner day"
316-
data-coreui-date="${date}"
317-
>${date.toLocaleDateString(locale, { day: 'numeric' })}</div>
318+
<div class="calendar-cell-inner day" data-coreui-date="${date}">
319+
${date.toLocaleDateString(locale, { day: 'numeric' })}
320+
</div>
318321
</td>`
319322
)).join('')}</tr>`
320323
)).join('') : ''}
321324
${this._view === 'months' ? listOfMonths.map((row, index) => (
322325
`<tr>${row.map((month, idx) => (
323326
`<td class="calendar-cell">
324-
<div class="calendar-cell-inner month" data-coreui-month="${index * 3 + idx}">
327+
<div class="calendar-cell-inner month" data-coreui-month="${(index * 3) + idx}">
325328
${month}
326329
</div>
327330
</td>`
@@ -351,7 +354,7 @@ class Calendar extends BaseComponent {
351354
_dayClassNames(date, month) {
352355
const classNames = [
353356
isToday(date) && 'today',
354-
isDisabled(date, this._config.minDate, this._config.maxDate, this._config.disabledDates) && 'disabled',
357+
isDateDisabled(date, this._config.minDate, this._config.maxDate, this._config.disabledDates) && 'disabled',
355358
`${month}`,
356359
isLastDayOfMonth(date) && 'last',
357360
month === 'current' && isDateInRange(date, this._startDate, this._endDate) && 'range',

js/src/util/calendar.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ export const convertToLocalDate = (d, locale, options = {}) => d.toLocaleDateStr
22
export const convertToLocalTime = (d, locale, options = {}) => d.toLocaleTimeString(locale, options)
33
export const createGroupsInArray = (arr, numberOfGroups) => {
44
const perGroup = Math.ceil(arr.length / numberOfGroups)
5-
return Array.from({ length: numberOfGroups })
5+
// eslint-disable-next-line unicorn/no-new-array
6+
return new Array(numberOfGroups)
67
.fill('')
78
.map((_, i) => arr.slice(i * perGroup, (i + 1) * perGroup))
89
}
@@ -102,7 +103,7 @@ export const getMonthDetails = (year, month, firstDayOfWeek) => {
102103
return weeks
103104
}
104105

105-
export const isDisabled = (date, min, max, dates) => {
106+
export const isDateDisabled = (date, min, max, dates) => {
106107
let disabled
107108
if (dates) {
108109
dates.forEach(_date => {
@@ -162,3 +163,8 @@ export const isToday = date => {
162163
date.getMonth() === today.getMonth() &&
163164
date.getFullYear() === today.getFullYear())
164165
}
166+
167+
export const isValidDate = date => {
168+
const d = new Date(date)
169+
return d instanceof Date && d.getTime()
170+
}

scss/_calendar.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
width: var(--#{$variable-prefix}calendar-table-cell-size);
2727
}
2828
}
29+
30+
&.months ~ .time-picker,
31+
&.years ~ .time-picker {
32+
display: none;
33+
}
2934
}
3035

3136
.calendar-nav {

0 commit comments

Comments
 (0)