-
-
Notifications
You must be signed in to change notification settings - Fork 734
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: controlled vs. uncontrolled selections (#2462)
* Update selection hook to use useControlledValue * Update useControlledValue docs * Add test case * Improve example and add test * Add console.log to better spy
- Loading branch information
Showing
8 changed files
with
135 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import React from "react"; | ||
|
||
import { dateButton, gridcell } from "@/test/elements"; | ||
import { render } from "@/test/render"; | ||
import { user } from "@/test/user"; | ||
|
||
import { ControlledSelection } from "./ControlledSelection"; | ||
|
||
const today = new Date(2024, 8, 17); | ||
beforeAll(() => jest.setSystemTime(today)); | ||
afterAll(() => jest.useRealTimers()); | ||
|
||
beforeEach(async () => {}); | ||
|
||
test("a range is selected after clicking two dates", async () => { | ||
render(<ControlledSelection />); | ||
await user.click(dateButton(new Date(2024, 8, 1))); | ||
await user.click(dateButton(new Date(2024, 8, 4))); | ||
|
||
expect(gridcell(new Date(2024, 8, 1), true)).toHaveAttribute( | ||
"aria-selected", | ||
"true" | ||
); | ||
expect(gridcell(new Date(2024, 8, 2), true)).toHaveAttribute( | ||
"aria-selected", | ||
"true" | ||
); | ||
expect(gridcell(new Date(2024, 8, 3), true)).toHaveAttribute( | ||
"aria-selected", | ||
"true" | ||
); | ||
expect(gridcell(new Date(2024, 8, 4), true)).toHaveAttribute( | ||
"aria-selected", | ||
"true" | ||
); | ||
expect(gridcell(new Date(2024, 8, 5), true)).not.toHaveAttribute( | ||
"aria-selected", | ||
"true" | ||
); | ||
}); | ||
|
||
test("a range is reset after clicking a third date", async () => { | ||
const consoleSpy = jest.spyOn(console, "log"); | ||
render(<ControlledSelection />); | ||
await user.click(dateButton(new Date(2024, 8, 1))); | ||
await user.click(dateButton(new Date(2024, 8, 4))); | ||
await user.click(dateButton(new Date(2024, 8, 5))); | ||
// eslint-disable-next-line no-console | ||
expect(console.log).toHaveBeenCalledWith("reset range"); | ||
expect(gridcell(new Date(2024, 8, 1), true)).not.toHaveAttribute( | ||
"aria-selected", | ||
"true" | ||
); | ||
expect(gridcell(new Date(2024, 8, 2), true)).not.toHaveAttribute( | ||
"aria-selected", | ||
"true" | ||
); | ||
expect(gridcell(new Date(2024, 8, 3), true)).not.toHaveAttribute( | ||
"aria-selected", | ||
"true" | ||
); | ||
expect(gridcell(new Date(2024, 8, 4), true)).not.toHaveAttribute( | ||
"aria-selected", | ||
"true" | ||
); | ||
expect(gridcell(new Date(2024, 8, 5), true)).toHaveAttribute( | ||
"aria-selected", | ||
"true" | ||
); | ||
expect(gridcell(new Date(2024, 8, 6), true)).not.toHaveAttribute( | ||
"aria-selected", | ||
"true" | ||
); | ||
|
||
consoleSpy.mockRestore(); | ||
}); |
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,31 @@ | ||
import React from "react"; | ||
import { useState } from "react"; | ||
|
||
import { DateRange, DayPicker } from "react-day-picker"; | ||
|
||
export function ControlledSelection() { | ||
const [selected, setSelected] = useState<DateRange | undefined>(); | ||
|
||
function handleOnSelect(range: DateRange | undefined, triggerDate: Date) { | ||
// Change the behavior of the selection when a range is already selected | ||
if (selected?.from && selected?.to) { | ||
// eslint-disable-next-line no-console | ||
console.log("reset range"); | ||
setSelected({ | ||
from: triggerDate, | ||
to: undefined | ||
}); | ||
} else { | ||
setSelected(range); | ||
} | ||
} | ||
|
||
return ( | ||
<DayPicker | ||
mode="range" | ||
min={1} | ||
selected={selected} | ||
onSelect={handleOnSelect} | ||
/> | ||
); | ||
} |
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
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
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