diff --git a/README.md b/README.md index e45dafe..9e7be31 100644 --- a/README.md +++ b/README.md @@ -93,13 +93,16 @@ const DemoComponent = () => { const handleChange = (selectedDate: Date) => { console.log(selectedDate) } + const handleClear = () => { + console.log('Date is cleared!') + } const handleClose = (state: boolean) => { setShow(state) } return (
- +
) } @@ -179,6 +182,7 @@ const DemoComponent = () => { - value?: Date - options?: [IOptions](###IOptions) - onChange?: (date: Date) => void +- onClear?: () => void - show: boolean - setShow: (show: boolean) => void - classNames?: string diff --git a/demo-app/pages/index.tsx b/demo-app/pages/index.tsx index a4bfe3b..c837e05 100644 --- a/demo-app/pages/index.tsx +++ b/demo-app/pages/index.tsx @@ -1,6 +1,7 @@ import type { NextPage } from "next" import { useState } from "react" import DatePicker from "tailwind-datepicker-react" +// import DatePicker from "../../dist/Components/DatePicker" import ThemeSelector from "../components/ThemeSelector" import { IOptions } from "tailwind-datepicker-react/types/Options" @@ -27,6 +28,7 @@ const Home: NextPage = () => { selected: "", }, } + return (
diff --git a/src/Components/Buttons.tsx b/src/Components/Buttons.tsx index 27b451c..0c58cd8 100644 --- a/src/Components/Buttons.tsx +++ b/src/Components/Buttons.tsx @@ -98,7 +98,7 @@ export const ButtonToday = () => { } export const ButtonClear = () => { - const { setShowSelectedDate, options } = useContext(DatePickerContext) + const { setShowSelectedDate, onClear, options } = useContext(DatePickerContext) return ( diff --git a/src/Components/DatePicker.tsx b/src/Components/DatePicker.tsx index acadaff..9fa9319 100644 --- a/src/Components/DatePicker.tsx +++ b/src/Components/DatePicker.tsx @@ -10,15 +10,16 @@ export interface IDatePickerProps { children?: ReactElement | ReactNode options?: IOptions onChange?: (date: Date) => void + onClear?: () => void show: boolean setShow: (show: boolean) => void classNames?: string selectedDateState?: [Date, (date: Date) => void] } -const DatePicker = ({ value, children, options, onChange, classNames, show, setShow, selectedDateState }: IDatePickerProps) => ( +const DatePicker = ({ value, children, options, onChange, onClear, classNames, show, setShow, selectedDateState }: IDatePickerProps) => (
- + {children} diff --git a/src/Components/DatePickerProvider.tsx b/src/Components/DatePickerProvider.tsx index aa1274c..546d547 100644 --- a/src/Components/DatePickerProvider.tsx +++ b/src/Components/DatePickerProvider.tsx @@ -15,6 +15,7 @@ interface IDatePickerContext { setShowSelectedDate: Dispatch> selectedMonth: number selectedYear: number + onClear: () => void getFormattedDate: (date: Date | number, formatOptions?: Intl.DateTimeFormatOptions | null | undefined) => string } @@ -32,6 +33,7 @@ export const DatePickerContext = createContext({ setShowSelectedDate: () => {}, selectedMonth: 0, selectedYear: 0, + onClear: () => {}, getFormattedDate: () => "", }) @@ -39,13 +41,13 @@ interface IDatePickerProviderProps { children: ReactElement options?: IOptions onChange?: (date: Date) => void + onClear: () => void show: boolean setShow: (show: boolean) => void selectedDateState?: [Date, (date: Date) => void] } -const DatePickerProvider = ({ children, options: customOptions, onChange, show, setShow, selectedDateState }: IDatePickerProviderProps) => { - +const DatePickerProvider = ({ children, options: customOptions, onChange, onClear, show, setShow, selectedDateState }: IDatePickerProviderProps) => { const options = { ...defaultOptions, ...customOptions } const [view, setView] = useState("days") const [selectedDate, setSelectedDate] = selectedDateState || useState(options?.defaultDate || new Date()) @@ -67,7 +69,7 @@ const DatePickerProvider = ({ children, options: customOptions, onChange, show, return ( {children}