@@ -14,13 +14,21 @@ import React, {
1414 useState ,
1515} from 'react' ;
1616import classnames from 'classnames' ;
17- import moment from 'moment' ;
17+ import dayjs from 'dayjs' ;
18+ import weekday from 'dayjs/plugin/weekday' ;
19+ import localeData from 'dayjs/plugin/localeData' ;
1820import { Button } from './Button' ;
1921import { Select , Option } from './Select' ;
2022import { getToday , isElInChildren } from './util' ;
2123import { ComponentSettingsContext } from './ComponentSettings' ;
2224import { useEventCallback , useMergeRefs } from './hooks' ;
2325
26+ /**
27+ *
28+ */
29+ dayjs . extend ( weekday ) ;
30+ dayjs . extend ( localeData ) ;
31+
2432/**
2533 *
2634 */
@@ -42,12 +50,12 @@ type Calendar = {
4250function createCalendarObject ( date ?: string , mnDate ?: string , mxDate ?: string ) {
4351 let minDate ;
4452 let maxDate ;
45- let d = moment ( date , 'YYYY-MM-DD' ) ;
53+ let d = dayjs ( date , 'YYYY-MM-DD' ) ;
4654 if ( ! d . isValid ( ) ) {
47- d = moment ( getToday ( ) , 'YYYY-MM-DD' ) ;
55+ d = dayjs ( getToday ( ) , 'YYYY-MM-DD' ) ;
4856 }
4957 if ( mnDate ) {
50- const minD = moment ( mnDate , 'YYYY-MM-DD' ) ;
58+ const minD = dayjs ( mnDate , 'YYYY-MM-DD' ) ;
5159 if ( minD . isValid ( ) ) {
5260 minDate = {
5361 year : minD . year ( ) ,
@@ -58,7 +66,7 @@ function createCalendarObject(date?: string, mnDate?: string, mxDate?: string) {
5866 }
5967 }
6068 if ( mxDate ) {
61- const maxD = moment ( mxDate , 'YYYY-MM-DD' ) ;
69+ const maxD = dayjs ( mxDate , 'YYYY-MM-DD' ) ;
6270 if ( maxD . isValid ( ) ) {
6371 maxDate = {
6472 year : maxD . year ( ) ,
@@ -70,8 +78,8 @@ function createCalendarObject(date?: string, mnDate?: string, mxDate?: string) {
7078 }
7179 const year = d . year ( ) ;
7280 const month = d . month ( ) ;
73- const first = moment ( d ) . startOf ( 'month' ) . startOf ( 'week' ) ;
74- const last = moment ( d ) . endOf ( 'month' ) . endOf ( 'week' ) ;
81+ const first = dayjs ( d ) . startOf ( 'month' ) . startOf ( 'week' ) ;
82+ const last = dayjs ( d ) . endOf ( 'month' ) . endOf ( 'week' ) ;
7583 const weeks = [ ] ;
7684 let days = [ ] ;
7785 for ( let dd = first ; dd . isBefore ( last ) ; dd = dd . add ( 1 , 'd' ) ) {
@@ -144,7 +152,7 @@ const DatepickerFilter: FC<DatepickerFilterProps> = (props) => {
144152 onClick = { onPrevMonth }
145153 />
146154 </ div >
147- < h2 className = 'slds-align-middle' > { moment . monthsShort ( ) [ cal . month ] } </ h2 >
155+ < h2 className = 'slds-align-middle' > { dayjs . monthsShort ( ) [ cal . month ] } </ h2 >
148156 < div className = 'slds-align-middle' >
149157 < Button
150158 className = 'slds-align-middle'
@@ -218,15 +226,15 @@ const DatepickerDate: FC<DatepickerDateProps> = (props) => {
218226 let selectable = true ;
219227 let enabled = date . year === cal . year && date . month === cal . month ;
220228 if ( cal . minDate ) {
221- const min = moment ( date . value , 'YYYY-MM-DD' ) . isAfter (
222- moment ( cal . minDate . value , 'YYYY-MM-DD' )
229+ const min = dayjs ( date . value , 'YYYY-MM-DD' ) . isAfter (
230+ dayjs ( cal . minDate . value , 'YYYY-MM-DD' )
223231 ) ;
224232 selectable = selectable && min ;
225233 enabled = enabled && min ;
226234 }
227235 if ( cal . maxDate ) {
228- const max = moment ( date . value , 'YYYY-MM-DD' ) . isBefore (
229- moment ( cal . maxDate . value , 'YYYY-MM-DD' )
236+ const max = dayjs ( date . value , 'YYYY-MM-DD' ) . isBefore (
237+ dayjs ( cal . maxDate . value , 'YYYY-MM-DD' )
230238 ) ;
231239 selectable = selectable && max ;
232240 enabled = enabled && max ;
@@ -241,7 +249,7 @@ const DatepickerDate: FC<DatepickerDateProps> = (props) => {
241249 return (
242250 < td
243251 className = { dateClassName }
244- headers = { moment . weekdays ( dayIndex ) }
252+ headers = { dayjs ( ) . weekday ( dayIndex ) . format ( 'ddd' ) }
245253 role = 'gridcell'
246254 aria-disabled = { ! enabled }
247255 aria-selected = { selected }
@@ -292,10 +300,10 @@ const DatepickerMonth = forwardRef(
292300 >
293301 < thead >
294302 < tr >
295- { moment . weekdaysMin ( true ) . map ( ( wd , i ) => (
303+ { dayjs . weekdaysMin ( true ) . map ( ( wd , i ) => (
296304 // eslint-disable-next-line react/no-array-index-key
297305 < th key = { i } >
298- < abbr title = { moment . weekdays ( true , i ) } > { wd } </ abbr >
306+ < abbr title = { dayjs ( ) . weekday ( i ) . format ( 'ddd' ) } > { wd } </ abbr >
299307 </ th >
300308 ) ) }
301309 </ tr >
@@ -406,20 +414,20 @@ export const Datepicker: FC<DatepickerProps> = (props) => {
406414 e . stopPropagation ( ) ;
407415 } else if ( e . keyCode >= 37 && e . keyCode <= 40 ) {
408416 // cursor key
409- let m ;
417+ let d ;
410418 if ( e . keyCode === 37 ) {
411- m = moment ( targetDate ) . add ( - 1 , e . shiftKey ? 'months' : 'days' ) ;
419+ d = dayjs ( targetDate ) . add ( - 1 , e . shiftKey ? 'months' : 'days' ) ;
412420 } else if ( e . keyCode === 39 ) {
413421 // right arrow key
414- m = moment ( targetDate ) . add ( 1 , e . shiftKey ? 'months' : 'days' ) ;
422+ d = dayjs ( targetDate ) . add ( 1 , e . shiftKey ? 'months' : 'days' ) ;
415423 } else if ( e . keyCode === 38 ) {
416424 // up arrow key
417- m = moment ( targetDate ) . add ( - 1 , e . shiftKey ? 'years' : 'weeks' ) ;
425+ d = dayjs ( targetDate ) . add ( - 1 , e . shiftKey ? 'years' : 'weeks' ) ;
418426 } else if ( e . keyCode === 40 ) {
419427 // down arrow key
420- m = moment ( targetDate ) . add ( 1 , e . shiftKey ? 'years' : 'weeks' ) ;
428+ d = dayjs ( targetDate ) . add ( 1 , e . shiftKey ? 'years' : 'weeks' ) ;
421429 }
422- const newTargetDate = m ?. format ( 'YYYY-MM-DD' ) ?? targetDate ;
430+ const newTargetDate = d ?. format ( 'YYYY-MM-DD' ) ?? targetDate ;
423431 setTargetDate ( newTargetDate ) ;
424432 setFocusDate ( true ) ;
425433 e . preventDefault ( ) ;
@@ -438,15 +446,15 @@ export const Datepicker: FC<DatepickerProps> = (props) => {
438446
439447 const onYearChange = useEventCallback (
440448 ( e : React . ChangeEvent < HTMLSelectElement > ) => {
441- const newTargetDate = moment ( targetDate )
449+ const newTargetDate = dayjs ( targetDate )
442450 . year ( Number ( e . target . value ) )
443451 . format ( 'YYYY-MM-DD' ) ;
444452 setTargetDate ( newTargetDate ) ;
445453 }
446454 ) ;
447455
448456 const onMonthChange = useEventCallback ( ( month : number ) => {
449- const newTargetDate = moment ( targetDate )
457+ const newTargetDate = dayjs ( targetDate )
450458 . add ( month , 'months' )
451459 . format ( 'YYYY-MM-DD' ) ;
452460 setTargetDate ( newTargetDate ) ;
0 commit comments