Focus of custom caption is lost #1502
-
Bug descriptionWhen passing a custom caption element, it probably manages an initial state for the navigation. To reproducehttps://codesandbox.io/s/react-day-picker-8-forked-02co1i Steps
Expected behaviorThe focus should stay on the caption element. Additional contextWe compared the behavior with the old |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I'm also running into this same issue. Even with a ref set up, the custom caption components do not regain focus. I'm thinking this has to do with the focus context? |
Beta Was this translation helpful? Give feedback.
-
Thanks! I understand the docs for the custom components is not clear :) Adding better docs in #1501. I see in the code above that the type of the returned function (passed to A better approach to the custom component is to pass the component function straight to the components prop. See how the https://codesandbox.io/s/react-day-picker-8-forked-bqkeo9?file=/src/App.tsx import React from 'react';
import { format } from 'date-fns';
import { CaptionProps, DayPicker, useNavigation } from 'react-day-picker';
function CustomCaption(props: CaptionProps) {
const { goToMonth, nextMonth, previousMonth } = useNavigation();
return (
<h2>
{format(props.displayMonth, 'MMM yyy')}
<button
disabled={!previousMonth}
onClick={() => previousMonth && goToMonth(previousMonth)}
>
Previous
</button>
<button
disabled={!nextMonth}
onClick={() => nextMonth && goToMonth(nextMonth)}
>
Next
</button>
</h2>
);
}
export default function App() {
return (
<DayPicker
components={{
Caption: CustomCaption
}}
/>
);
} |
Beta Was this translation helpful? Give feedback.
Thanks! I understand the docs for the custom components is not clear :) Adding better docs in #1501.
I see in the code above that the type of the returned function (passed to
components: { Caption }
) is not correct. I haven't investigated why this causes the DayPicker to render nothing :)A better approach to the custom component is to pass the component function straight to the components prop. See how the
useNavigation
hook can be used here to navigate between the months:https://codesandbox.io/s/react-day-picker-8-forked-bqkeo9?file=/src/App.tsx