Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,77 @@
|
||||
import { Menu } from "@headlessui/react";
|
||||
import clsx from "clsx";
|
||||
import { ReactNode, useEffect, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import DropdownOptions from "../dropdowns/DropdownOptions";
|
||||
import OpenCloseArrowIcon from "../icons/OpenCloseArrowIcon";
|
||||
|
||||
export interface RefSimpleRow {}
|
||||
|
||||
interface Props {
|
||||
value: ReactNode;
|
||||
title: ReactNode;
|
||||
children: ReactNode;
|
||||
onRemove?: () => void;
|
||||
initial?: boolean;
|
||||
className?: string;
|
||||
disabled?: boolean;
|
||||
draggable?: boolean;
|
||||
opened?: boolean;
|
||||
}
|
||||
|
||||
export default function CollapsibleRow({ value, title, children, onRemove, initial = false, className, disabled, draggable, opened }: Props) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const [open, setOpen] = useState(initial);
|
||||
|
||||
useEffect(() => {
|
||||
if (opened !== undefined) {
|
||||
setOpen(opened);
|
||||
}
|
||||
}, [opened]);
|
||||
|
||||
return (
|
||||
<div
|
||||
className={clsx(
|
||||
className,
|
||||
"border-border-default bg-surface-neutral-primary rounded-md border-2 border-dashed p-2",
|
||||
draggable && "hover:bg-surface-neutral-secondary hover:border-border-strong hover:cursor-move hover:border-dashed"
|
||||
)}
|
||||
>
|
||||
<div className="flex items-center justify-between space-x-2">
|
||||
<button type="button" onClick={() => setOpen(!open)} className={clsx("w-full truncate text-left text-sm", draggable && "hover:cursor-move")}>
|
||||
{!open ? <span className="text-text-neutral-tertiary">{value ?? t("shared.empty")}</span> : <span className="text-text-neutral-primary font-medium">{title}</span>}
|
||||
</button>
|
||||
<div className="flex shrink-0 items-center space-x-2">
|
||||
{onRemove && (
|
||||
<DropdownOptions
|
||||
options={
|
||||
<div>
|
||||
<Menu.Item>
|
||||
{({ active }) => (
|
||||
<button
|
||||
type="button"
|
||||
onClick={onRemove}
|
||||
disabled={disabled}
|
||||
className={clsx(
|
||||
"w-full text-left",
|
||||
active ? "text-text-neutral-primary bg-surface-neutral-secondary" : "text-text-neutral-secondary",
|
||||
"block px-4 py-2 text-sm",
|
||||
disabled && "cursor-not-allowed"
|
||||
)}
|
||||
>
|
||||
{t("shared.remove")}
|
||||
</button>
|
||||
)}
|
||||
</Menu.Item>
|
||||
</div>
|
||||
}
|
||||
></DropdownOptions>
|
||||
)}
|
||||
<OpenCloseArrowIcon open={open} setOpen={setOpen} />
|
||||
</div>
|
||||
</div>
|
||||
{open && <div>{children}</div>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
import { Transition } from "@headlessui/react";
|
||||
import clsx from "clsx";
|
||||
import { Fragment, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Form } from "react-router";
|
||||
import { ColumnDto } from "~/application/dtos/data/ColumnDto";
|
||||
import { useOuterClick } from "~/utils/shared/KeypressUtils";
|
||||
import { updateItemByIdx } from "~/utils/shared/ObjectUtils";
|
||||
import DotsHorizontalFilledIcon from "../icons/DotsHorizontalFilledIcon";
|
||||
import InputCheckboxInline from "../input/InputCheckboxInline";
|
||||
import { translateLabel } from "~/utils/i18n/translateLabel";
|
||||
|
||||
interface Props {
|
||||
items: ColumnDto[];
|
||||
setItems: React.Dispatch<React.SetStateAction<ColumnDto[]>>;
|
||||
onClear: () => void;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export default function ColumnSelector({ items, setItems, onClear, className }: Props) {
|
||||
const { t, i18n } = useTranslation();
|
||||
|
||||
const [opened, setOpened] = useState(false);
|
||||
|
||||
const clickOutside = useOuterClick(() => setOpened(false));
|
||||
|
||||
return (
|
||||
<div ref={clickOutside} className={clsx("relative", className)}>
|
||||
<button
|
||||
type="button"
|
||||
aria-label={t("shared.columns")}
|
||||
onClick={() => setOpened(!opened)}
|
||||
className="hover:bg-surface-neutral-secondary focus:border-border-default focus:shadow-focus relative z-0 inline-flex rounded-md text-sm shadow-2xs focus:z-10 focus:outline-hidden"
|
||||
>
|
||||
<span
|
||||
className={clsx(
|
||||
"border-border-default text-text-neutral-tertiary bg-surface-neutral-primary relative inline-flex items-center space-x-2 rounded-md border px-2 py-3 font-medium"
|
||||
)}
|
||||
>
|
||||
<div>
|
||||
<DotsHorizontalFilledIcon className="h-3 w-3" />
|
||||
</div>
|
||||
</span>
|
||||
</button>
|
||||
|
||||
<Transition
|
||||
as={Fragment}
|
||||
show={opened}
|
||||
enter="transition ease-out duration-100"
|
||||
enterFrom="transform opacity-0 scale-95"
|
||||
enterTo="transform opacity-100 scale-100"
|
||||
leave="transition ease-in duration-75"
|
||||
leaveFrom="transform opacity-100 scale-100"
|
||||
leaveTo="transform opacity-0 scale-95"
|
||||
>
|
||||
<Form
|
||||
method="get"
|
||||
className="divide-border-default bg-surface-neutral-primary absolute right-0 z-40 mt-2 w-64 origin-top-right divide-y overflow-visible rounded-md shadow-2xl ring-1 ring-black ring-opacity-5 focus:outline-hidden"
|
||||
>
|
||||
<div className="bg-surface-neutral-secondary flex items-center justify-between px-2 py-2 text-sm">
|
||||
<div className="font-bold">{t("shared.columns")}</div>
|
||||
</div>
|
||||
<div className="divide-border-default divide-y text-sm">
|
||||
{items.map((filter, idx) => {
|
||||
return (
|
||||
<div key={filter.name} className="divide-border-default divide-y">
|
||||
<div className="divide-y divide-gray-300 px-2 py-2">
|
||||
<InputCheckboxInline
|
||||
name={filter.name}
|
||||
title={translateLabel(t, i18n, filter.title)}
|
||||
value={filter.visible}
|
||||
setValue={(e) => {
|
||||
updateItemByIdx(items, setItems, idx, {
|
||||
visible: Boolean(e),
|
||||
});
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</Form>
|
||||
</Transition>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
import { FormEvent, useEffect, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useSearchParams, useNavigation } from "react-router";
|
||||
import Modal from "../modals/Modal";
|
||||
import Constants from "~/application/Constants";
|
||||
import clsx from "clsx";
|
||||
import InputSelector from "../input/InputSelector";
|
||||
import InputNumber from "../input/InputNumber";
|
||||
import ButtonPrimary from "../buttons/ButtonPrimary";
|
||||
import ButtonSecondary from "../buttons/ButtonSecondary";
|
||||
import { useLocaleFormatters } from "~/locale/format";
|
||||
import { ChevronLeftIcon, ChevronRightIcon } from "~/components/ui/icons/vendor";
|
||||
|
||||
interface Props {
|
||||
page: number;
|
||||
pageSize: number;
|
||||
totalItems: number;
|
||||
totalPages: number;
|
||||
defaultPageSize?: number;
|
||||
}
|
||||
export default function GridPagination({ page, pageSize, totalItems, totalPages, defaultPageSize = Constants.DEFAULT_PAGE_SIZE }: Props) {
|
||||
const { t } = useTranslation();
|
||||
const formatters = useLocaleFormatters();
|
||||
const [state, setState] = useState<{ page: number; pageSize: number | undefined }>({ page, pageSize });
|
||||
const [from, setFrom] = useState(0);
|
||||
const [to, setTo] = useState(0);
|
||||
// const submit = useSubmit();
|
||||
const navigation = useNavigation();
|
||||
const loading = navigation.state === "loading" || navigation.state === "submitting";
|
||||
const [searchParams, setSearchParams] = useSearchParams();
|
||||
|
||||
const [showPageSizeModal, setShowPageSizeModal] = useState(false);
|
||||
const [showPageNumberModal, setShowPageNumberModal] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
setFrom(page * pageSize - pageSize + 1);
|
||||
|
||||
let to = page * pageSize;
|
||||
if (to > totalItems) {
|
||||
to = totalItems;
|
||||
}
|
||||
setTo(to);
|
||||
}, [page, pageSize, totalItems]);
|
||||
|
||||
useEffect(() => {
|
||||
const pageSizeParam = searchParams.get("pageSize")?.toString() ?? defaultPageSize.toString();
|
||||
const pageParam = searchParams.get("page")?.toString() ?? "1";
|
||||
if (state.page.toString() !== pageParam || state.pageSize?.toString() !== pageSizeParam) {
|
||||
if (state.page.toString() !== pageParam) {
|
||||
searchParams.set("page", state.page.toString());
|
||||
}
|
||||
if (state.pageSize?.toString() !== pageSizeParam) {
|
||||
if (state.pageSize) {
|
||||
searchParams.set("pageSize", state.pageSize.toString());
|
||||
} else {
|
||||
searchParams.delete("pageSize");
|
||||
}
|
||||
}
|
||||
setSearchParams(searchParams);
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [state]);
|
||||
|
||||
return (
|
||||
<div className="text-text-neutral-tertiary flex items-center justify-end text-xs uppercase tracking-wide sm:justify-between" aria-label={t("components.pagination.ariaLabel")}>
|
||||
<div className="hidden sm:block">
|
||||
{totalItems > 0 && (
|
||||
<button type="button" onClick={() => setShowPageSizeModal(true)} className="uppercase hover:underline">
|
||||
{t("components.pagination.summary", {
|
||||
count: totalItems,
|
||||
from: formatters.number(totalItems === 0 ? 0 : from),
|
||||
to: formatters.number(to),
|
||||
total: formatters.number(totalItems),
|
||||
})}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
<div className="inline-flex items-center justify-center gap-2">
|
||||
<button
|
||||
type="button"
|
||||
disabled={page === 1 || loading}
|
||||
onClick={() => setState({ ...state, page: page - 1 })}
|
||||
className={clsx(
|
||||
"border-border-default bg-surface-neutral-primary inline-flex h-8 w-8 items-center justify-center rounded border focus:outline-hidden focus:shadow-focus",
|
||||
page === 1 || loading ? "cursor-not-allowed opacity-90" : "hover:bg-surface-neutral-secondary hover:border-border-default"
|
||||
)}
|
||||
>
|
||||
<ChevronLeftIcon className="h-3 w-3" />
|
||||
</button>
|
||||
|
||||
{showPageNumberModal ? (
|
||||
<input
|
||||
type="number"
|
||||
min={1}
|
||||
max={totalPages}
|
||||
disabled={totalPages <= 1}
|
||||
value={state.page}
|
||||
onChange={(e) => setState({ ...state, page: parseInt(e.target.value) })}
|
||||
onBlur={() => setShowPageNumberModal(false)}
|
||||
className="focus:border-border-default focus:shadow-focus border-border-default block w-full min-w-0 flex-1 rounded text-xs"
|
||||
/>
|
||||
) : (
|
||||
<button type="button" onClick={() => setShowPageSizeModal(true)} className={clsx("uppercase hover:underline")}>
|
||||
{formatters.number(page)}
|
||||
<span className="mx-0.25">/</span>
|
||||
{formatters.number(totalPages === 0 ? 1 : totalPages)}
|
||||
</button>
|
||||
)}
|
||||
|
||||
<button
|
||||
type="button"
|
||||
className={clsx(
|
||||
"border-border-default bg-surface-neutral-primary inline-flex h-8 w-8 items-center justify-center rounded border focus:outline-hidden focus:shadow-focus",
|
||||
page >= totalPages || loading ? "cursor-not-allowed opacity-90" : "hover:bg-surface-neutral-secondary hover:border-border-default"
|
||||
)}
|
||||
disabled={page >= totalPages || loading}
|
||||
onClick={() => setState({ ...state, page: page + 1 })}
|
||||
>
|
||||
<ChevronRightIcon className="h-3 w-3" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<Modal className="sm:max-w-xs" open={showPageSizeModal} setOpen={setShowPageSizeModal}>
|
||||
<PageOptionsForm
|
||||
page={state.page}
|
||||
pageSize={state.pageSize}
|
||||
totalItems={totalItems}
|
||||
onChange={(e) => {
|
||||
setState({
|
||||
page: e.currentPage,
|
||||
pageSize: e.pageSize,
|
||||
});
|
||||
setShowPageSizeModal(false);
|
||||
}}
|
||||
/>
|
||||
</Modal>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function PageOptionsForm({
|
||||
page,
|
||||
pageSize,
|
||||
onChange,
|
||||
totalItems,
|
||||
}: {
|
||||
page: number;
|
||||
pageSize?: number;
|
||||
totalItems?: number;
|
||||
onChange: (state: { currentPage: number; pageSize?: number }) => void;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const formatters = useLocaleFormatters();
|
||||
const [totalPages, setTotalPages] = useState(0);
|
||||
const [state, setState] = useState({ currentPage: page, pageSize: pageSize });
|
||||
|
||||
useEffect(() => {
|
||||
if (totalItems) {
|
||||
setTotalPages(Math.ceil(totalItems / (state.pageSize || Constants.DEFAULT_PAGE_SIZE)));
|
||||
}
|
||||
}, [state.pageSize, totalItems]);
|
||||
|
||||
function handleSubmit(e: FormEvent<HTMLFormElement>) {
|
||||
e.stopPropagation();
|
||||
e.preventDefault();
|
||||
onChange(state);
|
||||
}
|
||||
function onReset() {
|
||||
onChange({ currentPage: 1, pageSize: undefined });
|
||||
}
|
||||
function existingChanges() {
|
||||
return state.pageSize !== Constants.DEFAULT_PAGE_SIZE || state.currentPage !== 1;
|
||||
}
|
||||
return (
|
||||
<form onSubmit={handleSubmit} className="space-y-2">
|
||||
<div>
|
||||
<InputSelector
|
||||
withSearch={false}
|
||||
title={t("shared.pageSize")}
|
||||
value={state.pageSize}
|
||||
setValue={(e) => setState({ ...state, pageSize: Number(e) })}
|
||||
options={[undefined, ...Constants.PAGE_SIZE_OPTIONS].map((size) => {
|
||||
return {
|
||||
value: size,
|
||||
name: size === undefined ? t("shared.default") : formatters.number(size),
|
||||
};
|
||||
})}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<InputNumber
|
||||
title={t("shared.page")}
|
||||
hint={
|
||||
<div className="text-text-neutral-tertiary">
|
||||
{t("shared.totalPages")}: {formatters.number(totalPages)}
|
||||
</div>
|
||||
}
|
||||
value={state.currentPage}
|
||||
min={1}
|
||||
max={totalPages}
|
||||
setValue={(e) => setState({ ...state, currentPage: Number(e) })}
|
||||
/>
|
||||
</div>
|
||||
<div className="flex justify-between space-x-2">
|
||||
<ButtonSecondary disabled={!existingChanges()} onClick={onReset} className="flex justify-center text-center">
|
||||
{t("shared.reset")}
|
||||
</ButtonSecondary>
|
||||
<ButtonPrimary type="submit" className="flex justify-center text-center">
|
||||
{t("shared.confirm")}
|
||||
</ButtonPrimary>
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
// function PageSize({ number, onSelected }: { number?: number; onSelected: () => void }) {
|
||||
// const { t } = useTranslation();
|
||||
// const [searchParams] = useSearchParams();
|
||||
// return (
|
||||
// <button onClick={onSelected} className="w-full bg-background p-2 text-center text-sm text-muted-foreground hover:bg-secondary hover:text-foreground">
|
||||
// <div className="flex items-center justify-center space-x-2">
|
||||
// {number === undefined ? <div className="text-muted-foreground">{t("shared.default")}</div> : <div>{number}</div>}
|
||||
// <div>
|
||||
// {searchParams.get("pageSize") === number?.toString() || (number === undefined && !searchParams.get("pageSize")) ? (
|
||||
// <CheckIcon className="h-3 w-3 text-teal-500" />
|
||||
// ) : null}
|
||||
// </div>
|
||||
// </div>
|
||||
// </button>
|
||||
// );
|
||||
// }
|
||||
@@ -0,0 +1,66 @@
|
||||
import { t as i18nT } from "i18next";
|
||||
import { useLocaleFormatters } from "~/locale/format";
|
||||
import TableSimple from "./TableSimple";
|
||||
|
||||
const items = [
|
||||
{
|
||||
firstName: "John",
|
||||
lastName: "Doe",
|
||||
email: "john.doe@company.com",
|
||||
salary: 100,
|
||||
birthday: new Date(1990, 1, 1),
|
||||
},
|
||||
{
|
||||
firstName: "Luna",
|
||||
lastName: "Davis",
|
||||
email: "luna.davis@company.com",
|
||||
salary: 100.5,
|
||||
birthday: new Date(1990, 12, 31),
|
||||
},
|
||||
];
|
||||
export default function PreviewTableSimple() {
|
||||
const formatters = useLocaleFormatters();
|
||||
return (
|
||||
<div id="buttons">
|
||||
<div className="not-prose border-border bg-background border border-dashed p-6">
|
||||
<div className="w-full space-y-2">
|
||||
<TableSimple
|
||||
items={items}
|
||||
headers={[
|
||||
{
|
||||
title: i18nT("components.tables.preview.fullName"),
|
||||
name: "name",
|
||||
value: (item) => `${item.firstName} ${item.lastName}`,
|
||||
},
|
||||
{
|
||||
title: i18nT("components.tables.preview.email"),
|
||||
name: "email",
|
||||
value: (item) => item.email,
|
||||
},
|
||||
{
|
||||
title: i18nT("components.tables.preview.salary"),
|
||||
name: "salary",
|
||||
className: "text-blue-500",
|
||||
value: (item) => item.salary,
|
||||
formattedValue: (item) => formatters.number(item.salary, { minimumFractionDigits: 2, maximumFractionDigits: 2 }),
|
||||
},
|
||||
{
|
||||
title: i18nT("components.tables.preview.birthday"),
|
||||
name: "birthday",
|
||||
className: "text-muted-foreground",
|
||||
value: (item) => item.birthday,
|
||||
formattedValue: (item) => formatters.date(item.birthday),
|
||||
},
|
||||
]}
|
||||
actions={[
|
||||
{
|
||||
title: i18nT("components.tables.preview.view"),
|
||||
onClick: (_, item) => alert(i18nT("components.tables.preview.clicked", { name: item.firstName })),
|
||||
},
|
||||
]}
|
||||
></TableSimple>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import { useSearchParams } from "react-router";
|
||||
import { Fragment } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import Constants from "~/application/Constants";
|
||||
import type { PaginationDto } from "~/application/dtos/data/PaginationDto";
|
||||
import type { EntityViewWithDetails } from "~/utils/db/entities/entityViews.db.server";
|
||||
|
||||
export default function RowsLoadMoreCard({
|
||||
pagination,
|
||||
currentView,
|
||||
className,
|
||||
}: {
|
||||
pagination?: PaginationDto;
|
||||
currentView?: EntityViewWithDetails | null;
|
||||
className?: string;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const [searchParams, setSearchParams] = useSearchParams();
|
||||
function theresMore() {
|
||||
if (!pagination) {
|
||||
return false;
|
||||
}
|
||||
return pagination.totalItems > pagination.page * pagination.pageSize;
|
||||
}
|
||||
return (
|
||||
<Fragment>
|
||||
{theresMore() && (
|
||||
<div className={className}>
|
||||
<button
|
||||
type="button"
|
||||
className="border-border-default hover:border-border-default group inline-block h-full w-full truncate rounded-md border-2 border-dashed p-4 text-left align-middle shadow-2xs hover:border-dotted hover:bg-surface-neutral-secondary"
|
||||
onClick={() => {
|
||||
if (!pagination) {
|
||||
return;
|
||||
}
|
||||
let currentPageSize = 0;
|
||||
const paramsPageSize = searchParams.get("pageSize") ? parseInt(searchParams.get("pageSize") ?? "") : undefined;
|
||||
if (paramsPageSize) {
|
||||
currentPageSize = paramsPageSize;
|
||||
} else {
|
||||
currentPageSize = pagination.pageSize;
|
||||
}
|
||||
let currentViewPageSize = currentView ? currentView.pageSize : 0;
|
||||
if (currentViewPageSize === 0) {
|
||||
currentViewPageSize = Constants.DEFAULT_PAGE_SIZE;
|
||||
}
|
||||
const pageSize = currentPageSize + currentViewPageSize;
|
||||
searchParams.set("pageSize", pageSize.toString());
|
||||
setSearchParams(searchParams);
|
||||
}}
|
||||
>
|
||||
<div className="text-text-neutral-secondary mx-auto flex justify-center text-center align-middle text-sm font-medium">{t("shared.loadMore")}</div>
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</Fragment>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import { PaginationDto } from "~/application/dtos/data/PaginationDto";
|
||||
import { RowHeaderDisplayDto } from "~/application/dtos/data/RowHeaderDisplayDto";
|
||||
import { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow } from "~/components/ui/table";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import TablePagination from "./TablePagination";
|
||||
import { cn } from "~/lib/utils";
|
||||
import { translateLabel } from "~/utils/i18n/translateLabel";
|
||||
|
||||
interface Props<T> {
|
||||
headers: RowHeaderDisplayDto<T>[];
|
||||
items: T[];
|
||||
pagination?: PaginationDto;
|
||||
onClickRoute?: (idx: number, item: T) => string | undefined;
|
||||
onClick?: (idx: number, item: T) => void;
|
||||
onClickHeader?: (header: RowHeaderDisplayDto<T>) => void;
|
||||
selectedRows?: T[];
|
||||
onSelected?: (item: T[]) => void;
|
||||
className?: (idx: number, item: T) => string;
|
||||
padding?: string;
|
||||
containerClassName?: string;
|
||||
}
|
||||
|
||||
export default function TableNew<T>({ headers, items, pagination, containerClassName }: Props<T>) {
|
||||
const { t, i18n } = useTranslation();
|
||||
return (
|
||||
<div className={cn("border-border-default overflow-hidden rounded-lg border", containerClassName)}>
|
||||
<Table className="bg-surface-neutral-primary overflow-visible">
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
{headers.map((header) => (
|
||||
// Inherit Subframe header layout (h-10, px-4, extra-small medium typo,
|
||||
// per-cell divider) from the shadcn TableHead primitive — no inline
|
||||
// height/typography override here so the Subframe measures apply.
|
||||
<TableHead key={header.title}>{translateLabel(t, i18n, header.title)}</TableHead>
|
||||
))}
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody className="">
|
||||
{items.map((item, idx) => {
|
||||
return (
|
||||
<TableRow key={idx}>
|
||||
{headers.map((header) => (
|
||||
<TableCell key={header.title}>{header.value(item, idx)}</TableCell>
|
||||
))}
|
||||
</TableRow>
|
||||
);
|
||||
})}
|
||||
</TableBody>
|
||||
|
||||
{items.length === 0 && (
|
||||
<TableFooter>
|
||||
<TableRow>
|
||||
<TableCell colSpan={12} className="text-text-neutral-tertiary bg-surface-neutral-primary p-3 text-center text-sm font-normal">
|
||||
{t("shared.noRecords")}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
</TableFooter>
|
||||
)}
|
||||
</Table>
|
||||
|
||||
{pagination && <TablePagination {...pagination} />}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,256 @@
|
||||
import { FormEvent, useEffect, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useSearchParams, useNavigation } from "react-router";
|
||||
import Modal from "../modals/Modal";
|
||||
import Constants from "~/application/Constants";
|
||||
import clsx from "clsx";
|
||||
import InputSelector from "../input/InputSelector";
|
||||
import InputNumber from "../input/InputNumber";
|
||||
import ButtonPrimary from "../buttons/ButtonPrimary";
|
||||
import ButtonSecondary from "../buttons/ButtonSecondary";
|
||||
import { useLocaleFormatters } from "~/locale/format";
|
||||
import { ChevronLeftIcon, ChevronRightIcon } from "~/components/ui/icons/vendor";
|
||||
|
||||
interface Props {
|
||||
page: number;
|
||||
pageSize: number;
|
||||
totalItems: number;
|
||||
totalPages: number;
|
||||
}
|
||||
export default function TablePagination({ page, pageSize, totalItems, totalPages }: Props) {
|
||||
const { t } = useTranslation();
|
||||
const formatters = useLocaleFormatters();
|
||||
const [state, setState] = useState<{ page: number; pageSize: number | undefined }>({ page, pageSize });
|
||||
const [from, setFrom] = useState(0);
|
||||
const [to, setTo] = useState(0);
|
||||
// const submit = useSubmit();
|
||||
const navigation = useNavigation();
|
||||
const loading = navigation.state === "loading" || navigation.state === "submitting";
|
||||
const [searchParams, setSearchParams] = useSearchParams();
|
||||
|
||||
const [showPageSizeModal, setShowPageSizeModal] = useState(false);
|
||||
const [showPageNumberModal, setShowPageNumberModal] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
setFrom(page * pageSize - pageSize + 1);
|
||||
|
||||
let to = page * pageSize;
|
||||
if (to > totalItems) {
|
||||
to = totalItems;
|
||||
}
|
||||
setTo(to);
|
||||
}, [page, pageSize, totalItems]);
|
||||
|
||||
useEffect(() => {
|
||||
const pageSizeParam = searchParams.get("pageSize")?.toString() ?? Constants.DEFAULT_PAGE_SIZE.toString();
|
||||
const pageParam = searchParams.get("page")?.toString() ?? "1";
|
||||
if (state.page.toString() !== pageParam || state.pageSize?.toString() !== pageSizeParam) {
|
||||
if (state.page.toString() !== pageParam) {
|
||||
searchParams.set("page", state.page.toString());
|
||||
}
|
||||
if (state.pageSize?.toString() !== pageSizeParam) {
|
||||
if (state.pageSize) {
|
||||
searchParams.set("pageSize", state.pageSize.toString());
|
||||
} else {
|
||||
searchParams.delete("pageSize");
|
||||
}
|
||||
}
|
||||
setSearchParams(searchParams);
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [state]);
|
||||
|
||||
return (
|
||||
<div
|
||||
className="text-text-neutral-tertiary bg-surface-neutral-secondary flex items-center justify-end border-t px-4 py-3 text-xs uppercase tracking-wide sm:justify-between"
|
||||
aria-label={t("components.pagination.ariaLabel")}
|
||||
>
|
||||
<div className="hidden sm:block">
|
||||
{totalItems > 0 && (
|
||||
<button type="button" onClick={() => setShowPageSizeModal(true)} className="text-left uppercase hover:underline">
|
||||
{t("components.pagination.summary", {
|
||||
count: totalItems,
|
||||
from: formatters.number(totalItems === 0 ? 0 : from),
|
||||
to: formatters.number(to),
|
||||
total: formatters.number(totalItems),
|
||||
})}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex items-center space-x-2">
|
||||
<select
|
||||
id="pageSize"
|
||||
name="pageSize"
|
||||
aria-label={t("shared.perPage")}
|
||||
className={clsx(
|
||||
"focus:border-border-default focus:shadow-focus border-border-default bg-surface-neutral-primary inline-flex items-center justify-center rounded border text-xs",
|
||||
page === 1 || loading ? "cursor-not-allowed opacity-90" : "hover:bg-surface-neutral-secondary hover:border-border-default"
|
||||
)}
|
||||
onChange={(e) => setState({ ...state, page: 1, pageSize: Number(e.target.value) })}
|
||||
value={state.pageSize}
|
||||
>
|
||||
{[undefined, ...Constants.PAGE_SIZE_OPTIONS].map((f, idx) => {
|
||||
return (
|
||||
<option key={idx} value={Number(f ?? Constants.DEFAULT_PAGE_SIZE)} className="lowercase">
|
||||
{formatters.number(f === undefined ? Constants.DEFAULT_PAGE_SIZE : f)} {t("shared.perPage")?.toLowerCase()}
|
||||
</option>
|
||||
);
|
||||
})}
|
||||
</select>
|
||||
<div className="inline-flex items-center justify-center gap-2">
|
||||
<button
|
||||
type="button"
|
||||
aria-label={t("components.pagination.previousPage")}
|
||||
disabled={page === 1 || loading}
|
||||
onClick={() => setState({ ...state, page: page - 1 })}
|
||||
className={clsx(
|
||||
"border-border-default bg-surface-neutral-primary focus:shadow-focus inline-flex h-8 w-8 items-center justify-center rounded border focus:outline-hidden",
|
||||
page === 1 || loading ? "cursor-not-allowed opacity-90" : "hover:bg-surface-neutral-secondary hover:border-border-default"
|
||||
)}
|
||||
>
|
||||
<ChevronLeftIcon className="h-3 w-3" />
|
||||
</button>
|
||||
|
||||
{showPageNumberModal ? (
|
||||
<input
|
||||
type="number"
|
||||
min={1}
|
||||
max={totalPages}
|
||||
disabled={totalPages <= 1}
|
||||
value={state.page}
|
||||
onChange={(e) => setState({ ...state, page: parseInt(e.target.value) })}
|
||||
onBlur={() => setShowPageNumberModal(false)}
|
||||
className="focus:border-border-default focus:shadow-focus border-border-default block w-full min-w-0 flex-1 rounded text-xs"
|
||||
/>
|
||||
) : (
|
||||
<button type="button" onClick={() => setShowPageSizeModal(true)} className={clsx("text-left uppercase hover:underline")}>
|
||||
{formatters.number(page)}
|
||||
<span className="mx-0.25">/</span>
|
||||
{formatters.number(totalPages === 0 ? 1 : totalPages)}
|
||||
</button>
|
||||
)}
|
||||
|
||||
<button
|
||||
type="button"
|
||||
aria-label={t("components.pagination.nextPage")}
|
||||
className={clsx(
|
||||
"border-border-default bg-surface-neutral-primary focus:shadow-focus inline-flex h-8 w-8 items-center justify-center rounded border focus:outline-hidden",
|
||||
page >= totalPages || loading ? "cursor-not-allowed opacity-90" : "hover:bg-surface-neutral-secondary hover:border-border-default"
|
||||
)}
|
||||
disabled={page >= totalPages || loading}
|
||||
onClick={() => setState({ ...state, page: page + 1 })}
|
||||
>
|
||||
<ChevronRightIcon className="h-3 w-3" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Modal className="sm:max-w-xs" open={showPageSizeModal} setOpen={setShowPageSizeModal}>
|
||||
<PageOptionsForm
|
||||
page={state.page}
|
||||
pageSize={state.pageSize}
|
||||
totalItems={totalItems}
|
||||
onChange={(e) => {
|
||||
setState({
|
||||
page: e.currentPage,
|
||||
pageSize: e.pageSize,
|
||||
});
|
||||
setShowPageSizeModal(false);
|
||||
}}
|
||||
/>
|
||||
</Modal>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function PageOptionsForm({
|
||||
page,
|
||||
pageSize,
|
||||
onChange,
|
||||
totalItems,
|
||||
}: {
|
||||
page: number;
|
||||
pageSize?: number;
|
||||
totalItems?: number;
|
||||
onChange: (state: { currentPage: number; pageSize?: number }) => void;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const formatters = useLocaleFormatters();
|
||||
const [totalPages, setTotalPages] = useState(0);
|
||||
const [state, setState] = useState({ currentPage: page, pageSize: pageSize });
|
||||
|
||||
useEffect(() => {
|
||||
if (totalItems) {
|
||||
setTotalPages(Math.ceil(totalItems / (state.pageSize || Constants.DEFAULT_PAGE_SIZE)));
|
||||
}
|
||||
}, [state.pageSize, totalItems]);
|
||||
|
||||
function handleSubmit(e: FormEvent<HTMLFormElement>) {
|
||||
e.stopPropagation();
|
||||
e.preventDefault();
|
||||
onChange(state);
|
||||
}
|
||||
function onReset() {
|
||||
onChange({ currentPage: 1, pageSize: undefined });
|
||||
}
|
||||
function existingChanges() {
|
||||
return state.pageSize !== Constants.DEFAULT_PAGE_SIZE || state.currentPage !== 1;
|
||||
}
|
||||
return (
|
||||
<form onSubmit={handleSubmit} className="space-y-2">
|
||||
<div>
|
||||
<InputSelector
|
||||
withSearch={false}
|
||||
title={t("shared.pageSize")}
|
||||
value={state.pageSize}
|
||||
setValue={(e) => setState({ ...state, pageSize: Number(e) })}
|
||||
options={[undefined, ...Constants.PAGE_SIZE_OPTIONS].map((size) => {
|
||||
return {
|
||||
value: size,
|
||||
name: size === undefined ? t("shared.default") : formatters.number(size),
|
||||
};
|
||||
})}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<InputNumber
|
||||
title={t("shared.page")}
|
||||
hint={
|
||||
<div className="text-text-neutral-tertiary">
|
||||
{t("shared.totalPages")}: {formatters.number(totalPages)}
|
||||
</div>
|
||||
}
|
||||
value={state.currentPage}
|
||||
min={1}
|
||||
max={totalPages}
|
||||
setValue={(e) => setState({ ...state, currentPage: Number(e) })}
|
||||
/>
|
||||
</div>
|
||||
<div className="flex justify-between space-x-2">
|
||||
<ButtonSecondary disabled={!existingChanges()} onClick={onReset} className="flex justify-center text-center">
|
||||
{t("shared.reset")}
|
||||
</ButtonSecondary>
|
||||
<ButtonPrimary type="submit" className="flex justify-center text-center">
|
||||
{t("shared.confirm")}
|
||||
</ButtonPrimary>
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
// function PageSize({ number, onSelected }: { number?: number; onSelected: () => void }) {
|
||||
// const { t } = useTranslation();
|
||||
// const [searchParams] = useSearchParams();
|
||||
// return (
|
||||
// <button onClick={onSelected} className="w-full bg-background p-2 text-center text-sm text-muted-foreground hover:bg-secondary hover:text-foreground">
|
||||
// <div className="flex items-center justify-center space-x-2">
|
||||
// {number === undefined ? <div className="text-muted-foreground">{t("shared.default")}</div> : <div>{number}</div>}
|
||||
// <div>
|
||||
// {searchParams.get("pageSize") === number?.toString() || (number === undefined && !searchParams.get("pageSize")) ? (
|
||||
// <CheckIcon className="h-3 w-3 text-teal-500" />
|
||||
// ) : null}
|
||||
// </div>
|
||||
// </div>
|
||||
// </button>
|
||||
// );
|
||||
// }
|
||||
@@ -0,0 +1,21 @@
|
||||
// @vitest-environment node
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
const sourcePath = path.resolve(process.cwd(), "apps/web/app/components/ui/tables/TableSimple.tsx");
|
||||
|
||||
describe("TableSimple render performance", () => {
|
||||
it("precomputes repeated table collections outside the row render path", () => {
|
||||
const source = fs.readFileSync(sourcePath, "utf8");
|
||||
|
||||
expect(source).toContain("const visibleHeaders = useMemo");
|
||||
expect(source).toContain("const firstColumnActions = useMemo");
|
||||
expect(source).toContain("const trailingActions = useMemo");
|
||||
expect(source).toContain("const selectedRowsSet = useMemo");
|
||||
expect(source).not.toContain("actions.filter((f) => f.firstColumn)");
|
||||
expect(source).not.toContain("actions.filter((f) => !f.firstColumn)");
|
||||
expect(source).not.toContain("headers.filter((f) => !f.hidden)");
|
||||
expect(source).not.toContain("selectedRows?.includes(item)");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,382 @@
|
||||
import clsx from "clsx";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useNavigate, useSearchParams } from "react-router";
|
||||
import { PaginationDto } from "~/application/dtos/data/PaginationDto";
|
||||
import { RowHeaderDisplayDto } from "~/application/dtos/data/RowHeaderDisplayDto";
|
||||
import RowDisplayValueHelper from "~/utils/helpers/RowDisplayValueHelper";
|
||||
import ButtonTertiary from "../buttons/ButtonTertiary";
|
||||
import TablePagination from "./TablePagination";
|
||||
import { ReactNode, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
|
||||
import { RowHeaderActionDto } from "~/application/dtos/data/RowHeaderActionDto";
|
||||
import DownArrow from "../icons/DownArrow";
|
||||
import UpArrow from "../icons/UpArrow";
|
||||
import ConfirmModal, { RefConfirmModal } from "../modals/ConfirmModal";
|
||||
import { Checkbox } from "../checkbox";
|
||||
import { translateLabel } from "~/utils/i18n/translateLabel";
|
||||
|
||||
interface Props<T> {
|
||||
headers: RowHeaderDisplayDto<T>[];
|
||||
items: T[];
|
||||
actions?: RowHeaderActionDto<T>[];
|
||||
pagination?: PaginationDto;
|
||||
onClickRoute?: (idx: number, item: T) => string | undefined;
|
||||
selectedRows?: T[];
|
||||
onSelected?: (item: T[]) => void;
|
||||
className?: (idx: number, item: T) => string;
|
||||
padding?: string;
|
||||
noRecords?: ReactNode;
|
||||
emptyState?: { title: string; description: string; icon?: ReactNode };
|
||||
darkMode?: boolean;
|
||||
}
|
||||
|
||||
export default function TableSimple<T>(props: Props<T>) {
|
||||
const [showChild, setShowChild] = useState(false);
|
||||
|
||||
// Wait until after client-side hydration to show
|
||||
useEffect(() => {
|
||||
setShowChild(true);
|
||||
}, []);
|
||||
|
||||
if (!showChild) {
|
||||
// You can show some kind of placeholder UI here
|
||||
return null;
|
||||
}
|
||||
|
||||
return <Table {...props} />;
|
||||
}
|
||||
|
||||
function Table<T>({
|
||||
headers,
|
||||
items,
|
||||
actions = [],
|
||||
pagination,
|
||||
onClickRoute,
|
||||
selectedRows,
|
||||
onSelected,
|
||||
className,
|
||||
// Subframe Cell padding: px-4 py-1 (was px-2 py-2).
|
||||
padding = "px-4 py-1",
|
||||
noRecords,
|
||||
emptyState,
|
||||
darkMode,
|
||||
}: Props<T>) {
|
||||
const { t, i18n } = useTranslation();
|
||||
const navigate = useNavigate();
|
||||
const [searchParams, setSearchParams] = useSearchParams();
|
||||
const [sortBy, setSortBy] = useState<{ by: string; order: "asc" | "desc" }[]>();
|
||||
const visibleHeaders = useMemo(() => headers.filter((header) => !header.hidden), [headers]);
|
||||
const firstColumnActions = useMemo(() => actions.filter((action) => action.firstColumn), [actions]);
|
||||
const trailingActions = useMemo(() => actions.filter((action) => !action.firstColumn), [actions]);
|
||||
const selectedRowsSet = useMemo(() => new Set(selectedRows ?? []), [selectedRows]);
|
||||
|
||||
useEffect(() => {
|
||||
let sort = searchParams.get("sort");
|
||||
const sortArray = sort?.split(",") ?? [];
|
||||
const sortObject = sortArray.map((s) => {
|
||||
let order: "asc" | "desc" = "asc";
|
||||
if (s.startsWith("-")) {
|
||||
order = "desc";
|
||||
}
|
||||
return { by: s.replace("-", ""), order };
|
||||
});
|
||||
setSortBy(sortObject);
|
||||
}, [searchParams]);
|
||||
// const [selectedRows, setSelectedRows] = useState<T[]>([]);
|
||||
|
||||
// useEffect(() => {
|
||||
// if (onSelected) {
|
||||
// onSelected(selectedRows);
|
||||
// }
|
||||
// }, [selectedRows]);
|
||||
|
||||
function toggleSelected(_: number, item: T) {
|
||||
if (!selectedRows || !onSelected) {
|
||||
return;
|
||||
}
|
||||
if (selectedRowsSet.has(item)) {
|
||||
onSelected(selectedRows.filter((i) => i !== item));
|
||||
} else {
|
||||
onSelected([...selectedRows, item]);
|
||||
}
|
||||
}
|
||||
|
||||
const checkbox = useRef(null);
|
||||
const [checked, setChecked] = useState(false);
|
||||
const [indeterminate, setIndeterminate] = useState(false);
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (!selectedRows || !onSelected) {
|
||||
return;
|
||||
}
|
||||
const isIndeterminate = selectedRows.length > 0 && selectedRows.length < items.length;
|
||||
setChecked(selectedRows.length === items.length && items.length > 0);
|
||||
setIndeterminate(isIndeterminate);
|
||||
// @ts-ignore
|
||||
checkbox.current.indeterminate = isIndeterminate;
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [onSelected, selectedRows]);
|
||||
|
||||
function toggleAll() {
|
||||
if (!selectedRows || !onSelected) {
|
||||
return;
|
||||
}
|
||||
onSelected(checked || indeterminate ? [] : items);
|
||||
setChecked(!checked && !indeterminate);
|
||||
setIndeterminate(false);
|
||||
}
|
||||
|
||||
function onHeaderClick(header: RowHeaderDisplayDto<T>) {
|
||||
if (!header.sortBy) {
|
||||
return;
|
||||
}
|
||||
let currentSort = sortBy?.find((s) => s.by === header.sortBy);
|
||||
let newSort = header.sortBy;
|
||||
if (currentSort?.order === "asc") {
|
||||
newSort = `-${header.sortBy}`;
|
||||
}
|
||||
searchParams.set("sort", newSort);
|
||||
setSearchParams(searchParams);
|
||||
}
|
||||
|
||||
function getSortDirection(header: RowHeaderDisplayDto<T>) {
|
||||
if (!header.sortBy) {
|
||||
return;
|
||||
}
|
||||
let currentSort = sortBy?.find((s) => s.by === header.sortBy);
|
||||
if (!currentSort) {
|
||||
return;
|
||||
}
|
||||
return currentSort.order;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={clsx("shadow-2xs border-border-default w-full overflow-hidden rounded-lg border", darkMode && "")}>
|
||||
{/* tabIndex makes the horizontally-scrollable region keyboard-reachable
|
||||
(axe scrollable-region-focusable) when the table overflows on mobile. */}
|
||||
<div className="bg-surface-neutral-primary w-full overflow-x-auto" tabIndex={0}>
|
||||
{/* Subframe Table root layout: full-width, fit height, collapsed borders. */}
|
||||
<table className="whitespace-no-wrap h-fit w-full border-collapse">
|
||||
<thead className={clsx("", darkMode && "")}>
|
||||
{/* Subframe header row typo: extra-small medium label on secondary text. */}
|
||||
<tr
|
||||
className={clsx(
|
||||
"border-border-default text-text-neutral-secondary text-body-extra-small-medium-0 font-body-extra-small-medium-0 border-b text-left tracking-wide",
|
||||
darkMode && ""
|
||||
)}
|
||||
>
|
||||
{firstColumnActions.length > 0 && <th scope="col" className="px-2 py-1"></th>}
|
||||
{onSelected && (
|
||||
<th scope="col" className="relative w-10 px-6 py-5 sm:w-12 sm:px-6">
|
||||
<Checkbox
|
||||
title={t("shared.selectAll")}
|
||||
ref={checkbox}
|
||||
className="text-text-neutral-on-solid focus:shadow-focus border-border-default absolute left-4 top-1/2 -mt-2 h-4 w-4 rounded sm:left-6"
|
||||
checked={checked}
|
||||
onCheckedChange={toggleAll}
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
/>
|
||||
</th>
|
||||
)}
|
||||
{visibleHeaders.map((header, idxHeader) => {
|
||||
return (
|
||||
<th
|
||||
key={idxHeader}
|
||||
scope="col"
|
||||
onClick={() => onHeaderClick(header)}
|
||||
className={clsx(
|
||||
idxHeader === 0 && firstColumnActions.length === 0 && "pl-4",
|
||||
// Subframe HeaderCell measures: h-10 row, px-4 cell padding.
|
||||
"h-10 whitespace-nowrap px-4 tracking-wider",
|
||||
header.breakpoint === "sm" && "hidden sm:table-cell",
|
||||
header.breakpoint === "md" && "hidden md:table-cell",
|
||||
header.breakpoint === "lg" && "hidden lg:table-cell",
|
||||
header.breakpoint === "xl" && "hidden xl:table-cell",
|
||||
header.breakpoint === "2xl" && "hidden 2xl:table-cell",
|
||||
header.sortBy && "cursor-pointer"
|
||||
)}
|
||||
>
|
||||
<div
|
||||
className={clsx(
|
||||
"group flex space-x-2",
|
||||
!header.align && "justify-between",
|
||||
header.align === "right" && "justify-end",
|
||||
header.align === "center" && "justify-center",
|
||||
header.align === "left" && "justify-start"
|
||||
)}
|
||||
>
|
||||
<div className={clsx(header.className, "select-none truncate")}>{translateLabel(t, i18n, header.title)}</div>
|
||||
{header.sortBy && (
|
||||
<div className="text text-text-neutral-tertiary group-hover:text-text-neutral-primary">
|
||||
{getSortDirection(header) === "desc" ? (
|
||||
<DownArrow className="h-4 w-4" />
|
||||
) : (
|
||||
getSortDirection(header) === "asc" && <UpArrow className="h-4 w-4" />
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</th>
|
||||
);
|
||||
})}
|
||||
{trailingActions.length > 0 && <th scope="col" className="px-2 py-1"></th>}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className={clsx("divide-divide divide-y", darkMode && "")}>
|
||||
{items.length === 0 ? (
|
||||
<tr className={clsx("", darkMode && "")}>
|
||||
<td colSpan={visibleHeaders.length + actions.length + (onSelected ? 1 : 0)} className="text-center">
|
||||
{noRecords ?? (
|
||||
<div className="text-text-neutral-tertiary p-3 text-sm">
|
||||
{!emptyState ? (
|
||||
<span>{t("shared.noRecords")}</span>
|
||||
) : (
|
||||
<div className="space-y-1">
|
||||
<div className="font-medium">{emptyState.title}</div>
|
||||
<div>{emptyState.description}</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</td>
|
||||
</tr>
|
||||
) : (
|
||||
items.map((item, idxRow) => {
|
||||
const href = onClickRoute?.(idxRow, item);
|
||||
return (
|
||||
<tr
|
||||
key={idxRow}
|
||||
onClick={
|
||||
href
|
||||
? (e) => {
|
||||
if (e.ctrlKey || e.metaKey) {
|
||||
window.open(href, "_blank");
|
||||
} else {
|
||||
navigate(href);
|
||||
}
|
||||
}
|
||||
: undefined
|
||||
}
|
||||
className={clsx(
|
||||
// Subframe Cell minimum row height (min-h-[48px]).
|
||||
"group h-12",
|
||||
href && "hover:bg-surface-neutral-secondary cursor-pointer",
|
||||
darkMode && ""
|
||||
)}
|
||||
>
|
||||
<ActionsCells actions={firstColumnActions} className={className} item={item} idxRow={idxRow} />
|
||||
{onSelected && (
|
||||
<td className={clsx("relative w-10 px-6 sm:w-12 sm:px-6", darkMode && "")}>
|
||||
{selectedRowsSet.has(item) && <div className="bg-brand-brand-surface-solid-default absolute inset-y-0 left-0 w-0.5" />}
|
||||
<div className="flex items-center space-x-1">
|
||||
<Checkbox
|
||||
title={t("shared.select")}
|
||||
className="text-text-neutral-on-solid focus:shadow-focus border-border-default absolute left-4 top-1/2 -mt-2 h-4 w-4 rounded sm:left-6"
|
||||
checked={selectedRowsSet.has(item)}
|
||||
onCheckedChange={(e) => {
|
||||
toggleSelected(idxRow, item);
|
||||
}}
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
/>
|
||||
</div>
|
||||
</td>
|
||||
)}
|
||||
{visibleHeaders.map((header, idxHeader) => {
|
||||
return (
|
||||
<td
|
||||
key={idxHeader}
|
||||
className={clsx(
|
||||
idxHeader === 0 && firstColumnActions.length === 0 && "pl-4",
|
||||
"whitespace-nowrap text-sm",
|
||||
darkMode && "",
|
||||
header.className,
|
||||
padding ?? "px-4 py-1",
|
||||
header.breakpoint === "sm" && "hidden sm:table-cell",
|
||||
header.breakpoint === "md" && "hidden md:table-cell",
|
||||
header.breakpoint === "lg" && "hidden lg:table-cell",
|
||||
header.breakpoint === "xl" && "hidden xl:table-cell",
|
||||
header.breakpoint === "2xl" && "hidden 2xl:table-cell",
|
||||
className && className(idxRow, item)
|
||||
)}
|
||||
>
|
||||
{RowDisplayValueHelper.displayRowValue(t, header, item, idxRow)}
|
||||
</td>
|
||||
);
|
||||
})}
|
||||
<ActionsCells actions={trailingActions} className={className} item={item} idxRow={idxRow} />
|
||||
</tr>
|
||||
);
|
||||
})
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{pagination && (
|
||||
<TablePagination totalItems={pagination.totalItems} totalPages={pagination.totalPages} page={pagination.page} pageSize={pagination.pageSize} />
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function ActionsCells<T>({
|
||||
item,
|
||||
actions,
|
||||
idxRow,
|
||||
className,
|
||||
}: {
|
||||
item: T;
|
||||
actions: RowHeaderActionDto<T>[];
|
||||
idxRow: number;
|
||||
className?: (idx: number, item: T) => string;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const refConfirm = useRef<RefConfirmModal>(null);
|
||||
function onConfirmed({ action, item }: { action: RowHeaderActionDto<T>; item: T }) {
|
||||
if (action.onClick) {
|
||||
action.onClick(idxRow, item);
|
||||
}
|
||||
}
|
||||
return (
|
||||
<>
|
||||
{actions && actions.length > 0 && (
|
||||
<td className={clsx("whitespace-nowrap px-4 py-1", className && className(idxRow, item))}>
|
||||
<div className="flex space-x-2">
|
||||
{actions
|
||||
.filter((f) => !f.hidden || !f.hidden(item))
|
||||
.map((action, idx) => {
|
||||
return (
|
||||
<ButtonTertiary
|
||||
disabled={action.disabled !== undefined ? action.disabled(item) : action.disabled}
|
||||
key={idx}
|
||||
destructive={action.renderIsDestructive !== undefined ? action.renderIsDestructive(item) : action.destructive}
|
||||
prefetch={action.prefetch}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
e.preventDefault();
|
||||
if (action.onClick) {
|
||||
if (action.confirmation) {
|
||||
const confirmation = action.confirmation(item);
|
||||
refConfirm.current?.setDestructive(action.destructive || false);
|
||||
refConfirm.current?.setValue({
|
||||
action: action,
|
||||
item: item,
|
||||
});
|
||||
refConfirm.current?.show(confirmation.title, t("shared.confirm"), t("shared.cancel"), confirmation.description);
|
||||
} else {
|
||||
action.onClick(idxRow, item);
|
||||
}
|
||||
}
|
||||
}}
|
||||
to={action.onClickRoute && action.onClickRoute(idxRow, item)}
|
||||
target={action.onClickRouteTarget}
|
||||
>
|
||||
{action.renderTitle ? action.renderTitle(item) : action.title}
|
||||
</ButtonTertiary>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
<ConfirmModal ref={refConfirm} onYes={onConfirmed} />
|
||||
</td>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user