Skip to content

Commit

Permalink
docs: fix custom components docs (#2668)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodgobbi authored Jan 19, 2025
1 parent 6b0abf9 commit bf909c2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 43 deletions.
50 changes: 31 additions & 19 deletions examples/CustomDayButton.tsx
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>
);
}
58 changes: 34 additions & 24 deletions website/docs/guides/custom-components.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ Pass the components to customize to the `components` prop. See the [list of cust
```tsx
<DayPicker
components={{
Day: (props: DayProps) => <CustomDaycell {...props} />,
MonthGrid: (props: MonthGridProps) => <CustomMonthGrid {...props} />
Day: CustomDaycell,
MonthGrid: CustomMonthGrid
// etc
}}
/>
Expand Down Expand Up @@ -79,33 +79,43 @@ For example, you can use a custom [DayButton](../api/functions/DayButton.md) to
```tsx title="./MyDatePicker.tsx"
import React from "react";

import { DayPicker } from "react-day-picker";
import { DayButtonProps, DayPicker } from "react-day-picker";

export function MyDatePicker() {
const [selected, setSelected] = React.useState<Date>();
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 (
<DayPicker
mode="single"
onSelect={setSelected}
selected={selected}
components={{
DayButton: (props) => {
const { day, modifiers, ...buttonProps } = props;
return (
<button
{...buttonProps}
// Prevent the default click event
onClick={() => setSelected(undefined)}
// Handle the double click event and reset the selection
onDoubleClick={() => setSelected(day.date)}
/>
);
}
}}
footer={selected?.toDateString() || "Double click to select a date"}
<button
{...buttonProps}
onClick={() => setSelected?.(undefined)}
onDoubleClick={() => setSelected?.(day.date)}
/>
);
}

export function CustomDayButton() {
const [selected, setSelected] = React.useState<Date>();

return (
<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>
);
}
```

<BrowserWindow>
Expand Down

0 comments on commit bf909c2

Please sign in to comment.