-
-
Notifications
You must be signed in to change notification settings - Fork 739
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: fix custom components docs (#2668)
- Loading branch information
Showing
2 changed files
with
65 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,39 @@ | ||
import React from "react"; | ||
|
||
import { DayPicker } from "react-day-picker"; | ||
import { DayButtonProps, DayPicker } from "react-day-picker"; | ||
|
||
const SelectedDateContext = React.createContext<{ | ||
selected?: Date; | ||
setSelected?: React.Dispatch<React.SetStateAction<Date | undefined>>; | ||
}>({}); | ||
|
||
function DayButton(props: DayButtonProps) { | ||
const { day, modifiers, ...buttonProps } = props; | ||
|
||
const { setSelected } = React.use(SelectedDateContext); | ||
return ( | ||
<button | ||
{...buttonProps} | ||
onClick={() => setSelected?.(undefined)} | ||
onDoubleClick={() => setSelected?.(day.date)} | ||
/> | ||
); | ||
} | ||
|
||
export function CustomDayButton() { | ||
const [selected, setSelected] = React.useState<Date>(); | ||
|
||
return ( | ||
<DayPicker | ||
mode="single" | ||
onSelect={setSelected} | ||
selected={selected} | ||
components={{ | ||
DayButton: (props) => { | ||
const { day, modifiers, ...buttonProps } = props; | ||
return ( | ||
<button | ||
{...buttonProps} | ||
onDoubleClick={() => setSelected(day.date)} | ||
onClick={() => setSelected(undefined)} | ||
/> | ||
); | ||
} | ||
}} | ||
footer={selected?.toDateString() || "Double click to select a date"} | ||
/> | ||
<SelectedDateContext.Provider value={{ selected, setSelected }}> | ||
<DayPicker | ||
mode="single" | ||
selected={selected} | ||
onSelect={setSelected} | ||
components={{ | ||
DayButton | ||
}} | ||
footer={selected?.toDateString() || "Double click to select a date"} | ||
/> | ||
</SelectedDateContext.Provider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters