2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-28 11:36:44 +00:00

Update RowActions (#6106)

* Update RowActions

- Show text labels (again)
- Revert to Menu.Item
- Adjust placement
- Add optional tooltip

* Remove unused import
This commit is contained in:
Oliver 2023-12-16 21:45:03 +11:00 committed by GitHub
parent 95d29f18e9
commit bee3e93162
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 17 deletions

View File

@ -1,5 +1,5 @@
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import { ActionIcon, Group, Tooltip } from '@mantine/core'; import { ActionIcon, Tooltip } from '@mantine/core';
import { Menu } from '@mantine/core'; import { Menu } from '@mantine/core';
import { IconCopy, IconDots, IconEdit, IconTrash } from '@tabler/icons-react'; import { IconCopy, IconDots, IconEdit, IconTrash } from '@tabler/icons-react';
import { ReactNode, useMemo, useState } from 'react'; import { ReactNode, useMemo, useState } from 'react';
@ -9,6 +9,7 @@ import { notYetImplemented } from '../../functions/notifications';
// Type definition for a table row action // Type definition for a table row action
export type RowAction = { export type RowAction = {
title: string; title: string;
tooltip?: string;
color?: string; color?: string;
icon: ReactNode; icon: ReactNode;
onClick?: () => void; onClick?: () => void;
@ -18,14 +19,17 @@ export type RowAction = {
// Component for duplicating a row in a table // Component for duplicating a row in a table
export function RowDuplicateAction({ export function RowDuplicateAction({
onClick, onClick,
tooltip,
hidden hidden
}: { }: {
onClick?: () => void; onClick?: () => void;
tooltip?: string;
hidden?: boolean; hidden?: boolean;
}): RowAction { }): RowAction {
return { return {
title: t`Duplicate`, title: t`Duplicate`,
color: 'green', color: 'green',
tooltip: tooltip,
onClick: onClick, onClick: onClick,
icon: <IconCopy />, icon: <IconCopy />,
hidden: hidden hidden: hidden
@ -35,14 +39,17 @@ export function RowDuplicateAction({
// Component for editing a row in a table // Component for editing a row in a table
export function RowEditAction({ export function RowEditAction({
onClick, onClick,
tooltip,
hidden hidden
}: { }: {
onClick?: () => void; onClick?: () => void;
tooltip?: string;
hidden?: boolean; hidden?: boolean;
}): RowAction { }): RowAction {
return { return {
title: t`Edit`, title: t`Edit`,
color: 'blue', color: 'blue',
tooltip: tooltip,
onClick: onClick, onClick: onClick,
icon: <IconEdit />, icon: <IconEdit />,
hidden: hidden hidden: hidden
@ -52,14 +59,17 @@ export function RowEditAction({
// Component for deleting a row in a table // Component for deleting a row in a table
export function RowDeleteAction({ export function RowDeleteAction({
onClick, onClick,
tooltip,
hidden hidden
}: { }: {
onClick?: () => void; onClick?: () => void;
tooltip?: string;
hidden?: boolean; hidden?: boolean;
}): RowAction { }): RowAction {
return { return {
title: t`Delete`, title: t`Delete`,
color: 'red', color: 'red',
tooltip: tooltip,
onClick: onClick, onClick: onClick,
icon: <IconTrash />, icon: <IconTrash />,
hidden: hidden hidden: hidden
@ -97,10 +107,14 @@ export function RowActions({
// Render a single action icon // Render a single action icon
function RowActionIcon(action: RowAction) { function RowActionIcon(action: RowAction) {
return ( return (
<Tooltip withinPortal={true} label={action.title} key={action.title}> <Tooltip
<ActionIcon withinPortal={true}
label={action.tooltip ?? action.title}
key={action.title}
>
<Menu.Item
color={action.color} color={action.color}
size={20} icon={action.icon}
onClick={(event) => { onClick={(event) => {
// Prevent clicking on the action from selecting the row itself // Prevent clicking on the action from selecting the row itself
event?.preventDefault(); event?.preventDefault();
@ -116,23 +130,18 @@ export function RowActions({
setOpened(false); setOpened(false);
}} }}
> >
{action.icon} {action.title}
</ActionIcon> </Menu.Item>
</Tooltip> </Tooltip>
); );
} }
// If only a single action is available, display that
if (visibleActions.length == 1) {
return <RowActionIcon {...visibleActions[0]} />;
}
return ( return (
visibleActions.length > 0 && ( visibleActions.length > 0 && (
<Menu <Menu
withinPortal={true} withinPortal={true}
disabled={disabled} disabled={disabled}
position="left" position="bottom-end"
opened={opened} opened={opened}
onChange={setOpened} onChange={setOpened}
> >
@ -149,11 +158,9 @@ export function RowActions({
</Tooltip> </Tooltip>
</Menu.Target> </Menu.Target>
<Menu.Dropdown> <Menu.Dropdown>
<Group position="right" spacing="xs" p={8}> {visibleActions.map((action) => (
{visibleActions.map((action) => ( <RowActionIcon key={action.title} {...action} />
<RowActionIcon key={action.title} {...action} /> ))}
))}
</Group>
</Menu.Dropdown> </Menu.Dropdown>
</Menu> </Menu>
) )

View File

@ -110,6 +110,7 @@ export function PartParameterTable({ partId }: { partId: any }) {
actions.push( actions.push(
RowEditAction({ RowEditAction({
tooltip: t`Edit Part Parameter`,
hidden: !user.hasChangeRole(UserRoles.part), hidden: !user.hasChangeRole(UserRoles.part),
onClick: () => { onClick: () => {
openEditApiForm({ openEditApiForm({
@ -132,6 +133,7 @@ export function PartParameterTable({ partId }: { partId: any }) {
actions.push( actions.push(
RowDeleteAction({ RowDeleteAction({
tooltip: t`Delete Part Parameter`,
hidden: !user.hasDeleteRole(UserRoles.part), hidden: !user.hasDeleteRole(UserRoles.part),
onClick: () => { onClick: () => {
openDeleteApiForm({ openDeleteApiForm({