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

[ReviewEntries] Scroll-to-top after changing rows-per-page #2713

Merged
merged 5 commits into from
Oct 16, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export default function ReviewEntriesTable(
const { t } = useTranslation();
const [maxRows, setMaxRows] = useState(words.length);
const [pageState, setPageState] = useState(getPageState(words.length));
const [scrollToTop, setScrollToTop] = useState(false);

const updateMaxRows = (): void => {
if (tableRef.current) {
Expand All @@ -79,6 +80,15 @@ export default function ReviewEntriesTable(
});
}, [maxRows, setPageState]);

useEffect(() => {
// onRowsPerPageChange={() => window.scrollTo({ top: 0 })} doesn't work.
// This useEffect on an intermediate state triggers scrolling at the right time.
if (scrollToTop) {
window.scrollTo({ behavior: "smooth", top: 0 });
setScrollToTop(false);
}
}, [scrollToTop]);

const materialTableLocalization = {
body: {
editRow: {
Expand Down Expand Up @@ -136,6 +146,7 @@ export default function ReviewEntriesTable(
)}
data={words}
onFilterChange={updateMaxRows}
onRowsPerPageChange={() => setScrollToTop(true)}
editable={{
onRowUpdate: (newData: ReviewEntriesWord, oldData: ReviewEntriesWord) =>
new Promise(async (resolve, reject) => {
Expand Down