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

Add row context (right click) additionally to actions in tables #8013

Merged
merged 6 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"embla-carousel-react": "^8.3.0",
"fuse.js": "^7.0.0",
"html5-qrcode": "^2.3.8",
"mantine-contextmenu": "^7.11.3",
"mantine-datatable": "^7.12.4",
"qrcode": "^1.5.4",
"react": "^18.3.1",
Expand Down
31 changes: 17 additions & 14 deletions src/frontend/src/contexts/ThemeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { t } from '@lingui/macro';
import { MantineProvider, createTheme } from '@mantine/core';
import { ModalsProvider } from '@mantine/modals';
import { Notifications } from '@mantine/notifications';
import { ContextMenuProvider } from 'mantine-contextmenu';

import { AboutInvenTreeModal } from '../components/modals/AboutInvenTreeModal';
import { LicenseModal } from '../components/modals/LicenseModal';
Expand Down Expand Up @@ -40,20 +41,22 @@ export function ThemeContext({

return (
<MantineProvider theme={myTheme} colorSchemeManager={colorSchema}>
<LanguageContext>
<ModalsProvider
labels={{ confirm: t`Submit`, cancel: t`Cancel` }}
modals={{
qr: QrCodeModal,
info: ServerInfoModal,
about: AboutInvenTreeModal,
license: LicenseModal
}}
>
<Notifications />
{children}
</ModalsProvider>
</LanguageContext>
<ContextMenuProvider>
<LanguageContext>
<ModalsProvider
labels={{ confirm: t`Submit`, cancel: t`Cancel` }}
modals={{
qr: QrCodeModal,
info: ServerInfoModal,
about: AboutInvenTreeModal,
license: LicenseModal
}}
>
<Notifications />
{children}
</ModalsProvider>
</LanguageContext>
</ContextMenuProvider>
</MantineProvider>
);
}
1 change: 1 addition & 0 deletions src/frontend/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import '@mantine/dates/styles.css';
import '@mantine/notifications/styles.css';
import '@mantine/spotlight/styles.css';
import * as Sentry from '@sentry/react';
import 'mantine-contextmenu/styles.css';
import 'mantine-datatable/styles.css';
import React from 'react';
import ReactDOM from 'react-dom/client';
Expand Down
29 changes: 29 additions & 0 deletions src/frontend/src/tables/InvenTreeTable.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { t } from '@lingui/macro';
import { Box, Stack } from '@mantine/core';
import { useQuery } from '@tanstack/react-query';
import { useContextMenu } from 'mantine-contextmenu';
import {
DataTable,
type DataTableCellClickHandler,
Expand Down Expand Up @@ -87,6 +88,7 @@ export type InvenTreeTableProps<T = any> = {
modelType?: ModelType;
rowStyle?: (record: T, index: number) => any;
modelField?: string;
onRowContextMenu?: (record: T, event: any) => void;
minHeight?: number;
noHeader?: boolean;
};
Expand Down Expand Up @@ -137,6 +139,7 @@ export function InvenTreeTable<T extends Record<string, any>>({
const [fieldNames, setFieldNames] = useState<Record<string, string>>({});

const navigate = useNavigate();
const { showContextMenu } = useContextMenu();

// Construct table filters - note that we can introspect filter labels from column names
const filters: TableFilter[] = useMemo(() => {
Expand Down Expand Up @@ -565,6 +568,31 @@ export function InvenTreeTable<T extends Record<string, any>>({
[props.onRowClick, props.onCellClick]
);

// Callback when a row is right-clicked
const handleRowContextMenu = ({
record,
event
}: {
record: any;
event: any;
}) => {
if (props.onRowContextMenu) {
return props.onRowContextMenu(record, event);
} else if (props.rowActions) {
const empty = () => {};
const items = props.rowActions(record).map((action) => ({
key: action.title ?? '',
title: action.title ?? '',
color: action.color,
icon: action.icon,
onClick: action.onClick ?? empty,
hidden: action.hidden,
disabled: action.disabled
}));
return showContextMenu(items)(event);
}
};

// pagination refresth table if pageSize changes
function updatePageSize(newData: number) {
tableState.setPageSize(newData);
Expand Down Expand Up @@ -663,6 +691,7 @@ export function InvenTreeTable<T extends Record<string, any>>({
overflow: 'hidden'
})
}}
onRowContextMenu={handleRowContextMenu}
{...optionalParams}
/>
</Box>
Expand Down
5 changes: 5 additions & 0 deletions src/frontend/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3389,6 +3389,11 @@ make-dir@^4.0.0:
dependencies:
semver "^7.5.3"

mantine-contextmenu@^7.11.3:
version "7.12.2"
resolved "https://registry.yarnpkg.com/mantine-contextmenu/-/mantine-contextmenu-7.12.2.tgz#60eb6b1aade5136737e894c1c2f3fa8dad22b1c0"
integrity sha512-HD/xrGTGkq51WIVOh97gG1C0Z+PgV6gBPzALrPmv14R4t8iBUEJEY1A9CUvnV/yNjxBas+AA+2flXMAc0wyBxw==

mantine-datatable@^7.12.4:
version "7.12.4"
resolved "https://registry.yarnpkg.com/mantine-datatable/-/mantine-datatable-7.12.4.tgz#68e5b0ab4dc7e32b505139bf2b4d05fbb1922c0f"
Expand Down
Loading