Skip to content
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

chore: move Nav to own component #2435

Merged
merged 4 commits into from
Sep 8, 2024
Merged
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
37 changes: 5 additions & 32 deletions src/DayPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ export function DayPicker(props: DayPickerProps) {
labelGrid,
labelMonthDropdown,
labelNav,
labelNext,
labelPrevious,
labelWeekday,
labelWeekNumber,
labelWeekNumberHeader,
Expand Down Expand Up @@ -278,36 +276,11 @@ export function DayPicker(props: DayPickerProps) {
className={classNames[UI.Nav]}
style={styles?.[UI.Nav]}
aria-label={labelNav()}
>
<components.Button
type="button"
className={classNames[UI.ButtonPrevious]}
tabIndex={previousMonth ? undefined : -1}
disabled={previousMonth ? undefined : true}
aria-label={labelPrevious(previousMonth)}
onClick={handlePreviousClick}
>
<components.Chevron
disabled={previousMonth ? undefined : true}
className={classNames[UI.Chevron]}
orientation="left"
/>
</components.Button>
<components.Button
type="button"
className={classNames[UI.ButtonNext]}
tabIndex={nextMonth ? undefined : -1}
disabled={nextMonth ? undefined : true}
aria-label={labelNext(nextMonth)}
onClick={handleNextClick}
>
<components.Chevron
disabled={previousMonth ? undefined : true}
orientation="right"
className={classNames[UI.Chevron]}
/>
</components.Button>
</components.Nav>
onPreviousClick={handlePreviousClick}
onNextClick={handleNextClick}
previousMonth={previousMonth}
nextMonth={nextMonth}
/>
)}
{months.map((calendarMonth, displayIndex) => {
const handleMonthChange: ChangeEventHandler<HTMLSelectElement> = (
Expand Down
61 changes: 58 additions & 3 deletions src/components/Nav.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,68 @@
import React from "react";
import React, { MouseEventHandler } from "react";

import { UI } from "../UI.js";
import { useDayPicker } from "../useDayPicker.js";

/**
* Render the toolbar with the navigation button.
*
* @group Components
* @see https://daypicker.dev/guides/custom-components
*/
export function Nav(props: JSX.IntrinsicElements["nav"]) {
return <nav {...props} />;
export function Nav(
props: {
onPreviousClick?: MouseEventHandler<HTMLButtonElement>;
onNextClick?: MouseEventHandler<HTMLButtonElement>;
previousMonth?: Date | undefined;
nextMonth?: Date | undefined;
} & JSX.IntrinsicElements["nav"]
) {
const {
onPreviousClick,
onNextClick,
previousMonth,
nextMonth,
...navProps
} = props;

const {
components,
classNames,
labels: { labelPrevious, labelNext }
} = useDayPicker();

return (
<nav {...navProps}>
<components.Button
type="button"
className={classNames[UI.ButtonPrevious]}
tabIndex={previousMonth ? undefined : -1}
disabled={previousMonth ? undefined : true}
aria-label={labelPrevious(previousMonth)}
onClick={props.onPreviousClick}
>
<components.Chevron
disabled={previousMonth ? undefined : true}
className={classNames[UI.Chevron]}
orientation="left"
/>
</components.Button>
<components.Button
type="button"
className={classNames[UI.ButtonNext]}
tabIndex={nextMonth ? undefined : -1}
disabled={nextMonth ? undefined : true}
aria-label={labelNext(nextMonth)}
onClick={props.onNextClick}
>
<components.Chevron
disabled={previousMonth ? undefined : true}
orientation="right"
className={classNames[UI.Chevron]}
/>
</components.Button>
</nav>
);
}

export type NavProps = Parameters<typeof Nav>[0];