11import React , { forwardRef , ReactElement , useContext , useEffect , useRef , ReactNode } from "react"
22import { twMerge } from "tailwind-merge"
33import { IOptions } from "../Options"
4+ import defaultOptions from "../Options"
45import DatePickerPopup from "./DatePickerPopup"
56import DatePickerProvider , { DatePickerContext } from "./DatePickerProvider"
67
@@ -10,19 +11,20 @@ export interface IDatePickerProps {
1011 onChange ?: ( date : Date ) => void
1112 show : boolean
1213 setShow : ( show : boolean ) => void
13- classNames ?: string ,
14+ classNames ?: string
1415 selectedDateState ?: [ Date , ( date : Date ) => void ]
1516}
1617
1718const DatePicker = ( { children, options, onChange, classNames, show, setShow, selectedDateState } : IDatePickerProps ) => (
1819 < div className = { twMerge ( "w-full" , classNames ) } >
1920 < DatePickerProvider options = { options } onChange = { onChange } show = { show } setShow = { setShow } selectedDateState = { selectedDateState } >
20- < DatePickerMain > { children } </ DatePickerMain >
21+ < DatePickerMain options = { options } > { children } </ DatePickerMain >
2122 </ DatePickerProvider >
2223 </ div >
2324)
2425
25- const DatePickerMain = ( { children } : { children ?: ReactElement } ) => {
26+ const DatePickerMain = ( { options : customOptions , children } : { options ?: IOptions ; children ?: ReactElement } ) => {
27+ const options = { ...defaultOptions , ...customOptions }
2628 const { setShow, show } = useContext ( DatePickerContext )
2729 const InputRef = useRef < HTMLInputElement > ( null )
2830 const DatePickerRef = useRef < HTMLDivElement > ( null )
@@ -51,21 +53,21 @@ const DatePickerMain = ({ children }: { children?: ReactElement }) => {
5153 < div className = "absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none" >
5254 < CalendarIcon />
5355 </ div >
54- < Input ref = { InputRef } />
56+ < Input ref = { InputRef } nameProp = { options ?. inputNameProp } />
5557 </ div >
5658 ) }
5759 { show && < DatePickerPopup ref = { DatePickerRef } /> }
5860 </ >
5961 )
6062}
6163
62- const Input = forwardRef < HTMLInputElement > ( ( _props , ref ) => {
64+ const Input = forwardRef < HTMLInputElement , { nameProp ?: string } > ( ( props , ref ) => {
6365 const { setShow, selectedDate, showSelectedDate, options, getFormattedDate } = useContext ( DatePickerContext )
6466 return (
6567 < input
6668 ref = { ref }
6769 type = "text"
68- name = "date"
70+ name = { props . nameProp || "date" }
6971 id = "date"
7072 className = { twMerge (
7173 "pl-9 pr-2.5 py-2.5 bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" ,
0 commit comments