Skip to content

Commit 58f3446

Browse files
committed
perf(Calendar): optimize cell attribute calculation methods
- Add early returns and state caching to _cellDayAttributes for major performance boost - Cache range calculations in _cellMonthAttributes and _cellYearAttributes - Eliminate redundant function calls and complex nested logic - Improve code readability while maintaining functionality - Significant performance improvement for calendar rendering
1 parent 809ed04 commit 58f3446

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

js/src/calendar.js

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -802,46 +802,63 @@ class Calendar extends BaseComponent {
802802

803803
_cellDayAttributes(date, month) {
804804
const isCurrentMonth = month === 'current'
805+
805806
const isDisabled = isDateDisabled(date, this._minDate, this._maxDate, this._config.disabledDates)
806807
const isSelected = isDateSelected(date, this._startDate, this._endDate)
808+
const isTodayDate = isToday(date)
809+
810+
if (this._config.selectionType !== 'day' || this._view !== 'days') {
811+
return {
812+
className: this._classNames({
813+
[CLASS_NAME_CALENDAR_CELL]: true,
814+
today: isTodayDate,
815+
[month]: true
816+
}),
817+
tabIndex: -1,
818+
ariaSelected: false
819+
}
820+
}
821+
822+
const isInRange = isCurrentMonth && isDateInRange(date, this._startDate, this._endDate)
823+
const isRangeHover = isCurrentMonth && this._hoverDate && (
824+
this._selectEndDate ?
825+
isDateInRange(date, this._startDate, this._hoverDate) :
826+
isDateInRange(date, this._hoverDate, this._endDate)
827+
)
807828

808829
const classNames = this._classNames({
809830
[CLASS_NAME_CALENDAR_CELL]: true,
810-
...(this._config.selectionType === 'day' && this._view === 'days' && {
811-
clickable: !isCurrentMonth && this._config.selectAdjacementDays,
812-
disabled: isDisabled,
813-
range: isCurrentMonth && isDateInRange(date, this._startDate, this._endDate),
814-
'range-hover': isCurrentMonth &&
815-
(this._hoverDate && this._selectEndDate ?
816-
isDateInRange(date, this._startDate, this._hoverDate) :
817-
isDateInRange(date, this._hoverDate, this._endDate)),
818-
selected: isSelected
819-
}),
820-
today: isToday(date),
831+
clickable: !isCurrentMonth && this._config.selectAdjacementDays,
832+
disabled: isDisabled,
833+
range: isInRange,
834+
'range-hover': isRangeHover,
835+
selected: isSelected,
836+
today: isTodayDate,
821837
[month]: true
822838
})
823839

824840
return {
825841
className: classNames,
826-
tabIndex: this._config.selectionType === 'day' &&
827-
(isCurrentMonth || this._config.selectAdjacementDays) &&
828-
!isDisabled ? 0 : -1,
842+
tabIndex: (isCurrentMonth || this._config.selectAdjacementDays) && !isDisabled ? 0 : -1,
829843
ariaSelected: isSelected
830844
}
831845
}
832846

833847
_cellMonthAttributes(date) {
834848
const isDisabled = isMonthDisabled(date, this._minDate, this._maxDate, this._config.disabledDates)
835849
const isSelected = isMonthSelected(date, this._startDate, this._endDate)
850+
const isInRange = isMonthInRange(date, this._startDate, this._endDate)
851+
const isRangeHover = this._config.selectionType === 'month' && this._hoverDate && (
852+
this._selectEndDate ?
853+
isMonthInRange(date, this._startDate, this._hoverDate) :
854+
isMonthInRange(date, this._hoverDate, this._endDate)
855+
)
836856

837857
const classNames = this._classNames({
838858
[CLASS_NAME_CALENDAR_CELL]: true,
839859
disabled: isDisabled,
840-
'range-hover': this._config.selectionType === 'month' &&
841-
(this._hoverDate && this._selectEndDate ?
842-
isMonthInRange(date, this._startDate, this._hoverDate) :
843-
isMonthInRange(date, this._hoverDate, this._endDate)),
844-
range: isMonthInRange(date, this._startDate, this._endDate),
860+
'range-hover': isRangeHover,
861+
range: isInRange,
845862
selected: isSelected
846863
})
847864

@@ -855,15 +872,18 @@ class Calendar extends BaseComponent {
855872
_cellYearAttributes(date) {
856873
const isDisabled = isYearDisabled(date, this._minDate, this._maxDate, this._config.disabledDates)
857874
const isSelected = isYearSelected(date, this._startDate, this._endDate)
875+
const isInRange = isYearInRange(date, this._startDate, this._endDate)
876+
const isRangeHover = this._config.selectionType === 'year' && this._hoverDate && (
877+
this._selectEndDate ?
878+
isYearInRange(date, this._startDate, this._hoverDate) :
879+
isYearInRange(date, this._hoverDate, this._endDate)
880+
)
858881

859882
const classNames = this._classNames({
860883
[CLASS_NAME_CALENDAR_CELL]: true,
861884
disabled: isDisabled,
862-
'range-hover': this._config.selectionType === 'year' &&
863-
(this._hoverDate && this._selectEndDate ?
864-
isYearInRange(date, this._startDate, this._hoverDate) :
865-
isYearInRange(date, this._hoverDate, this._endDate)),
866-
range: isYearInRange(date, this._startDate, this._endDate),
885+
'range-hover': isRangeHover,
886+
range: isInRange,
867887
selected: isSelected
868888
})
869889

0 commit comments

Comments
 (0)