| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- import {
- useImperativeHandle,
- useLayoutEffect,
- forwardRef,
- useEffect,
- useState,
- type Ref,
- useRef
- } from "react";
- import {
- TouchableOpacity,
- type ViewStyle,
- View
- } from "react-native";
- import type {
- IMonthSelectorRef,
- CalendarMonth
- } from "./type";
- import type IMonthSelectorProps from "./type";
- import stylesheet, {
- useStyles
- } from "./stylesheet";
- import {
- dateSelect
- } from "./util";
- import {
- NCoreUIKitLocalize,
- NCoreUIKitTheme,
- NCoreUIKitToast
- } from "../../core/hooks";
- import {
- type Mutable,
- webStyle
- } from "../../types";
- import {
- ChevronRight as ChevronRightIcon,
- ChevronLeft as ChevronLeftIcon
- } from "lucide-react-native";
- import moment from "moment";
- import Text from "../text";
- const MONTH_LINES = [
- [
- 0,
- 1,
- 2
- ],
- [
- 3,
- 4,
- 5
- ],
- [
- 6,
- 7,
- 8
- ],
- [
- 9,
- 10,
- 11
- ]
- ];
- const MonthSelector = ({
- multipleSelectMinimumRequiredDayCount,
- monthOfYearLengthType = "long",
- isShowTodayIndicator = true,
- viewDate: viewDateProp,
- multipleSelectDayLimit,
- setIsSheetContentReady,
- selectMultipleObject,
- selectObject,
- dateRange,
- maxDate,
- minDate,
- variant,
- date
- }: IMonthSelectorProps, ref: Ref<IMonthSelectorRef>) => {
- const {
- radiuses,
- borders,
- spaces,
- colors
- } = NCoreUIKitTheme.useContext();
- const {
- localize
- } = NCoreUIKitLocalize.useContext();
- const selectDate = dateSelect({
- dateRange,
- variant,
- date
- });
- const [
- viewDate,
- setViewDate
- ] = useState(viewDateProp ? viewDateProp : selectDate);
- const [
- yearMonths,
- setYearMonths
- ] = useState<Array<CalendarMonth>>([]);
- const allSelectedMonths = useRef<Array<Date>>([]);
- const {
- nextPrevToolChevronButton: nextPrevToolChevronButtonDynamicStyle,
- nextPrevToolContainer: nextPrevToolContainerDynamicStyle,
- todayIndicator: todayIndicatorDynamicStyle,
- day: dayDynamicStyle
- } = useStyles({
- radiuses,
- borders,
- colors,
- spaces
- });
- useEffect(() => {
- const allMonths = getMonthsOfViewYear({
- tDate: viewDate
- });
- setYearMonths(allMonths);
- }, [
- dateRange,
- viewDate,
- date
- ]);
- useLayoutEffect(() => {
- if(yearMonths && yearMonths.length) setIsSheetContentReady(true);
- }, [
- yearMonths
- ]);
- useEffect(() => {
- if(viewDateProp) {
- setViewDate(viewDateProp);
- }
- }, [viewDateProp]);
- useImperativeHandle(
- ref,
- () => ({
- changeCurrentYear: (date: Date) => {
- setViewDate(date);
- }
- }),
- []
- );
- const getMonthsOfViewYear = ({
- tDate
- }: {
- tDate: Date
- }): Array<CalendarMonth> => {
- allSelectedMonths.current = [];
- const targetDate = moment(new Date(tDate));
- const startOfCalendar = targetDate.clone().startOf("year");
- const months = monthOfYearLengthType === "short" ? moment.monthsShort() : moment.months();
- const totalMonths = months.length;
- const allMonths = Array.from({
- length: totalMonths
- }).map((_, index) => {
- const currentMonth = startOfCalendar.clone().add(index, "months");
- let isSelected = false;
- if(variant === "single" && date) {
- allSelectedMonths.current = [date];
- if(moment(currentMonth).isSame(date, "month")) isSelected = true;
- } else if(variant === "range" && dateRange && dateRange.start) {
- if(dateRange.end) {
- allSelectedMonths.current = Array.from({
- length: moment(dateRange.end).diff(dateRange.start, "months") + 1
- }, (_, i) =>
- moment(dateRange.start).clone().add(i, "months").toDate()
- );
- if(moment(currentMonth).isBetween(dateRange.start, dateRange.end, "month", "[]")) isSelected = true;
- } else if(moment(currentMonth).isSame(dateRange.start)) {
- allSelectedMonths.current = [dateRange.start];
- isSelected = true;
- }
- }
- let isDisabled = false;
- if(minDate && moment(currentMonth).isBefore(moment(minDate).startOf("month"))) {
- isDisabled = true;
- }
- if(maxDate && moment(currentMonth).isAfter(moment(maxDate).endOf("month"))) {
- isDisabled = true;
- }
- const monthNumber = currentMonth.month();
- return {
- originalIndex: allSelectedMonths.current.findIndex((month) => moment(currentMonth).isSame(month, "month")),
- isCurrentYear: currentMonth.isSame(targetDate, "year"),
- isToday: currentMonth.isSame(moment(), "month"),
- title: months[monthNumber] as string,
- monthNumber: monthNumber + 1,
- monthOfYear: monthNumber,
- date: currentMonth,
- isDisabled,
- isSelected
- };
- });
- return allMonths;
- };
- const renderDay = ({
- monthIndex,
- monthItem
- }: {
- monthItem: CalendarMonth;
- monthIndex: number;
- }) => {
- const isLastItemSelected = monthItem.isSelected && monthItem.originalIndex === allSelectedMonths.current.length - 1;
- const isFirstItemSelected = monthItem.isSelected && monthItem.originalIndex === 0;
- const selectionStyle: Array<Mutable<ViewStyle> | null> = [
- isFirstItemSelected && allSelectedMonths.current.length > 1 ? {
- borderBottomRightRadius: 0,
- borderTopRightRadius: 0
- } : null,
- isLastItemSelected && allSelectedMonths.current.length > 1 ? {
- borderBottomLeftRadius: 0,
- borderTopLeftRadius: 0
- } : null
- ];
- let dayTitleColor: keyof NCoreUIKit.TextContentColors = "mid";
- if(monthItem.isSelected && !isLastItemSelected && !isFirstItemSelected) {
- dayTitleColor = "emphasized";
- selectionStyle.push({
- borderBottomRightRadius: 0,
- borderBottomLeftRadius: 0,
- borderTopRightRadius: 0,
- borderTopLeftRadius: 0
- });
- } else if(monthItem.isSelected) {
- dayTitleColor = "onPrimary";
- }
- if(!monthItem.isCurrentYear || monthItem.isDisabled) {
- dayTitleColor = "disabled";
- }
- return <TouchableOpacity
- disabled={!monthItem.isCurrentYear || monthItem.isDisabled}
- key={`month-${monthIndex}`}
- onPress={() => {
- if(!monthItem.isCurrentYear) {
- return;
- }
- if(variant === "single") {
- const newDate = date as Date;
- newDate.setMonth(monthItem.date.toDate().getMonth());
- selectObject(newDate);
- } else if(variant === "range") {
- if(!dateRange || !dateRange.start) {
- selectMultipleObject({
- start: new Date(monthItem.date.toDate().setHours(0, 0, 0)),
- end: undefined
- });
- return;
- }
- if(dateRange && moment(dateRange.start).isSame(monthItem.date, "month")) {
- selectMultipleObject({
- start: undefined,
- end: undefined
- });
- return;
- }
- if(dateRange && !dateRange.end) {
- if(multipleSelectDayLimit && moment(monthItem.date).diff(dateRange.start, "month") >= multipleSelectDayLimit) {
- NCoreUIKitToast.open({
- title: localize("maximum-selection-number-of-days-limit-exceeds")
- });
- return;
- }
- if(multipleSelectMinimumRequiredDayCount && moment(monthItem.date).diff(dateRange.start, "month") + 1 < multipleSelectMinimumRequiredDayCount) {
- NCoreUIKitToast.open({
- title: localize("minimum-selection-number-of-days-required-not-provided")
- });
- return;
- }
- if(moment(monthItem.date).isBefore(dateRange.start)) {
- selectMultipleObject({
- start: new Date(monthItem.date.toDate().setHours(0, 0, 0)),
- end: undefined
- });
- } else {
- selectMultipleObject({
- end: new Date(monthItem.date.toDate().setHours(23, 59, 59)),
- start: dateRange?.start
- });
- }
- return;
- }
- selectMultipleObject({
- start: monthItem.date.toDate(),
- end: undefined
- });
- }
- }}
- style={[
- stylesheet.day,
- dayDynamicStyle,
- monthItem.isSelected ? {
- backgroundColor: colors.content.container.primary,
- borderColor: colors.content.container.primary
- } : null,
- monthItem.isSelected && !isLastItemSelected && !isFirstItemSelected ? {
- backgroundColor: colors.content.container.emphasized,
- borderColor: colors.content.container.emphasized
- } : null,
- monthItem.isSelected && (monthItem.isDisabled || !monthItem.isCurrentYear) ? {
- opacity: 0.33
- } : null,
- ...selectionStyle
- ]}
- >
- <Text
- variant="labelLargeSize"
- color={dayTitleColor}
- >
- {monthItem.title}
- </Text>
- {isShowTodayIndicator && monthItem.isToday ? <View
- style={[
- stylesheet.todayIndicator,
- selectionStyle,
- todayIndicatorDynamicStyle,
- monthItem.isSelected ? {
- borderColor: colors.content.border.subtle
- } : null
- ]}
- /> : null}
- </TouchableOpacity>;
- };
- const renderNextPrevTool = () => {
- return <View
- style={[
- stylesheet.nextPrevToolContainer,
- nextPrevToolContainerDynamicStyle
- ]}
- >
- <TouchableOpacity
- onPress={() => {
- const prevViewDate = moment(viewDate).clone().subtract(1, "years");
- setViewDate(prevViewDate.toDate());
- }}
- style={[
- nextPrevToolChevronButtonDynamicStyle
- ]}
- >
- <ChevronLeftIcon
- color={colors.content.icon.default}
- size={26}
- />
- </TouchableOpacity>
- <Text
- variant="labelLargeSize"
- color="high"
- >
- {moment(viewDate).format("YYYY")}
- </Text>
- <TouchableOpacity
- onPress={() => {
- const nextViewDate = moment(viewDate).clone().add(1, "years");
- setViewDate(nextViewDate.toDate());
- }}
- style={[
- nextPrevToolChevronButtonDynamicStyle
- ]}
- >
- <ChevronRightIcon
- color={colors.content.icon.default}
- size={26}
- />
- </TouchableOpacity>
- </View>;
- };
- return <View
- style={[
- stylesheet.container
- ]}
- >
- <View>
- {renderNextPrevTool()}
- </View>
- <View
- style={[
- stylesheet.contentContainer
- ]}
- >
- {yearMonths && yearMonths.length ? MONTH_LINES.map((mItem, mIndex) => {
- const currentMonths = mItem.map(mI => yearMonths[mI]);
- return <View
- key={`month-row-${mIndex}`}
- style={[
- stylesheet.rowContainer,
- webStyle({
- width: "100%"
- })
- ]}
- >
- {currentMonths.map((monthItem, monthIndex) => {
- return renderDay({
- monthItem: monthItem as CalendarMonth,
- monthIndex
- });
- })}
- </View>;
- }) : null}
- </View>
- </View>;
- };
- export default forwardRef(MonthSelector);
|