import React, { useCallback } from 'react';
import { View, Text, Pressable, StyleSheet,Image} from 'react-native';
import { useCalendarContext } from '../CalendarContext';
import dayjs from 'dayjs';
import { CalendarViews } from '../enums';
import type { HeaderProps } from '../types';
import { getDateYear, getYearRange, YEAR_PAGE_SIZE } from '../utils';
import { LEFT_ICON,RIGHT_ICON } from '../constants/imageBase64';
const Header = ({ buttonPrevIcon, buttonNextIcon }: HeaderProps) => {
const {
currentDate,
currentYear,
onChangeMonth,
onChangeYear,
calendarView,
setCalendarView,
theme,
locale,
} = useCalendarContext();
const currentMonthText = dayjs(currentDate).locale(locale).format('MMMM');
const renderPrevButton = (
calendarView === CalendarViews.day
? onChangeMonth(-1)
: calendarView === CalendarViews.month
? onChangeYear(currentYear - 1)
: calendarView === CalendarViews.year &&
onChangeYear(currentYear - YEAR_PAGE_SIZE)
}
testID="btn-prev"
accessibilityRole="button"
accessibilityLabel="Prev"
>
{buttonPrevIcon || (
)}
);
const renderNextButton = (
calendarView === CalendarViews.day
? onChangeMonth(1)
: calendarView === CalendarViews.month
? onChangeYear(currentYear + 1)
: calendarView === CalendarViews.year &&
onChangeYear(currentYear + YEAR_PAGE_SIZE)
}
testID="btn-next"
accessibilityRole="button"
accessibilityLabel="Next"
>
{buttonNextIcon || (
)}
);
const yearSelector = useCallback(() => {
const years = getYearRange(currentYear);
return (
<>
{
setCalendarView(
calendarView === CalendarViews.year
? CalendarViews.day
: CalendarViews.year
);
onChangeYear(getDateYear(currentDate));
}}
testID="btn-year"
accessibilityRole="button"
accessibilityLabel={dayjs(currentDate).format('YYYY')}
>
{calendarView === CalendarViews.year
? `${years.at(0)} - ${years.at(-1)}`
: dayjs(currentDate).format('YYYY')}
>
);
}, [
calendarView,
currentDate,
currentYear,
setCalendarView,
onChangeYear,
theme,
]);
const monthSelector = (
<>
setCalendarView(
calendarView === CalendarViews.month
? CalendarViews.day
: CalendarViews.month
)
}
testID="btn-month"
accessibilityRole="button"
accessibilityLabel={currentMonthText}
>
{currentMonthText}
>
);
const renderSelectors = (
<>
{calendarView !== CalendarViews.year ? monthSelector : null}
{yearSelector()}
>
);
return (
{theme?.headerButtonsPosition === 'left' ? (
{renderPrevButton}
{renderNextButton}
{renderSelectors}
) : theme?.headerButtonsPosition === 'right' ? (
{renderSelectors}
{renderPrevButton}
{renderNextButton}
) : (
{renderPrevButton}
{renderSelectors}
{renderNextButton}
)}
);
};
const styles = StyleSheet.create({
headerContainer: {
marginBottom: 5,
},
container: {
padding: 5,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
},
selectorContainer: {
flexDirection: 'row',
alignItems: 'center',
},
textContainer: {
marginHorizontal: 1,
paddingHorizontal: 5,
paddingVertical: 5,
borderRadius: 5,
},
text: {
fontWeight: 'bold',
fontSize: 15,
},
iconContainer: {
padding: 4,
},
prev: {
marginRight: 3,
},
next: {
marginLeft: 3,
},
row: {
flexDirection: 'row',
},
});
export default Header;