-
-
Notifications
You must be signed in to change notification settings - Fork 707
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(website): add example for week custom selections
- Loading branch information
Showing
4 changed files
with
159 additions
and
2 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
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { mockDate, gridcell, renderApp, user, act } from "@/test"; | ||
|
||
import { CustomWeek } from "./CustomWeek"; | ||
|
||
const today = new Date(2021, 10, 25); | ||
mockDate(today); | ||
|
||
beforeEach(() => { | ||
renderApp(<CustomWeek />); | ||
}); | ||
|
||
describe("when a day is clicked", () => { | ||
beforeEach(async () => { | ||
await act(() => user.click(gridcell(today))); | ||
}); | ||
test("the whole week should appear selected", () => { | ||
const week = [ | ||
gridcell(new Date(2021, 10, 21)), | ||
gridcell(new Date(2021, 10, 22)), | ||
gridcell(new Date(2021, 10, 23)), | ||
gridcell(new Date(2021, 10, 24)), | ||
gridcell(new Date(2021, 10, 25)), | ||
gridcell(new Date(2021, 10, 26)), | ||
gridcell(new Date(2021, 10, 27)) | ||
]; | ||
week.forEach((day) => { | ||
expect(day).toHaveAttribute("aria-selected", "true"); | ||
}); | ||
}); | ||
describe("when clicking the day again", () => { | ||
beforeEach(async () => { | ||
await act(() => user.click(gridcell(today))); | ||
}); | ||
test("the whole week should not be selected", () => { | ||
const week = [ | ||
gridcell(new Date(2021, 10, 21)), | ||
gridcell(new Date(2021, 10, 22)), | ||
gridcell(new Date(2021, 10, 23)), | ||
gridcell(new Date(2021, 10, 24)), | ||
gridcell(new Date(2021, 10, 25)), | ||
gridcell(new Date(2021, 10, 26)), | ||
gridcell(new Date(2021, 10, 27)) | ||
]; | ||
week.forEach((day) => { | ||
expect(day).not.toHaveAttribute("aria-selected", "true"); | ||
}); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useState } from "react"; | ||
|
||
import { endOfWeek, isSameWeek, startOfWeek } from "date-fns"; | ||
import { DateRange, DayPicker } from "react-day-picker"; | ||
|
||
/** Select the whole week when the day is clicked. */ | ||
export function CustomWeek() { | ||
const [selectedWeek, setSelectedWeek] = useState<DateRange | undefined>(); | ||
|
||
return ( | ||
<DayPicker | ||
showWeekNumber | ||
modifiers={{ | ||
selected: selectedWeek | ||
}} | ||
onDayClick={(day, modifiers) => { | ||
if (modifiers.selected) { | ||
setSelectedWeek(undefined); // clear the selection if the week is already selected | ||
return; | ||
} | ||
setSelectedWeek({ | ||
from: startOfWeek(day), | ||
to: endOfWeek(day) | ||
}); | ||
}} | ||
onWeekNumberClick={(weekNumber, dates) => { | ||
if (selectedWeek?.from && isSameWeek(dates[0], selectedWeek.from)) { | ||
setSelectedWeek(undefined); // clear the selection if the week is already selected | ||
return; | ||
} | ||
setSelectedWeek({ | ||
from: startOfWeek(dates[0]), | ||
to: endOfWeek(dates[dates.length - 1]) | ||
}); | ||
}} | ||
footer={ | ||
selectedWeek && ( | ||
<p> | ||
Week from {selectedWeek.from.toLocaleDateString()} to | ||
{selectedWeek.to.toLocaleDateString()} | ||
</p> | ||
) | ||
} | ||
/> | ||
); | ||
} |
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