-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure that the Table defaults to the page that will contain the sele…
…cted row when it is first mounted
- Loading branch information
1 parent
b4fd8ea
commit be359f9
Showing
5 changed files
with
80 additions
and
37 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
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,14 @@ | ||
import { describe, expect, test } from "vitest"; | ||
import { calculatePageForSelectedRow } from "./pagination"; | ||
|
||
describe("calculatePageForSelectedRow", () => { | ||
test.each([ | ||
[undefined, ["a", "b", "c"], (option: string) => option, 1], | ||
["a", ["a", "b", "c"], (option: string) => option, 1], | ||
["j", ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"], (option: string) => option, 1], | ||
["k", ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"], (option: string) => option, 2], | ||
])('selectedOption: %s, options: %s, key: %s, expectedPage: %s', (selectedOption, options, key, expectedPage) => { | ||
const page = calculatePageForSelectedRow(selectedOption, options, key); | ||
expect(page).toBe(expectedPage); | ||
}); | ||
}); |
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,12 @@ | ||
export function calculatePageForSelectedRow<T>(selectedOption: T | undefined, options: T[], key: (option: T) => string): number { | ||
const pageSize = 10; | ||
|
||
if (selectedOption) { | ||
const index = options.findIndex((option) => key(option) === key(selectedOption)); | ||
if (index >= 0) { | ||
const page = Math.floor(index / pageSize) + 1; | ||
return page; | ||
} | ||
} | ||
return 1; | ||
} |