This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathdate.js
201 lines (175 loc) · 5.28 KB
/
date.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* External dependencies
*/
import moment from 'moment';
import classnames from 'classnames';
// react-dates doesn't tree-shake correctly, so we import from the individual
// component here, to avoid including too much of the library
import DayPickerSingleDateController from 'react-dates/lib/components/DayPickerSingleDateController';
/**
* WordPress dependencies
*/
import { Component, createRef, useEffect, useRef } from '@wordpress/element';
import { isRTL, _n, sprintf } from '@wordpress/i18n';
/**
* Module Constants
*/
const TIMEZONELESS_FORMAT = 'YYYY-MM-DDTHH:mm:ss';
const ARIAL_LABEL_TIME_FORMAT = 'dddd, LL';
function DatePickerDay( { day, events = [] } ) {
const ref = useRef();
/*
* a11y hack to make the `There is/are n events` string
* available speaking for readers,
* re-defining the aria-label attribute.
* This attribute is handled by the react-dates component.
*/
useEffect( () => {
// Bail when no parent node.
if ( ! ref?.current?.parentNode ) {
return;
}
const { parentNode } = ref.current;
const dayAriaLabel = moment( day ).format( ARIAL_LABEL_TIME_FORMAT );
if ( ! events.length ) {
// Set aria-label without event description.
parentNode.setAttribute( 'aria-label', dayAriaLabel );
return;
}
const dayWithEventsDescription = sprintf(
// translators: 1: Calendar day format, 2: Calendar event number.
_n(
'%1$s. There is %2$d event.',
'%1$s. There are %2$d events.',
events.length
),
dayAriaLabel,
events.length
);
parentNode.setAttribute( 'aria-label', dayWithEventsDescription );
}, [ events.length ] );
return (
<div
ref={ ref }
className={ classnames( 'components-datetime__date__day', {
'has-events': events?.length,
} ) }
>
{ day.format( 'D' ) }
</div>
);
}
class DatePicker extends Component {
constructor() {
super( ...arguments );
this.onChangeMoment = this.onChangeMoment.bind( this );
this.nodeRef = createRef();
this.onMonthPreviewedHandler = this.onMonthPreviewedHandler.bind(
this
);
}
onMonthPreviewedHandler( newMonthDate ) {
this.props.onMonthPreviewed?.( newMonthDate.toISOString() );
this.keepFocusInside();
}
/*
* Todo: We should remove this function ASAP.
* It is kept because focus is lost when we click on the previous and next month buttons.
* This focus loss closes the date picker popover.
* Ideally we should add an upstream commit on react-dates to fix this issue.
*/
keepFocusInside() {
if ( ! this.nodeRef.current ) {
return;
}
const { ownerDocument } = this.nodeRef.current;
const { activeElement } = ownerDocument;
// If focus was lost.
if (
! activeElement ||
! this.nodeRef.current.contains( ownerDocument.activeElement )
) {
// Retrieve the focus region div.
const focusRegion = this.nodeRef.current.querySelector(
'.DayPicker_focusRegion'
);
if ( ! focusRegion ) {
return;
}
// Keep the focus on focus region.
focusRegion.focus();
}
}
onChangeMoment( newDate ) {
const { currentDate, onChange } = this.props;
// If currentDate is null, use now as momentTime to designate hours, minutes, seconds.
const momentDate = currentDate ? moment( currentDate ) : moment();
const momentTime = {
hours: momentDate.hours(),
minutes: momentDate.minutes(),
seconds: 0,
};
onChange( newDate.set( momentTime ).format( TIMEZONELESS_FORMAT ) );
// Keep focus on the date picker.
this.keepFocusInside();
}
/**
* Create a Moment object from a date string. With no currentDate supplied, default to a Moment
* object representing now. If a null value is passed, return a null value.
*
* @param {?string} currentDate Date representing the currently selected date or null to signify no selection.
* @return {?moment.Moment} Moment object for selected date or null.
*/
getMomentDate( currentDate ) {
if ( null === currentDate ) {
return null;
}
return currentDate ? moment( currentDate ) : moment();
}
getEventsPerDay( day ) {
if ( ! this.props.events?.length ) {
return [];
}
return this.props.events.filter( ( eventDay ) =>
day.isSame( eventDay.date, 'day' )
);
}
render() {
const { currentDate, isInvalidDate } = this.props;
const momentDate = this.getMomentDate( currentDate );
return (
<div className="components-datetime__date" ref={ this.nodeRef }>
<DayPickerSingleDateController
date={ momentDate }
daySize={ 30 }
focused
hideKeyboardShortcutsPanel
// This is a hack to force the calendar to update on month or year change
// https://github.com/airbnb/react-dates/issues/240#issuecomment-361776665
key={ `datepicker-controller-${
momentDate ? momentDate.format( 'MM-YYYY' ) : 'null'
}` }
noBorder
numberOfMonths={ 1 }
onDateChange={ this.onChangeMoment }
transitionDuration={ 0 }
weekDayFormat="ddd"
dayAriaLabelFormat={ ARIAL_LABEL_TIME_FORMAT }
isRTL={ isRTL() }
isOutsideRange={ ( date ) => {
return isInvalidDate && isInvalidDate( date.toDate() );
} }
onPrevMonthClick={ this.onMonthPreviewedHandler }
onNextMonthClick={ this.onMonthPreviewedHandler }
renderDayContents={ ( day ) => (
<DatePickerDay
day={ day }
events={ this.getEventsPerDay( day ) }
/>
) }
/>
</div>
);
}
}
export default DatePicker;