Skip to content

Commit 63aa7a2

Browse files
authored
docs: update CustomDayButton example and clarify custom components doc (#2815)
* docs: update CustomDayButton example Signed-off-by: gpbl <[email protected]> * Update test Signed-off-by: gpbl <[email protected]> * Lint file, set default formatter Signed-off-by: gpbl <[email protected]> --------- Signed-off-by: gpbl <[email protected]>
1 parent 9c7b2c6 commit 63aa7a2

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
lines changed

examples/CustomDayButton.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ test("update the footer when a day is double clicked", () => {
2121

2222
test("update the footer when a day is single clicked", () => {
2323
fireEvent.click(dateButton(startOfMonth(today)));
24-
expect(screen.getByText("Double click to select a date")).toBeInTheDocument();
24+
expect(screen.getByText(/Double click to select a date/)).toBeInTheDocument();
2525
});

examples/CustomDayButton.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import React from "react";
22

3-
import { type DayButtonProps, DayPicker } from "react-day-picker";
3+
import { DayButton, type DayButtonProps, DayPicker } from "react-day-picker";
44

55
const SelectedDateContext = React.createContext<{
66
selected?: Date;
77
setSelected?: React.Dispatch<React.SetStateAction<Date | undefined>>;
88
}>({});
99

10-
function DayButton(props: DayButtonProps) {
10+
function DayButtonWithContext(props: DayButtonProps) {
1111
const { day, modifiers, ...buttonProps } = props;
1212

1313
const { setSelected } = React.use(SelectedDateContext);
1414
return (
15-
<button
15+
<DayButton
1616
{...buttonProps}
17+
day={day}
18+
modifiers={modifiers}
1719
onClick={() => setSelected?.(undefined)}
1820
onDoubleClick={() => setSelected?.(day.date)}
1921
/>
@@ -25,14 +27,15 @@ export function CustomDayButton() {
2527

2628
return (
2729
<SelectedDateContext.Provider value={{ selected, setSelected }}>
30+
<p>Double click to select a date or single click to clear selection</p>
2831
<DayPicker
2932
mode="single"
3033
selected={selected}
3134
onSelect={setSelected}
3235
components={{
33-
DayButton,
36+
DayButton: DayButtonWithContext,
3437
}}
35-
footer={selected?.toDateString() || "Double click to select a date"}
38+
footer={selected?.toDateString()}
3639
/>
3740
</SelectedDateContext.Provider>
3841
);

react-day-picker.code-workspace

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@
4343
"versioned_docs": true
4444
},
4545
"jest.runMode": "on-demand",
46+
"editor.defaultFormatter": "biomejs.biome",
4647
}
4748
}

website/docs/guides/custom-components.mdx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ Custom components allow you to extend DayPicker beyond just using [formatters](.
2828

2929
## Implementing a Custom Component
3030

31-
Pass the components you want to customize to the `components` prop. See the [list of custom components](#list-of-custom-components) below.
31+
- Pass the components you want to customize to the `components` prop (see the [list of custom components](#list-of-custom-components) below).
32+
- Consider reusing the core components and composing your customizations on top of them.
33+
- Use a custom React Context to share state between your custom components and your application.
3234

3335
```tsx
3436
<DayPicker
@@ -107,32 +109,36 @@ The DayPicker context provides the current state of the calendar, including the
107109

108110
### Intercepting Click Events
109111

110-
For example, you can use a custom [DayButton](../api/functions/DayButton.md) to select days with a double-click event.
112+
For example, you can use a custom [DayButton](../api/functions/DayButton.md) to select days with a double-click event. This approach uses a custom React context to share the selected date state between the custom `DayButton` and the main component.
111113

112114
```tsx title="./MyDatePicker.tsx"
113115
import React from "react";
114116

115-
import { DayButtonProps, DayPicker } from "react-day-picker";
117+
import { DayButton, type DayButtonProps, DayPicker } from "react-day-picker";
116118

119+
// Create a context to share the selected date state between the custom DayButton and the main component.
117120
const SelectedDateContext = React.createContext<{
118121
selected?: Date;
119122
setSelected?: React.Dispatch<React.SetStateAction<Date | undefined>>;
120123
}>({});
121124

122-
function DayButton(props: DayButtonProps) {
125+
// Custom DayButton that uses the context to set the selected date on double-click and clear it on single click.
126+
function DayButtonWithContext(props: DayButtonProps) {
123127
const { day, modifiers, ...buttonProps } = props;
124128

125-
const { setSelected } = React.useContext(SelectedDateContext);
129+
const { setSelected } = React.use(SelectedDateContext);
126130
return (
127-
<button
131+
<DayButton
128132
{...buttonProps}
133+
day={day}
134+
modifiers={modifiers}
129135
onClick={() => setSelected?.(undefined)}
130136
onDoubleClick={() => setSelected?.(day.date)}
131137
/>
132138
);
133139
}
134140

135-
export function CustomDayButton() {
141+
export function MyDatePicker() {
136142
const [selected, setSelected] = React.useState<Date>();
137143

138144
return (
@@ -142,9 +148,8 @@ export function CustomDayButton() {
142148
selected={selected}
143149
onSelect={setSelected}
144150
components={{
145-
DayButton,
151+
DayButton: DayButtonWithContext,
146152
}}
147-
footer={selected?.toDateString() || "Double click to select a date"}
148153
/>
149154
</SelectedDateContext.Provider>
150155
);

0 commit comments

Comments
 (0)