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

feat:show table in mobile with hscroll #1273

Merged
merged 15 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
4 changes: 2 additions & 2 deletions app/components/dashboard/most-scanned-assets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const Row = ({
}) => (
<>
{/* Item */}
<Td className="w-full whitespace-normal p-0 md:p-0">
<Td className="w-full min-w-[130px] whitespace-normal p-0 md:p-0">
<div className="flex justify-between gap-3 px-4 py-3 md:justify-normal md:px-6">
<div className="flex items-center gap-3">
<div className="flex size-12 shrink-0 items-center justify-center">
Expand Down Expand Up @@ -106,6 +106,6 @@ const Row = ({
</Td>

{/* Category */}
<Td className="hidden md:table-cell">{item.scanCount} scans</Td>
<Td>{item.scanCount} scans</Td>
</>
);
2 changes: 1 addition & 1 deletion app/components/dashboard/most-scanned-categories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@ const Row = ({
</Td>

{/* Category */}
<Td className="hidden md:table-cell">{item.scanCount} scans</Td>
<Td>{item.scanCount} scans</Td>
</>
);
2 changes: 1 addition & 1 deletion app/components/dashboard/newest-assets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const Row = ({
</Td>

{/* Category */}
<Td className="hidden md:table-cell">
<Td>
{category ? (
<Badge color={category.color} withDot={false}>
{category.name}
Expand Down
2 changes: 1 addition & 1 deletion app/components/list/bulk-actions/bulk-list-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function BulkListHeader() {
}

return (
<Th className="table-cell md:pl-4 md:pr-3">
<Th className="md:pl-4 md:pr-3">
{partialItemsSelected ? (
<PartialCheckboxIcon
className="cursor-pointer"
Expand Down
5 changes: 1 addition & 4 deletions app/components/list/bulk-actions/bulk-list-item-checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ export default function BulkListItemCheckbox({
}

return (
<Td
className="table-cell md:pl-4 md:pr-3"
onClick={handleBulkItemSelection}
>
<Td className="md:pl-4 md:pr-3" onClick={handleBulkItemSelection}>
<FakeCheckbox
className={tw("text-white", checked ? "text-primary" : "")}
checked={checked}
Expand Down
2 changes: 1 addition & 1 deletion app/components/list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export const List = React.forwardRef<HTMLDivElement, ListProps>(function List(
<div
ref={ref}
className={tw(
"-mx-4 overflow-x-auto border border-gray-200 bg-white md:mx-0 md:rounded",
"-mx-4 overflow-auto border border-gray-200 bg-white md:mx-0 md:rounded",
className
)}
>
Expand Down
52 changes: 47 additions & 5 deletions app/components/table.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { TdHTMLAttributes } from "react";
import React from "react";
import React, { useEffect, useRef, useState } from "react";
import { tw } from "~/utils/tw";

export function Table({
Expand All @@ -9,10 +9,49 @@ export function Table({
children: React.ReactNode;
className?: string;
}) {
const containerRef = useRef<HTMLDivElement>(null);
const [isOverflowing, setIsOverflowing] = useState(false);

useEffect(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please abstract this into its own hook called useTableIsOverflowing which returns the value?

const checkOverflow = () => {
const container = containerRef.current;
if (container) {
const hasOverflow = container.scrollWidth > container.clientWidth;
const hasReachedEnd =
container.scrollLeft + container.clientWidth >= container.scrollWidth;
setIsOverflowing(hasOverflow && !hasReachedEnd);
}
};

checkOverflow(); // Initial check
window.addEventListener("resize", checkOverflow); // Check on resize
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to do this, inside the useEffect you should have a if (window)


// Ensure the scroll event listener is added correctly
const container = containerRef.current;
if (container) {
container.addEventListener("scroll", checkOverflow);
}

return () => {
window.removeEventListener("resize", checkOverflow);
if (container) {
container.removeEventListener("scroll", checkOverflow);
}
};
}, []);

return (
<table className={tw("w-full table-auto border-collapse", className)}>
{children}
</table>
<div className={`relative ${isOverflowing ? "overflowing" : ""}`}>
<div className="fixed-gradient"></div>
<div
ref={containerRef}
className="scrollbar-top scrollbar-always-visible"
>
<table className={tw("w-full table-auto border-collapse", className)}>
{children}
</table>
</div>
</div>
);
}

Expand Down Expand Up @@ -56,7 +95,10 @@ interface TdProps extends TdHTMLAttributes<HTMLTableCellElement> {
export function Td({ children, className, ...props }: TdProps) {
return (
<td
className={tw("whitespace-nowrap border-b p-4 md:px-6", className)}
className={tw(
"max-w-[200px] truncate whitespace-nowrap border-b p-4 md:px-6",
className
)}
{...props}
>
{children}
Expand Down
16 changes: 8 additions & 8 deletions app/routes/_layout+/admin-dashboard+/qrs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,16 @@ export default function Area51() {
hideFirstHeaderColumn
headerChildren={
<>
<Th className="hidden md:table-cell">QR id</Th>
<Th className="hidden md:table-cell">Asset</Th>
<Th className="hidden md:table-cell">Kit</Th>
<Th className="hidden md:table-cell">Organization ID</Th>
<Th className="hidden md:table-cell">User ID</Th>
<Th className="hidden md:table-cell">
<Th>QR id</Th>
<Th>Asset</Th>
<Th>Kit</Th>
<Th>Organization ID</Th>
<Th>User ID</Th>
<Th>
<span title="Only available for batched codes">Printed</span>
</Th>
<Th className="hidden md:table-cell">Batch</Th>
<Th className="hidden md:table-cell">Created at</Th>
<Th>Batch</Th>
<Th>Created at</Th>
</>
}
/>
Expand Down
23 changes: 8 additions & 15 deletions app/routes/_layout+/assets._index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -484,25 +484,24 @@ export const AssetsList = ({
* Using remix's navigate is the default behaviour, however it can receive also a custom function
*/
navigate={(itemId) => navigate(`/assets/${itemId}`)}
className=" overflow-x-visible md:overflow-x-auto"
bulkActions={disableBulkActions ? undefined : <BulkActionsDropdown />}
customEmptyStateContent={
customEmptyState ? customEmptyState : undefined
}
headerChildren={
<>
<Th className="hidden md:table-cell">Category</Th>
<Th className="hidden md:table-cell">Tags</Th>
<Th>Category</Th>
<Th>Tags</Th>
<When
truthy={userHasPermission({
roles,
entity: PermissionEntity.custody,
action: PermissionAction.read,
})}
>
<Th className="hidden md:table-cell">Custodian</Th>
<Th>Custodian</Th>
</When>
<Th className="hidden md:table-cell">Location</Th>
<Th>Location</Th>
</>
}
/>
Expand Down Expand Up @@ -580,15 +579,11 @@ const ListAssetContent = ({
</div>
</div>
</div>

<button className="block md:hidden">
<ChevronRight />
</button>
</div>
</Td>

{/* Category */}
<Td className="hidden md:table-cell">
<Td>
{category ? (
<Badge color={category.color} withDot={false}>
{category.name}
Expand All @@ -601,7 +596,7 @@ const ListAssetContent = ({
</Td>

{/* Tags */}
<Td className="hidden text-left md:table-cell">
<Td className="text-left">
<ListItemTagsColumn tags={tags} />
</Td>

Expand All @@ -613,7 +608,7 @@ const ListAssetContent = ({
action: PermissionAction.read,
})}
>
<Td className="hidden md:table-cell">
<Td>
{custody ? (
<GrayBadge>
<>
Expand Down Expand Up @@ -648,9 +643,7 @@ const ListAssetContent = ({
</When>

{/* Location */}
<Td className="hidden md:table-cell">
{location?.name ? <GrayBadge>{location.name}</GrayBadge> : null}
</Td>
<Td>{location?.name ? <GrayBadge>{location.name}</GrayBadge> : null}</Td>
</>
);
};
Expand Down
29 changes: 14 additions & 15 deletions app/routes/_layout+/bookings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,15 @@ export default function BookingsIndexPage({
bulkActions={disableBulkActions ? undefined : <BulkActionsDropdown />}
ItemComponent={ListAssetContent}
navigate={(id) => navigate(`/bookings/${id}`)}
className="overflow-x-visible md:overflow-x-auto"
headerChildren={
<>
<Th className="hidden md:table-cell" />
<Th className="hidden md:table-cell">Description</Th>
<Th />
<Th>Description</Th>

<Th className="hidden md:table-cell">From</Th>
<Th className="hidden md:table-cell">To</Th>
<Th className="hidden md:table-cell">Custodian</Th>
<Th className="hidden md:table-cell">Created by</Th>
<Th>From</Th>
<Th>To</Th>
<Th>Custodian</Th>
<Th>Created by</Th>
</>
}
/>
Expand Down Expand Up @@ -302,9 +301,9 @@ const ListAssetContent = ({
</div>
</div>

<button className="block md:hidden">
{/* <button className="block md:hidden">
<ChevronRight />
</button>
</button> */}
</div>
</Td>

Expand All @@ -314,7 +313,7 @@ const ListAssetContent = ({
* 2. Have custody
* 3. Have other bookings with the same period - this I am not sure how to handle yet
* */}
<Td className="hidden md:table-cell">
<Td>
{hasUnavaiableAssets ? (
<AvailabilityBadge
badgeText={"Includes unavailable assets"}
Expand All @@ -326,12 +325,12 @@ const ListAssetContent = ({
) : null}
</Td>

<Td className="hidden md:table-cell">
<Td className="max-w-62">
{item.description ? <LineBreakText text={item.description} /> : null}
</Td>

{/* From */}
<Td className="hidden md:table-cell">
<Td>
{item.displayFrom ? (
<div className="min-w-[130px]">
<span className="word-break mb-1 block font-medium">
Expand All @@ -343,7 +342,7 @@ const ListAssetContent = ({
</Td>

{/* To */}
<Td className="hidden md:table-cell">
<Td>
{item.displayTo ? (
<div className="min-w-[130px]">
<span className="word-break mb-1 block font-medium">
Expand All @@ -355,7 +354,7 @@ const ListAssetContent = ({
</Td>

{/* Custodian */}
<Td className="hidden md:table-cell">
<Td>
{item?.custodianUser ? (
<UserBadge
img={
Expand All @@ -372,7 +371,7 @@ const ListAssetContent = ({
</Td>

{/* Created by */}
<Td className="hidden md:table-cell">
<Td>
<UserBadge
img={
item?.creator?.profilePicture || "/static/images/default_pfp.jpg"
Expand Down
9 changes: 5 additions & 4 deletions app/routes/_layout+/categories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { DeleteCategory } from "~/components/category/delete-category";
import { ErrorContent } from "~/components/errors";
import Header from "~/components/layout/header";
import type { HeaderData } from "~/components/layout/header/types";
import LineBreakText from "~/components/layout/line-break-text";
import { List } from "~/components/list";
import { ListContentWrapper } from "~/components/list/content-wrapper";
import { Filters } from "~/components/list/filters";
Expand Down Expand Up @@ -166,9 +167,9 @@ export default function CategoriesPage() {
ItemComponent={CategoryItem}
headerChildren={
<>
<Th className="hidden md:table-cell">Description</Th>
<Th className="hidden md:table-cell">Assets</Th>
<Th className="hidden md:table-cell">Actions</Th>
<Th>Description</Th>
<Th>Assets</Th>
<Th>Actions</Th>
</>
}
/>
Expand All @@ -192,7 +193,7 @@ const CategoryItem = ({
{item.name}
</Badge>
</Td>
<Td className="w-3/4 text-gray-500" title="Description">
<Td className="text-gray-500 md:w-3/4" title="Description">
{item.description}
</Td>
<Td>{item._count.assets}</Td>
Expand Down
8 changes: 4 additions & 4 deletions app/routes/_layout+/kits.$kitId.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,8 @@ export default function KitDetails() {
}}
headerChildren={
<>
<Th className="hidden md:table-cell">Category</Th>
<Th className="hidden md:table-cell">Location</Th>
<Th>Category</Th>
<Th>Location</Th>
</>
}
/>
Expand Down Expand Up @@ -606,15 +606,15 @@ function ListContent({
</div>
</Td>

<Td className="hidden md:table-cell">
<Td>
{category ? (
<Badge color={category.color} withDot={false}>
{category.name}
</Badge>
) : null}
</Td>

<Td className="hidden md:table-cell">
<Td>
{location ? (
<GrayBadge>
{location.image ? (
Expand Down
Loading
Loading