import { createElement, type RefObject, type ReactNode } from "react"; import CalendarMonthIcon from "@mui/icons-material/CalendarMonth"; import SettingsIcon from "@mui/icons-material/Settings"; import { CustomButtons } from "./CustomButtons"; import { createRenderCustomHeader } from "./renderCustomHeader"; const getRenderCustomHeader = (layout: string, datePickerStyleType: string) => { if (layout === "Multi Month") { return createRenderCustomHeader(false); } if (layout === "Default" && datePickerStyleType === "Dropdown") { return createRenderCustomHeader(true); } return undefined; }; const buildDatePickerProps = ({ startDate, onChange, props, dayClassName, datePickerRef, customInput, overrides = {}, }: { startDate: Date | null; onChange: (date: any) => void; props: any; dayClassName: (date: Date) => string; datePickerRef: RefObject; customInput: ReactNode; overrides?: Record; }) => { const renderCustomHeader = getRenderCustomHeader(props.layout, props.datePickerStyleType); const usesCustomHeaderDropdown = Boolean(renderCustomHeader); return { selected: startDate || null, onChange, customInput, wrapperClassName: "rds-datepicker__wrapper", disabled: props.isDisabled, placeholderText: props.placeholderText || "Select date", showMonthYearPicker: props.layout === "Month Picker", showYearPicker: props.layout === "Year Picker", showPreviousMonths: true, monthsShown: props.layout === "Multi Month" ? 3 : 1, scrollableMonthYearDropdown: props.datePickerStyleType === "Dropdown", todayButton: , peekNextMonth: true, showMonthDropdown: props.datePickerStyleType === "Dropdown" && !usesCustomHeaderDropdown, showYearDropdown: props.datePickerStyleType === "Dropdown" && !usesCustomHeaderDropdown, dropdownMode: "select", dayClassName, ref: datePickerRef, renderCustomHeader, calendarClassName: props.layout === "Default" ? "rds-datepicker--layout-default" : undefined, ...overrides, }; }; export const renderDatePickerStateView = ( state: string, startDate: Date | null, handlerDateChange: (date: Date | null) => void, handlerDateTimeChange: (date: any) => void, props: any, datePickerRef: RefObject, expandedDatePickerRef: RefObject, selectedDatePickerRef: RefObject, dayClassName: (date: Date) => string, CustomInputWithClear: React.ElementType, CustomButtons: React.ElementType ) => { const SafeDatePicker = props.SafeDatePicker; if (state === "Default") { const pickerProps = buildDatePickerProps({ startDate, onChange: handlerDateChange, props, dayClassName, datePickerRef, customInput: ( ), }); return (
); } else if (state === "Expanded") { const pickerProps = buildDatePickerProps({ startDate, onChange: handlerDateTimeChange, props, dayClassName, datePickerRef: expandedDatePickerRef, customInput: ( ), overrides: { timeInputLabel: "Time:", dateFormat: "MM/dd/yyyy h:mm aa", autoFocus: true, }, }); return (
); } else if (state === "Selected") { const pickerProps = buildDatePickerProps({ startDate, onChange: handlerDateTimeChange, props, dayClassName, datePickerRef: selectedDatePickerRef, customInput: ( ), overrides: { timeInputLabel: "Time:", dateFormat: "MM/dd/yyyy", }, }); return (
); } return null; }; export const renderDatePickerTypeView = ( type: string, startDate: Date | null, endDate: Date | null, handlerDateChange: (date: Date | null) => void, onRangeChange: (dates: [Date | null, Date | null]) => void, props: any, isDropdownOpen: boolean, toggleDropdown: () => void, dropdownDisplayValue: string, datePickerRef: RefObject, activeList: string, todayClickHandler: () => void, yesterdayClickHandler: () => void, lastSevenDaysClickHandler: () => void, lastFourteenDaysClickHandler: () => void, customClickHandler: () => void, dayClassName: (date: Date) => string, CustomInputWithClear: React.ElementType, ExampleCustomInput: React.ElementType, CustomButtons: React.ElementType, customDropdownRef?: RefObject ) => { const SafeDatePicker = props.SafeDatePicker; if (type === "Default") { const pickerProps = buildDatePickerProps({ startDate, onChange: handlerDateChange, props, dayClassName, datePickerRef, customInput: ( ), }); return (
); } else if (type === "Custom") { const todayButtonElement = ; const rangeCustomInput = createElement(ExampleCustomInput, { changeIcon: props.changeIcon, onActivate: customClickHandler, }); const pickerProps = buildDatePickerProps({ startDate, onChange: handlerDateChange, props, dayClassName, datePickerRef, customInput: ( ), overrides: { open: false }, }); return (
  • Custom Date {dropdownDisplayValue ? ( {dropdownDisplayValue} ) : null}
  • {children}
    } popperClassName="rds-datepicker__popper" disabled={props.isDisabled} placeholderText={props.placeholderText || "Select date"} showMonthYearPicker={props.layout === "Month Picker"} showYearPicker={props.layout === "Year Picker"} todayButton={todayButtonElement} peekNextMonth={true} showMonthDropdown={props.datePickerStyleType === "Dropdown" && !getRenderCustomHeader(props.layout, props.datePickerStyleType)} showYearDropdown={props.datePickerStyleType === "Dropdown" && !getRenderCustomHeader(props.layout, props.datePickerStyleType)} dropdownMode="select" showPreviousMonths monthsShown={props.layout === "Multi Month" ? 3 : 1} dayClassName={dayClassName} renderCustomHeader={getRenderCustomHeader(props.layout, props.datePickerStyleType)} calendarClassName={props.layout === "Default" ? "rds-datepicker--layout-default" : undefined} customInput={rangeCustomInput} />
); } return null; };