Refactor table columns (#5519)

- Improve show/hide
- Refactor "actions" colum
This commit is contained in:
Oliver 2023-09-09 08:34:27 +10:00 committed by GitHub
parent 528fa349b0
commit 14a6330114
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 101 additions and 91 deletions

View File

@ -15,6 +15,7 @@ import { DownloadAction } from './DownloadAction';
import { TableFilter } from './Filter';
import { FilterGroup } from './FilterGroup';
import { FilterSelectModal } from './FilterSelectModal';
import { RowAction, RowActions } from './RowActions';
import { TableSearchInput } from './Search';
/*
@ -96,7 +97,8 @@ export function InvenTreeTable({
printingActions = [],
barcodeActions = [],
customActionGroups = [],
customFilters = []
customFilters = [],
rowActions
}: {
url: string;
params: any;
@ -115,12 +117,15 @@ export function InvenTreeTable({
barcodeActions?: any[];
customActionGroups?: any[];
customFilters?: TableFilter[];
rowActions?: (record: any) => RowAction[];
}) {
// Data columns
const [dataColumns, setDataColumns] = useState<any[]>(columns);
// Check if any columns are switchable (can be hidden)
const hasSwitchableColumns = columns.some((col: any) => col.switchable);
const hasSwitchableColumns = columns.some(
(col: TableColumn) => col.switchable
);
// Manage state for switchable columns (initially load from local storage)
let [hiddenColumns, setHiddenColumns] = useState(() =>
@ -129,15 +134,34 @@ export function InvenTreeTable({
// Update column visibility when hiddenColumns change
useEffect(() => {
setDataColumns(
dataColumns.map((col) => {
return {
...col,
hidden: hiddenColumns.includes(col.accessor)
};
})
);
}, [hiddenColumns]);
let cols = dataColumns.map((col) => {
let hidden: boolean = col.hidden;
if (col.switchable) {
hidden = hiddenColumns.includes(col.accessor);
}
return {
...col,
hidden: hidden
};
});
// If row actions are available, add a column for them
if (rowActions) {
cols.push({
accessor: 'actions',
title: '',
hidden: false,
switchable: false,
render: function (record: any) {
return <RowActions actions={rowActions(record)} />;
}
});
}
setDataColumns(cols);
}, [columns, hiddenColumns, rowActions]);
// Callback when column visibility is toggled
function toggleColumn(columnName: string) {

View File

@ -24,25 +24,27 @@ export function RowActions({
actions: RowAction[];
}): ReactNode {
return (
<Menu>
<Menu.Target>
<ActionIcon variant="subtle" color="gray">
<IconDots />
</ActionIcon>
</Menu.Target>
<Menu.Dropdown>
<Menu.Label>{title || t`Actions`}</Menu.Label>
{actions.map((action, idx) => (
<Menu.Item
key={idx}
onClick={action.onClick}
icon={action.icon}
title={action.tooltip || action.title}
>
{action.title}
</Menu.Item>
))}
</Menu.Dropdown>
</Menu>
actions.length > 0 && (
<Menu>
<Menu.Target>
<ActionIcon variant="subtle" color="gray">
<IconDots />
</ActionIcon>
</Menu.Target>
<Menu.Dropdown>
<Menu.Label>{title || t`Actions`}</Menu.Label>
{actions.map((action, idx) => (
<Menu.Item
key={idx}
onClick={action.onClick}
icon={action.icon}
title={action.tooltip || action.title}
>
{action.title}
</Menu.Item>
))}
</Menu.Dropdown>
</Menu>
)
);
}

View File

@ -10,7 +10,7 @@ import { ThumbnailHoverCard } from '../../items/Thumbnail';
import { TableColumn } from '../Column';
import { TableFilter } from '../Filter';
import { InvenTreeTable } from '../InvenTreeTable';
import { RowActions } from '../RowActions';
import { RowAction } from '../RowActions';
/**
* Construct a list of columns for the part table
@ -82,38 +82,6 @@ function partTableColumns(): TableColumn[] {
accessor: 'link',
title: t`Link`,
switchable: true
},
{
accessor: 'actions',
title: '',
switchable: false,
render: function (record: any) {
return (
<RowActions
title={`Part Actions`}
actions={[
{
title: t`Edit`,
icon: <IconEdit color="blue" />,
onClick: () =>
editPart({
part_id: record.pk,
callback: () => {
// TODO: Reload the table, somehow?
// TODO: Insert / update a single row in the table?
// TODO: We need to have a hook back into the table
}
})
},
{
title: t`Delete`,
onClick: notYetImplemented,
icon: <IconTrash color="red" />
}
]}
/>
);
}
}
];
}
@ -229,6 +197,32 @@ export function PartListTable({ params = {} }: { params?: any }) {
// Add required query parameters
tableParams.category_detail = true;
function partTableRowActions(record: any): RowAction[] {
let actions: RowAction[] = [];
actions.push({
title: t`Edit`,
onClick: () => {
editPart({
part_id: record.pk,
callback: () => {
// TODO: Reload the table, somehow?
notYetImplemented();
}
});
}
});
if (record.IPN) {
actions.push({
title: t`View IPN`,
onClick: () => {}
});
}
return actions;
}
return (
<InvenTreeTable
url="part/"
@ -241,6 +235,7 @@ export function PartListTable({ params = {} }: { params?: any }) {
params={tableParams}
columns={tableColumns}
customFilters={tableFilters}
rowActions={partTableRowActions}
/>
);
}

View File

@ -8,7 +8,7 @@ import { ActionButton } from '../../items/ActionButton';
import { ThumbnailHoverCard } from '../../items/Thumbnail';
import { TableColumn } from '../Column';
import { TableFilter } from '../Filter';
import { RowActions } from '../RowActions';
import { RowAction } from '../RowActions';
import { InvenTreeTable } from './../InvenTreeTable';
/**
@ -66,7 +66,7 @@ function stockItemTableColumns(): TableColumn[] {
// TODO: Custom renderer for location
return record.location;
}
},
}
// TODO: stocktake column
// TODO: expiry date
// TODO: last updated
@ -76,31 +76,6 @@ function stockItemTableColumns(): TableColumn[] {
// TODO: stock value
// TODO: packaging
// TODO: notes
{
accessor: 'actions',
title: '',
sortable: false,
switchable: false,
render: function (record: any) {
return (
<RowActions
title={t`Stock Actions`}
actions={[
{
title: t`Edit`,
icon: <IconEdit color="blue" />,
onClick: notYetImplemented
},
{
title: t`Delete`,
icon: <IconTrash color="red" />,
onClick: notYetImplemented
}
]}
/>
);
}
}
];
}
@ -142,6 +117,19 @@ export function StockItemTable({ params = {} }: { params?: any }) {
let tableColumns = useMemo(() => stockItemTableColumns(), []);
let tableFilters = useMemo(() => stockItemTableFilters(), []);
function stockItemRowActions(record: any): RowAction[] {
let actions: RowAction[] = [];
actions.push({
title: t`Edit`,
onClick: () => {
notYetImplemented();
}
});
return actions;
}
return (
<InvenTreeTable
url="stock/"
@ -151,6 +139,7 @@ export function StockItemTable({ params = {} }: { params?: any }) {
params={tableParams}
columns={tableColumns}
customFilters={tableFilters}
rowActions={stockItemRowActions}
/>
);
}