Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add active date props to header #544

Merged
merged 7 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export interface CalendarProps<T> {
onPressDateHeader?: (date: Date) => void
onPressEvent?: (event: ICalendarEvent<T>) => void
eventMinHeightForMonthView?: number
activeDate?: Date
}
```

Expand Down Expand Up @@ -148,6 +149,7 @@ export interface CalendarProps<T> {
| `renderEvent` | no | `EventRenderer` | Custom event renderer. See below type definition. |
| `renderHeader` | no | `HeaderRenderer` | Custom header renderer. |
| `eventMinHeightForMonthView` | no | `number` | Minimun height for events in month view. Should match the min-height of your custom events. Defaults to 22. |
| `activeDate` | no | `Date` | Date highlighted in header. Defualts to today (current time). |

## EventRenderer

Expand Down
3 changes: 3 additions & 0 deletions src/components/CalendarContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export interface CalendarContainerProps<T> {
weekEndsOn?: WeekNum
maxVisibleEventCount?: number
eventMinHeightForMonthView?: number
activeDate?: Date
}

function _CalendarContainer<T>({
Expand Down Expand Up @@ -102,6 +103,7 @@ function _CalendarContainer<T>({
weekEndsOn = 6,
maxVisibleEventCount = 3,
eventMinHeightForMonthView = 22,
activeDate,
}: CalendarContainerProps<T>) {
const [targetDate, setTargetDate] = React.useState(dayjs(date))

Expand Down Expand Up @@ -204,6 +206,7 @@ function _CalendarContainer<T>({
style: headerContainerStyle,
allDayEvents: allDayEvents,
onPressDateHeader: onPressDateHeader,
activeDate,
}

return (
Expand Down
13 changes: 8 additions & 5 deletions src/components/CalendarHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { Platform, Text, TouchableOpacity, View, ViewStyle } from 'react-native'
import { eventCellCss, u } from '../commonStyles'
import { ICalendarEvent } from '../interfaces'
import { useTheme } from '../theme/ThemeContext'
import { isToday, typedMemo } from '../utils'
import { isActiveDate, isToday, typedMemo } from '../utils'

export interface CalendarHeaderProps<T> {
dateRange: dayjs.Dayjs[]
cellHeight: number
style: ViewStyle
allDayEvents: ICalendarEvent<T>[]
onPressDateHeader?: (date: Date) => void
activeDate?: Date
}

function _CalendarHeader<T>({
Expand All @@ -21,6 +22,7 @@ function _CalendarHeader<T>({
style,
allDayEvents,
onPressDateHeader,
activeDate,
}: CalendarHeaderProps<T>) {
const _onPress = React.useCallback(
(date: Date) => {
Expand All @@ -46,6 +48,7 @@ function _CalendarHeader<T>({
<View style={[u['z-10'], u['w-50'], borderColor]} />
{dateRange.map((date) => {
const _isToday = isToday(date)
const _isActiveDate = activeDate ? isActiveDate(date, activeDate) : _isToday
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be performance improvement: ​if activeDate is passed, _isToday will be calculated but not used.

So, the logic should be like this:

const shouldHighlight = activeDate ? date.isSame(activeDate, 'date') : isToday(date)return (

Also, please note that

  • The var name shouldHighlight is more clear
  • I think the function isActiveDate is a bit redundant, writing it inline must be simpler
  • isSame(.., 'date') is more clear than isSame(.., 'day') in this case

Sorry that I couldn't find it at the first time ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your review. I'll fix it!

return (
<TouchableOpacity
style={[u['flex-1'], u['pt-2']]}
Expand All @@ -58,14 +61,14 @@ function _CalendarHeader<T>({
style={[
theme.typography.xs,
u['text-center'],
{ color: _isToday ? theme.palette.primary.main : theme.palette.gray['500'] },
{ color: _isActiveDate ? theme.palette.primary.main : theme.palette.gray['500'] },
]}
>
{date.format('ddd')}
</Text>
<View
style={
_isToday
_isActiveDate
? [
primaryBg,
u['h-36'],
Expand All @@ -83,13 +86,13 @@ function _CalendarHeader<T>({
<Text
style={[
{
color: _isToday
color: _isActiveDate
? theme.palette.primary.contrastText
: theme.palette.gray['800'],
},
theme.typography.xl,
u['text-center'],
Platform.OS === 'web' && _isToday && u['mt-6'],
Platform.OS === 'web' && _isActiveDate && u['mt-6'],
]}
>
{date.format('D')}
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ export function isToday(date: dayjs.Dayjs) {
return today.isSame(date, 'day')
}

export function isActiveDate(date: dayjs.Dayjs, activeDate: Date) {
return date.isSame(activeDate, 'day')
}

export function getRelativeTopInDay(date: dayjs.Dayjs) {
return (100 * (date.hour() * 60 + date.minute())) / DAY_MINUTES
}
Expand Down