Skip to content

Conditional rendering of week calendar and calendar list #2647

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
15 changes: 8 additions & 7 deletions example/src/screens/expandableCalendarScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {useRef, useCallback} from 'react';
import {Animated, Easing, StyleSheet, Text, TouchableOpacity} from 'react-native';
import {Animated, Button, Easing, StyleSheet, Text, TouchableOpacity} from 'react-native';
import {ExpandableCalendar, AgendaList, CalendarProvider, WeekCalendar} from 'react-native-calendars';
import testIDs from '../testIDs';
import {agendaItems, getMarkedDates} from '../mocks/agendaItems';
Expand Down Expand Up @@ -32,7 +32,7 @@ const ExpandableCalendarScreen = (props: Props) => {
// }, []);

const renderItem = useCallback(({item}: any) => {
return <AgendaItem item={item}/>;
return <AgendaItem item={item} />;
}, []);

const calendarRef = useRef<{toggleCalendarPosition: () => boolean}>(null);
Expand All @@ -57,7 +57,7 @@ const ExpandableCalendarScreen = (props: Props) => {
return (
<TouchableOpacity style={styles.header} onPress={toggleCalendarExpansion}>
<Text style={styles.headerTitle}>{date?.toString('MMMM yyyy')}</Text>
<Animated.Image source={CHEVRON} style={{transform: [{rotate: '90deg'}, {rotate: rotationInDegrees}]}}/>
<Animated.Image source={CHEVRON} style={{transform: [{rotate: '90deg'}, {rotate: rotationInDegrees}]}} />
</TouchableOpacity>
);
},
Expand All @@ -83,16 +83,16 @@ const ExpandableCalendarScreen = (props: Props) => {
// disableAutoDaySelection={[ExpandableCalendar.navigationTypes.MONTH_SCROLL, ExpandableCalendar.navigationTypes.MONTH_ARROWS]}
>
{weekView ? (
<WeekCalendar testID={testIDs.weekCalendar.CONTAINER} firstDay={1} markedDates={marked.current}/>
<WeekCalendar testID={testIDs.weekCalendar.CONTAINER} firstDay={1} markedDates={marked.current} />
) : (
<ExpandableCalendar
testID={testIDs.expandableCalendar.CONTAINER}
renderHeader={renderHeader}
// renderHeader={renderHeader}
ref={calendarRef}
onCalendarToggled={onCalendarToggled}
// onCalendarToggled={onCalendarToggled}
// horizontal={false}
// hideArrows
// disablePan
disablePan
// hideKnob
// initialPosition={ExpandableCalendar.positions.OPEN}
// calendarStyle={styles.calendar}
Expand All @@ -115,6 +115,7 @@ const ExpandableCalendarScreen = (props: Props) => {
sectionStyle={styles.section}
// dayFormat={'yyyy-MM-d'}
/>
<Button title='Button' onPress={() => toggleCalendarExpansion()} />
</CalendarProvider>
);
};
Expand Down
1 change: 1 addition & 0 deletions src/calendar-list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Calendar, {CalendarProps} from '../calendar';
import CalendarListItem from './item';
import CalendarHeader from '../calendar/header/index';
import isEqual from 'lodash/isEqual';
export {CalendarHeader};

const CALENDAR_WIDTH = constants.screenWidth;
const CALENDAR_HEIGHT = 360;
Expand Down
84 changes: 50 additions & 34 deletions src/expandableCalendar/WeekCalendar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import XDate from 'xdate';
import React, {useCallback, useContext, useMemo, useRef, useState} from 'react';
import {FlatList, View, ViewToken} from 'react-native';
import {sameWeek, onSameDateRange, getWeekDates} from '../../dateutils';
import {toMarkingFormat} from '../../interface';
import {parseDate, toMarkingFormat} from '../../interface';
import {DateData, MarkedDates} from '../../types';
import styleConstructor from '../style';
import {CalendarListProps} from '../../calendar-list';
import {CalendarListProps, CalendarHeader} from '../../calendar-list';
import WeekDaysNames from '../../commons/WeekDaysNames';
import Week from '../week';
import {UpdateSources} from '../commons';
import constants from '../../commons/constants';
import {extractCalendarProps} from '../../componentUpdater';
import {extractCalendarProps, extractHeaderProps} from '../../componentUpdater';
import CalendarContext from '../Context';
import {useDidUpdate} from '../../hooks';

Expand All @@ -20,6 +20,7 @@ const NUM_OF_ITEMS = NUMBER_OF_PAGES * 2 + 1; // NUMBER_OF_PAGES before + NUMBER
export interface WeekCalendarProps extends CalendarListProps {
/** whether to have shadow/elevation for the calendar */
allowShadow?: boolean;
showHeader?: boolean;
}

/**
Expand All @@ -34,9 +35,11 @@ const WeekCalendar = (props: WeekCalendarProps) => {
current,
theme,
testID,
markedDates
markedDates,
showHeader
} = props;
const context = useContext(CalendarContext);
const headerProps = extractHeaderProps(props);
const {allowShadow = true, ...calendarListProps} = props;
const {style: propsStyle, onDayPress, firstDay = 0, ...others} = extractCalendarProps(calendarListProps);
const {date, numberOfDays, updateSource, setDate, timelineLeftInset} = context;
Expand All @@ -47,6 +50,7 @@ const WeekCalendar = (props: WeekCalendarProps) => {
const changedItems = useRef(constants.isRTL);
const list = useRef<FlatList>(null);
const currentIndex = useRef(NUMBER_OF_PAGES);
const currentMonth = parseDate(current);

const shouldFixRTL = useMemo(() => !constants.isRN73() && constants.isAndroidRTL, []);

Expand Down Expand Up @@ -206,38 +210,50 @@ const WeekCalendar = (props: WeekCalendarProps) => {
},
onViewableItemsChanged
}]);

const renderCalendarHeader = () => {
return (
<CalendarHeader
{...headerProps}
testID={`${testID}.staticHeader`}
style={style.current.weekCalendarHeader}
month={currentMonth}
accessibilityElementsHidden={true} // iOS
importantForAccessibility={'no-hide-descendants'} // Android
hideDayNames={true}
/>
);
};

return (
<View
testID={testID}
style={weekCalendarStyle}
>
{!hideDayNames && (
<View style={containerStyle}>
{renderWeekDaysNames}
return (
<>
<View style={{backgroundColor: 'red'}}>
{showHeader && renderCalendarHeader()}
</View>
)}
<View style={style.current.container}>
<FlatList
testID={`${testID}.list`}
ref={list}
style={style.current.container}
data={listData}
horizontal
showsHorizontalScrollIndicator={false}
pagingEnabled
scrollEnabled
renderItem={renderItem}
keyExtractor={keyExtractor}
initialScrollIndex={NUMBER_OF_PAGES}
getItemLayout={getItemLayout}
viewabilityConfigCallbackPairs={viewabilityConfigCallbackPairs.current}
onEndReached={onEndReached}
onEndReachedThreshold={1 / NUM_OF_ITEMS}
/>
</View>
</View>
);
<View testID={testID} style={weekCalendarStyle}>
{!hideDayNames && <View style={containerStyle}>{renderWeekDaysNames}</View>}
<View style={style.current.container}>
<FlatList
testID={`${testID}.list`}
ref={list}
style={style.current.container}
data={listData}
horizontal
showsHorizontalScrollIndicator={false}
pagingEnabled
scrollEnabled
renderItem={renderItem}
keyExtractor={keyExtractor}
initialScrollIndex={NUMBER_OF_PAGES}
getItemLayout={getItemLayout}
viewabilityConfigCallbackPairs={viewabilityConfigCallbackPairs.current}
onEndReached={onEndReached}
onEndReachedThreshold={1 / NUM_OF_ITEMS}
/>
</View>
</View>
</>
);
};

function getDateForDayRange(date: string, weekIndex: number, numberOfDays: number) {
Expand Down
Loading