8000 feat(infiniteAgendaList): add the ability to set item height per item… · wix/react-native-calendars@7bc7398 · GitHub
[go: up one dir, main page]

Skip to content

Commit

Permalink
feat(infiniteAgendaList): add the ability to set item height per item (
Browse files Browse the repository at this point in the history
…#2515)

* feat(infiniteAgendaList): add the ability to set item height per item

* Update infiniteAgendaList.tsx

* add example

* Fix PR comment

* Rename itemType to itemCustomHeightType
  • Loading branch information
yakirza17 authored Sep 11, 2024
1 parent 6b091db commit 7bc7398
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 9 deletions.
12 changes: 6 additions & 6 deletions example/src/mocks/agendaItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function getPastDate(numberOfDays: number) {
export const agendaItems = [
{
title: dates[0],
data: [{hour: '12am', duration: '1h', title: 'First Yoga'}]
data: [{hour: '12am', duration: '1h', title: 'First Yoga'}, {hour: '9am', duration: '1h', title: 'Long Yoga', itemCustomHeightType: 'LongEvent'}],
},
{
title: dates[1],
Expand Down Expand Up @@ -63,13 +63,13 @@ export const agendaItems = [
]
},
{
title: dates[6],
title: dates[6],
data: [
{hour: '12am', duration: '1h', title: 'Ashtanga Yoga'}
]
},
{
title: dates[7],
title: dates[7],
data: [{}]
},
{
Expand All @@ -90,7 +90,7 @@ export const agendaItems = [
]
},
{
title: dates[10],
title: dates[10],
data: [
{hour: '12am', duration: '1h', title: 'Last Yoga'}
]
Expand All @@ -104,13 +104,13 @@ export const agendaItems = [
]
},
{
title: dates[12],
title: dates[12],
data: [
{hour: '12am', duration: '1h', title: 'Last Yoga'}
]
},
{
title: dates[13],
title: dates[13],
data: [
{hour: '12am', duration: '1h', title: 'Last Yoga'}
]
Expand Down
81 changes: 81 additions & 0 deletions example/src/screens/agendaInfiniteListScreen.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import React, {useRef, useCallback} from 'react';
import {StyleSheet, View} from 'react-native';
import {ExpandableCalendar, AgendaList, CalendarProvider, WeekCalendar} from 'react-native-calendars';
import testIDs from '../testIDs';
import {agendaItems, getMarkedDates} from '../mocks/agendaItems';
import AgendaItem from '../mocks/AgendaItem';
import {getTheme, themeColor, lightThemeColor} from '../mocks/theme';

const leftArrowIcon = require('../img/previous.png');
const rightArrowIcon = require('../img/next.png');
const ITEMS: any[] = agendaItems;

interface Props {
weekView?: boolean;
}

const AgendaInfiniteListScreen = (props: Props) => {
const {weekView} = props;
const marked = useRef(getMarkedDates());
const theme = useRef(getTheme());
const todayBtnTheme = useRef({
todayButtonTextColor: themeColor
});

const renderItem = useCallback(({item}: any) => {
const isLongItem = item.itemCustomHeightType === 'LongEvent';
return <View style={{paddingTop: isLongItem ? 40 : 0}}><AgendaItem item={item}/></View>;
}, []);

return (
<CalendarProvider
date={ITEMS[1]?.title}
showTodayButton
theme={todayBtnTheme.current}
>
{weekView ? (
<WeekCalendar testID={testIDs.weekCalendar.CONTAINER} firstDay={1} markedDates={marked.current}/>
) : (
<ExpandableCalendar
testID={testIDs.expandableCalendar.CONTAINER}
theme={theme.current}
firstDay={1}
markedDates={marked.current}
leftArrowImageSource={leftArrowIcon}
rightArrowImageSource={rightArrowIcon}
/>
)}
<AgendaList
sections={ITEMS}
renderItem={renderItem}
sectionStyle={styles.section}
infiniteListProps={
{
itemHeight: 80,
titleHeight: 50,
itemHeightByType: {
LongEvent: 120,
}
}
}
/>
</CalendarProvider>
);
};

export default AgendaInfiniteListScreen;

const styles = StyleSheet.create({
calendar: {
paddingLeft: 20,
paddingRight: 20
},
header: {
backgroundColor: 'lightgrey'
},
section: {
backgroundColor: lightThemeColor,
color: 'grey',
textTransform: 'capitalize'
}
});
2 changes: 2 additions & 0 deletions example/src/screens/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import MenuScreen from './menuScreen';
import CalendarScreen from './calendarScreen';
import CalendarPlaygroundScreen from './calendarPlaygroundScreen';
import AgendaScreen from './agendaScreen';
import AgendaInfiniteListScreen from './agendaInfiniteListScreen';
import CalendarsListScreen from './calendarListScreen';
import NewCalendarsListScreen from './newCalendarListScreen';
import ExpandableCalendarScreen from './expandableCalendarScreen';
Expand All @@ -16,6 +17,7 @@ export function registerScreens() {
Navigation.registerComponent('CalendarScreen', () => CalendarScreen);
Navigation.registerComponent('CalendarPlaygroundScreen', () => CalendarPlaygroundScreen);
Navigation.registerComponent('AgendaScreen', () => AgendaScreen);
Navigation.registerComponent('AgendaInfiniteListScreen', () => AgendaInfiniteListScreen);
Navigation.registerComponent('CalendarListScreen', () => CalendarsListScreen);
Navigation.registerComponent('NewCalendarListScreen', () => NewCalendarsListScreen);
Navigation.registerComponent('ExpandableCalendarScreen', () => ExpandableCalendarScreen);
Expand Down
3 changes: 2 additions & 1 deletion example/src/screens/menuScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface Props {
const MenuScreen = (props: Props) => {
const {componentId} = props;
const [forceRTL, setForceRTL] = useState(false);

const toggleRTL = (value) => {
I18nManager.forceRTL(value);
setForceRTL(value);
Expand Down Expand Up @@ -67,6 +67,7 @@ const MenuScreen = (props: Props) => {
{renderEntry(testIDs.menu.HORIZONTAL_LIST, 'Horizontal Calendar List', 'CalendarListScreen', {horizontalView: true})}
{renderEntry(testIDs.menu.HORIZONTAL_LIST, 'NEW Calendar List', 'NewCalendarListScreen')}
{renderEntry(testIDs.menu.AGENDA, 'Agenda', 'AgendaScreen')}
{renderEntry(testIDs.menu.AGENDA_INFINITE, 'Agenda Infinite List', 'AgendaInfiniteListScreen')}
{renderEntry(testIDs.menu.EXPANDABLE_CALENDAR, 'Expandable Calendar', 'ExpandableCalendarScreen')}
{renderEntry(testIDs.menu.TIMELINE_CALENDAR, 'Timeline Calendar', 'TimelineCalendarScreen')}
{renderEntry(testIDs.menu.WEEK_CALENDAR, 'Week Calendar', 'ExpandableCalendarScreen', {weekView: true})}
Expand Down
1 change: 1 addition & 0 deletions example/src/testIDs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default {
CALENDAR_LIST: 'calendar_list_btn',
HORIZONTAL_LIST: 'horizontal_list_btn',
AGENDA: 'agenda_btn',
AGENDA_INFINITE: 'agenda_infinite_btn',
EXPANDABLE_CALENDAR: 'expandable_calendar_btn',
WEEK_CALENDAR: 'week_calendar_btn',
TIMELINE_CALENDAR: 'timeline_calendar_btn',
Expand Down
2 changes: 2 additions & 0 deletions src/expandableCalendar/AgendaListsCommon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export interface AgendaListProps extends SectionListProps<any, DefaultSectionT>
infiniteListProps?: {
itemHeight?: number;
titleHeight?: number;
/** item height by type, require passing itemCustomHeightType in the elements you want to set custom height */
itemHeightByType?: Record<string, number>;
visibleIndicesChangedDebounce?: number;
renderFooter?: () => React.ReactElement | null;
};
Expand Down
13 changes: 11 additions & 2 deletions src/expandableCalendar/infiniteAgendaList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,19 @@ const InfiniteAgendaList = ({

const layoutProvider = useMemo(
() => new LayoutProvider(
(index) => dataRef.current[index]?.isTitle ? 'title': 'page',
(index) => dataRef.current[index]?.isTitle ? 'title': dataRef.current[index]?.itemCustomHeightType ?? 'page',
(type, dim) => {
dim.width = constants.screenWidth;
dim.height = type === 'title' ? infiniteListProps?.titleHeight ?? 60 : infiniteListProps?.itemHeight ?? 80;
switch (type) {
case 'title':
dim.height = infiniteListProps?.titleHeight ?? 60;
break;
case 'page':
dim.height = infiniteListProps?.itemHeight ?? 80;
break;
default:
dim.height = infiniteListProps?.itemHeightByType?.[type] ?? infiniteListProps?.itemHeight ?? 80;
}
}
),
[]
Expand Down

0 comments on commit 7bc7398

Please sign in to comment.
0