Skip to content

Added "onClear" callback #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div>
<Datepicker options={options} onChange={handleChange} show={show} setShow={handleClose} />
<Datepicker options={options} onChange={handleChange} onClear={handleClear} show={show} setShow={handleClose} />
</div>
)
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions demo-app/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -27,6 +28,7 @@ const Home: NextPage = () => {
selected: "",
},
}

return (
<div className="flex flex-col items-center w-full h-full gap-5 mt-20">
<ThemeSelector />
Expand Down
7 changes: 5 additions & 2 deletions src/Components/Buttons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,18 @@ export const ButtonToday = () => {
}

export const ButtonClear = () => {
const { setShowSelectedDate, options } = useContext(DatePickerContext)
const { setShowSelectedDate, onClear, options } = useContext(DatePickerContext)
return (
<button
type="button"
className={twMerge(
"w-full px-5 py-2 text-sm font-medium text-center text-gray-900 bg-white border border-gray-300 rounded-lg dark:text-white dark:bg-gray-700 dark:border-gray-600 hover:bg-gray-100 dark:hover:bg-gray-600 focus:ring-4 focus:ring-blue-300",
options?.theme?.clearBtn
)}
onClick={() => setShowSelectedDate(false)}
onClick={() => {
setShowSelectedDate(false);
if (onClear) onClear()
}}
>
{options?.clearBtnText}
</button>
Expand Down
5 changes: 3 additions & 2 deletions src/Components/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => (
<div className={twMerge("w-full", classNames)}>
<DatePickerProvider options={options} onChange={onChange} show={show} setShow={setShow} selectedDateState={selectedDateState}>
<DatePickerProvider options={options} onChange={onChange} onClear={onClear} show={show} setShow={setShow} selectedDateState={selectedDateState}>
<DatePickerMain value={value} options={options}>
{children}
</DatePickerMain>
Expand Down
8 changes: 5 additions & 3 deletions src/Components/DatePickerProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface IDatePickerContext {
setShowSelectedDate: Dispatch<SetStateAction<boolean>>
selectedMonth: number
selectedYear: number
onClear: () => void
getFormattedDate: (date: Date | number, formatOptions?: Intl.DateTimeFormatOptions | null | undefined) => string
}

Expand All @@ -32,20 +33,21 @@ export const DatePickerContext = createContext<IDatePickerContext>({
setShowSelectedDate: () => {},
selectedMonth: 0,
selectedYear: 0,
onClear: () => {},
getFormattedDate: () => "",
})

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<Views>("days")
const [selectedDate, setSelectedDate] = selectedDateState || useState<Date>(options?.defaultDate || new Date())
Expand All @@ -67,7 +69,7 @@ const DatePickerProvider = ({ children, options: customOptions, onChange, show,

return (
<DatePickerContext.Provider
value={{ options, view, setView, show, setShow, selectedDate, changeSelectedDate, showSelectedDate, setShowSelectedDate, selectedMonth, selectedYear, getFormattedDate }}
value={{ options, view, setView, show, setShow, selectedDate, changeSelectedDate, showSelectedDate, setShowSelectedDate, selectedMonth, selectedYear, onClear, getFormattedDate }}
>
{children}
</DatePickerContext.Provider>
Expand Down