Select dropdown, scroll position resets to top when opened #1353
-
I followed the guide from the Data Table component. When I click the pagesize option, it will scroll to the top and I'm unable to select a new option if the table has to many rows. Here is my component codeimport {
type DataFunctionArgs,
json,
type SerializeFrom,
} from '@remix-run/node';
import { useLoaderData } from '@remix-run/react';
import {
type ColumnDef,
flexRender,
getCoreRowModel,
getPaginationRowModel,
useReactTable,
} from '@tanstack/react-table';
import { Button } from '#app/components/ui/button.tsx';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from '#app/components/ui/dropdown-menu.tsx';
import { Icon } from '#app/components/ui/icon.tsx';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '#app/components/ui/select.tsx';
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '#app/components/ui/table.tsx';
import { prisma } from '#app/utils/db.server.ts';
export async function loader({ request }: DataFunctionArgs) {
const users = await prisma.user.findMany({
select: {
id: true,
email: true,
firstName: true,
lastName: true,
company: {
select: {
name: true,
},
},
},
});
return json({ users });
}
type UserType = SerializeFrom<typeof loader>['users'][number];
export const columns: ColumnDef<UserType>[] = [
{
accessorKey: 'firstName',
header: 'First name',
},
{
accessorKey: 'lastName',
header: 'Last name',
},
{
accessorKey: 'email',
header: 'Email',
},
{
accessorKey: 'company',
header: 'Company name',
cell: ({ row }) => {
const company = row.getValue<UserType['company']>('company');
return <>{company?.name}</>;
},
},
{
id: 'actions',
cell: ({ row }) => {
const user = row.original;
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" className="h-8 w-8 p-0">
<span className="sr-only">Open menu</span>
<Icon name="DotsVerticalIcon" className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuLabel>Actions</DropdownMenuLabel>
<DropdownMenuItem>Impersonate</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem>Edit</DropdownMenuItem>
<DropdownMenuItem>Delete</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
);
},
},
];
export default function Users() {
const loaderData = useLoaderData<typeof loader>();
const table = useReactTable({
data: loaderData.users,
columns,
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(),
});
return (
<div className="container">
<h2 className="mt-4 text-h3">Users</h2>
<div className="mt-2 rounded-md border">
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef
.header,
header.getContext(),
)}
</TableHead>
);
})}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={
row.getIsSelected() && 'selected'
}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(
cell.column.columnDef.cell,
cell.getContext(),
)}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell
colSpan={columns.length}
className="h-24 text-center"
>
No results.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div>
<div className="mb-4 mt-2 flex items-center justify-between px-2">
<div className="flex-1 text-sm text-muted-foreground">
{table.getFilteredSelectedRowModel().rows.length} of{' '}
{table.getFilteredRowModel().rows.length} row(s) selected.
</div>
<div className="flex items-center space-x-6 lg:space-x-8">
<div className="flex items-center space-x-2">
<p className="text-sm font-medium">Rows per page</p>
<Select
value={`${table.getState().pagination.pageSize}`}
onValueChange={(value) => {
table.setPageSize(Number(value));
}}
>
<SelectTrigger className="h-8 w-[70px]">
<SelectValue
placeholder={
table.getState().pagination.pageSize
}
/>
</SelectTrigger>
<SelectContent side="top">
{[10, 20, 30, 40, 50].map((pageSize) => (
<SelectItem
key={pageSize}
value={`${pageSize}`}
>
{pageSize}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="flex w-[100px] items-center justify-center text-sm font-medium">
Page {table.getState().pagination.pageIndex + 1} of{' '}
{table.getPageCount()}
</div>
<div className="flex items-center space-x-2">
<Button
variant="outline"
className="hidden h-8 w-8 p-0 lg:flex"
onClick={() => table.setPageIndex(0)}
disabled={!table.getCanPreviousPage()}
>
<span className="sr-only">Go to first page</span>
<Icon name="DoubleArrowLeftIcon" size="4xs" />
</Button>
<Button
variant="outline"
className="h-8 w-8 p-0"
onClick={() => table.previousPage()}
disabled={!table.getCanPreviousPage()}
>
<span className="sr-only">Go to previous page</span>
<Icon name="ChevronLeftIcon" size="4xs" />
</Button>
<Button
variant="outline"
className="h-8 w-8 p-0"
onClick={() => table.nextPage()}
disabled={!table.getCanNextPage()}
>
<span className="sr-only">Go to next page</span>
<Icon name="ChevronRightIcon" size="4xs" />
</Button>
<Button
variant="outline"
className="hidden h-8 w-8 p-0 lg:flex"
onClick={() =>
table.setPageIndex(table.getPageCount() - 1)
}
disabled={!table.getCanNextPage()}
>
<span className="sr-only">Go to last page</span>
<Icon name="DoubleArrowRightIcon" size="4xs" />
</Button>
</div>
</div>
</div>
</div>
);
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I don't know if this help but in my case |
Beta Was this translation helpful? Give feedback.
-
@affeldt28 did you find a solution? Just noticed this issue. My issue was using the |
Beta Was this translation helpful? Give feedback.
@affeldt28 did you find a solution? Just noticed this issue.
My issue was using the
h-screen
tailwind class on a parent div.