Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 700x 700x 700x 700x 1x 699x 12x 687x 700x 630x 700x 700x 699x 1x 1x 1x 700x 700x 700x 700x 700x 700x 700x | import React, {Component} from 'react'
import PropTypes from 'prop-types'
import {omit} from 'lodash'
import configMapping from '../Utilities/configMapping'
/**
* A className depending on whether the date is in the focused month or not.
* @private
* @param {DateTime} date - the date in question as a Luxon DateTime
* @param {string} currentMonth the current month as a string formatted yyyy-MM
* @returns {string} the className
*/
function calculateMonthClass(date, currentMonth) {
const modifier =
date.toFormat('yyyy-MM') === currentMonth ? 'thisMonth' : 'otherMonth'
return `rev-Calendar-body-bodyCell--${modifier}`
}
/**
* A className depending on whether the date is selectable or not.
* @private
* @param {Function} isSelectable - a function which returns true id the date is
* selectable
* @param {DateTime} date - a Luxon DateTime for the date in question
* @param {string} selectedDate - an iso string of the selected date
* @return {string} - a class to use for styling
*/
/* eslint complexity: [2, 4] */
function calculateSelectionClass(isSelectable, date, selectedDate) {
const selectable = isSelectable(date)
if (!selectable) {
return 'rev-Calendar-body-bodyCell--unselectable'
} else if (selectedDate && date.toISODate() === selectedDate) {
return 'rev-Calendar-body-bodyCell--selected'
}
return ''
}
/**
* A className for a date depending on whether or not it is 'highlighted.'
* @private
* @param {DateTime} date - the date in question as a Luxon DateTime
* @param {object|Array|Function} highlights a mapping between dates and
* highlight classes. If an array, gives a standard --highlighted modifier to
* a found date cell.
* @return {string} - a class to use for styling
*/
function calculateHighlightClass(date, highlights) {
return (
configMapping(
highlights || {},
date,
(dateArg) => dateArg.toISODate(),
'rev-Calendar-body-bodyCell--highlighted'
) || ''
)
}
/**
* Handles clicks onto the cell. If the cell is selectable, invoke the
* dateChanger that was passed in. If not, do nothing.
* @private
* @param {Function} isSelectable returns true if the date is selectable
* @param {DateTime} date the date in question
* @param {Function} dateChanger the handler to invoke if the cell is selectable
* @return {(Function| null)} - if the cell is selectable, invoke the
* dateChanger that was passed in. If not, do nothing
*/
function dayClickHandler(isSelectable, date, dateChanger) {
const selectable = isSelectable(date)
if (selectable) {
return (event) => {
event.preventDefault()
dateChanger(date.toISODate())
}
}
return null
}
/**
* A single day of the calendar. It is a button which takes up the entire table
* cell. It handles click events, and date dependent formatting (e.g. selected
* date format, unselectable date format, highlighted date format, etc).
* @param {object} props the props of the day component
*/
class CalendarDay extends Component {
static propTypes = {
currentMonth: PropTypes.string,
date: PropTypes.object,
dateChanger: PropTypes.func,
highlights: PropTypes.oneOfType([
PropTypes.array,
PropTypes.func,
PropTypes.object,
]),
isSelectable: PropTypes.func,
selectedDate: PropTypes.string,
children: PropTypes.node,
}
render() {
const {
currentMonth,
date,
dateChanger,
highlights,
isSelectable,
selectedDate,
...props
} = this.props
const monthClass = calculateMonthClass(date, currentMonth)
const selectionClass = calculateSelectionClass(
isSelectable,
date,
selectedDate
)
const highlightClass = calculateHighlightClass(date, highlights)
const selectable = isSelectable(date)
const buttonProps = omit(props, 'overrides')
return (
<td
className={`rev-Calendar-body-bodyCell ${monthClass} ${selectionClass} ${highlightClass}`}
>
<button
{...buttonProps}
onClick={dayClickHandler(isSelectable, date, dateChanger)}
aria-label={date.toLocaleString({
year: 'numeric',
month: 'long',
day: 'numeric',
})}
disabled={!selectable}
>
{date.toLocaleString({day: 'numeric'})}
</button>
</td>
)
}
}
export default CalendarDay
|