Skip to content

Commit

Permalink
fix: table actions (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonai authored Jul 29, 2024
1 parent d162e5f commit 4bed473
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-hornets-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@smile/haring-react-table': patch
---

Fix table actions
72 changes: 43 additions & 29 deletions packages/haring-react-table/src/Components/Table/Table.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import type { ITableAction, ITableConfirmAction } from '../../types';
import type { ITableAction } from '../../types';
import type { FloatingPosition } from '@mantine/core';
import type { IPaginationProps } from '@smile/haring-react';
import type { MRT_Row, MRT_TableOptions } from 'mantine-react-table';
Expand Down Expand Up @@ -52,6 +52,11 @@ const tooltipProps = {
withinPortal: true,
};

interface IConfirmAction<Data extends Record<string, unknown>> {
actionIndex: number;
item: MRT_Row<Data> | MRT_Row<Data>[];
}

/** Additional props will be forwarded to the [Mantine React Table useMantineReactTable hook](https://www.mantine-react-table.com/docs/api/table-options) */
export function Table<Data extends Record<string, unknown>>(
props: ITableProps<Data>,
Expand All @@ -68,7 +73,7 @@ export function Table<Data extends Record<string, unknown>>(
} = props;
const { enablePagination = true, manualPagination } = mantineTableProps;
const [confirmAction, setConfirmAction] =
useState<ITableConfirmAction<Data> | null>(null);
useState<IConfirmAction<Data> | null>(null);
const [openedMenuRowIndex, setOpenedMenuRowIndex] = useState<number | null>(
null,
);
Expand All @@ -90,35 +95,40 @@ export function Table<Data extends Record<string, unknown>>(

function handleAction(
item: MRT_Row<Data> | MRT_Row<Data>[],
action: ITableAction<Data>,
actionIndex: number,
): void {
const action = actions[actionIndex];
if (action.confirmation) {
setConfirmAction({ ...action, item });
setConfirmAction({ actionIndex, item });
} else {
action.onAction?.(item);
}
}

function handleCancel(): void {
if (
confirmAction?.confirmModalProps?.onCancel?.(confirmAction.item) !== false
) {
setConfirmAction(null);
if (confirmAction) {
const action = actions[confirmAction.actionIndex];
if (action.confirmModalProps?.onCancel?.(confirmAction.item) !== false) {
setConfirmAction(null);
}
}
}

function handleClose(): void {
setConfirmAction(null);
confirmAction?.confirmModalProps?.onClose?.();
if (confirmAction) {
const action = actions[confirmAction.actionIndex];
action.confirmModalProps?.onClose?.();
}
}

function handleConfirm(): void {
if (
confirmAction?.confirmModalProps?.onConfirm?.(confirmAction.item) !==
false
) {
setConfirmAction(null);
confirmAction?.onAction?.(confirmAction.item);
if (confirmAction) {
const action = actions[confirmAction.actionIndex];
if (action.confirmModalProps?.onConfirm?.(confirmAction.item) !== false) {
setConfirmAction(null);
action.onAction?.(confirmAction.item);
}
}
}

Expand Down Expand Up @@ -187,7 +197,7 @@ export function Table<Data extends Record<string, unknown>>(
>
<ActionIcon
className={classes.action}
onClick={() => handleAction(row, action)}
onClick={() => handleAction(row, index)}
radius={4}
type="button"
variant="transparent"
Expand Down Expand Up @@ -226,7 +236,7 @@ export function Table<Data extends Record<string, unknown>>(
key={index}
color={action.color}
leftSection={getActionIcon(action, row)}
onClick={() => handleAction(row, action)}
onClick={() => handleAction(row, index)}
{...getActionComponentProps(action, row)}
>
{getActionLabel(action, row)}
Expand All @@ -252,7 +262,7 @@ export function Table<Data extends Record<string, unknown>>(
key={index}
color={action.color}
leftSection={getActionIcon(action, rows)}
onClick={() => handleAction(rows, action)}
onClick={() => handleAction(rows, index)}
variant="default"
>
{getActionLabel(action, rows)}
Expand Down Expand Up @@ -313,6 +323,11 @@ export function Table<Data extends Record<string, unknown>>(
paginationProps?.onPageChange?.(value - 1);
}

let action: ITableAction<Data> | undefined = undefined;
if (confirmAction) {
action = actions[confirmAction.actionIndex];
}

return (
<>
<MantineReactTable table={table} />
Expand All @@ -335,17 +350,16 @@ export function Table<Data extends Record<string, unknown>>(
)}
</>
)}
<ConfirmModal
{...confirmAction?.confirmModalProps}
onCancel={handleCancel}
onClose={handleClose}
onConfirm={handleConfirm}
opened={Boolean(confirmAction)}
title={
confirmAction?.confirmModalProps?.title ??
getActionLabel(confirmAction)
}
/>
{action ? (
<ConfirmModal
{...action.confirmModalProps}
onCancel={handleCancel}
onClose={handleClose}
onConfirm={handleConfirm}
opened={Boolean(confirmAction)}
title={action.confirmModalProps?.title ?? getActionLabel(action)}
/>
) : null}
</>
);
}

0 comments on commit 4bed473

Please sign in to comment.