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

Timeline calendar num of days issues #2080

Merged
merged 12 commits into from
Nov 28, 2022
Merged
15 changes: 14 additions & 1 deletion src/dateutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,20 @@ export function sameDate(a?: XDate, b?: XDate) {
return false;
} else {
return a?.getFullYear() === b?.getFullYear() && a?.getMonth() === b?.getMonth() && a?.getDate() === b?.getDate();
}
}
}

export function sameWeekRange(a: string, b: string, numberOfDays: number, firstDayOfWeek: string){
const aDate = new XDate(a);
const bDate = new XDate(b);
const firstDayDate = new XDate(firstDayOfWeek);
const aDiff = aDate.getTime() - firstDayDate.getTime();
const bDiff = bDate.getTime() - firstDayDate.getTime();
const aTotalDays = Math.ceil(aDiff / (1000 * 3600 * 24));
const bTotalDays = Math.ceil(bDiff / (1000 * 3600 * 24));
const aWeek = Math.floor(aTotalDays / numberOfDays);
const bWeek = Math.floor(bTotalDays / numberOfDays);
return aWeek === bWeek;
}

export function sameWeek(a: string, b: string, firstDayOfWeek: number) {
Expand Down
4 changes: 2 additions & 2 deletions src/day-state-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ const {toMarkingFormat} = require('./interface');


export function getState(day: XDate, current: XDate, props: any) {
const {minDate, maxDate, disabledByDefault, context} = props;
const {minDate, maxDate, disabledByDefault, context, disableDaySelection} = props;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is disableDaySelection part of the 'props' if it's not a prop? It's confusing, why not add an option object or another pharam?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i agree, moved to be a different parameter

let state = '';

if ((context?.date ?? toMarkingFormat(current)) === toMarkingFormat(day)) {
if (!disableDaySelection && ((context?.date ?? toMarkingFormat(current)) === toMarkingFormat(day))) {
state = 'selected';
} else if (isToday(day)) {
state = 'today';
Expand Down
14 changes: 10 additions & 4 deletions src/expandableCalendar/WeekCalendar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import XDate from 'xdate';
import React, {useCallback, useContext, useMemo, useRef, useState} from 'react';
import {FlatList, View, ViewToken} from 'react-native';

import {sameWeek} from '../../dateutils';
import {sameWeek, sameWeekRange} from '../../dateutils';
import {toMarkingFormat} from '../../interface';
import {DateData} from '../../types';
import styleConstructor from '../style';
Expand All @@ -23,6 +23,7 @@ const APPLY_ANDROID_FIX = constants.isAndroid && constants.isRTL;
export interface WeekCalendarProps extends CalendarListProps {
/** whether to have shadow/elevation for the calendar */
allowShadow?: boolean;
disableDaySelection?: boolean;
yuvalsho marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -36,7 +37,8 @@ const WeekCalendar = (props: WeekCalendarProps) => {
hideDayNames,
current,
theme,
testID
testID,
disableDaySelection,
} = props;
const context = useContext(CalendarContext);
const {allowShadow = true, ...calendarListProps} = props;
Expand All @@ -52,7 +54,10 @@ const WeekCalendar = (props: WeekCalendarProps) => {

useDidUpdate(() => {
if (updateSource !== UpdateSources.WEEK_SCROLL) {
const pageIndex = items.current.findIndex(item => sameWeek(item, date, firstDay));
const pageIndex = items.current.findIndex(
item => (numberOfDays && numberOfDays > 1) ?
Copy link
Collaborator

Choose a reason for hiding this comment

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

Better move this condition (numberOfDays && numberOfDays > 1) to a const since you're using the same one later in line 250

Copy link
Contributor Author

Choose a reason for hiding this comment

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

extracted to a function (it's used outside of the component so it cant be a const/memoized)

sameWeekRange(item, date, numberOfDays, item) :
yuvalsho marked this conversation as resolved.
Show resolved Hide resolved
sameWeek(item, date, firstDay));
if (pageIndex !== currentIndex.current) {
if (pageIndex >= 0) {
visibleWeek.current = items.current[pageIndex];
Expand Down Expand Up @@ -95,9 +100,10 @@ const WeekCalendar = (props: WeekCalendarProps) => {
onDayPress={_onDayPress}
numberOfDays={numberOfDays}
timelineLeftInset={timelineLeftInset}
disableDaySelection={disableDaySelection}
/>
);
},[firstDay, _onDayPress, context, date]);
},[firstDay, _onDayPress, context, date, disableDaySelection]);

const keyExtractor = useCallback((item) => item, []);

Expand Down
3 changes: 2 additions & 1 deletion src/expandableCalendar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
paddingRight: isNumber(rightPaddings) ? rightPaddings + 6 : DAY_NAMES_PADDING
}
];
}, [calendarStyle, numberOfDays]);
Inbal-Tish marked this conversation as resolved.
Show resolved Hide resolved
}, [calendarStyle]);

const animatedHeaderStyle = useMemo(() => {
return [style.current.header, {height: HEADER_HEIGHT + 10, top: headerDeltaY.current}];
Expand Down Expand Up @@ -539,6 +539,7 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
onDayPress={_onDayPress}
accessibilityElementsHidden // iOS
importantForAccessibility={'no-hide-descendants'} // Android
disableDaySelection={!!numberOfDays && numberOfDays > 1}
yuvalsho marked this conversation as resolved.
Show resolved Hide resolved
/>
</Animated.View>
);
Expand Down
1 change: 1 addition & 0 deletions src/expandableCalendar/week.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {CalendarContextProps} from './Context';

export type WeekProps = CalendarProps & {
context?: CalendarContextProps;
disableDaySelection?: boolean;
};

function arePropsEqual(prevProps: WeekProps, nextProps: WeekProps) {
Expand Down