From 38d9f389091a682d954ecb041d37af6a0ca23526 Mon Sep 17 00:00:00 2001 From: alx-chernigin Date: Tue, 22 Oct 2024 15:00:24 +0300 Subject: [PATCH] feat(Table): review fixes 2 --- src/components/RowActions/ActionsCell.tsx | 34 ++++++++ src/components/RowActions/RowActions.tsx | 96 ++++++++++++++--------- src/components/RowActions/index.ts | 1 + src/constants/actionsColumn.tsx | 38 ++------- 4 files changed, 100 insertions(+), 69 deletions(-) create mode 100644 src/components/RowActions/ActionsCell.tsx diff --git a/src/components/RowActions/ActionsCell.tsx b/src/components/RowActions/ActionsCell.tsx new file mode 100644 index 0000000..c4f9971 --- /dev/null +++ b/src/components/RowActions/ActionsCell.tsx @@ -0,0 +1,34 @@ +import React from 'react'; + +import type {CellContext} from '@tanstack/react-table'; + +import {RowActions} from './RowActions'; +import type {TableActionsSettings} from './types'; + +export const ACTIONS_COLUMN_ID = '_actions'; + +interface ActionsCellProps extends TableActionsSettings { + cellContext: CellContext; +} + +export const ActionsCell = ({ + getRowActions, + renderRowActions, + rowActionsSize, + cellContext, +}: ActionsCellProps) => { + const {original: item, index} = cellContext.row; + + if (renderRowActions) { + return renderRowActions({row: cellContext.row}); + } + + return ( + + item={item} + index={index} + getRowActions={getRowActions} + rowActionsSize={rowActionsSize} + /> + ); +}; diff --git a/src/components/RowActions/RowActions.tsx b/src/components/RowActions/RowActions.tsx index 883f69a..271a091 100644 --- a/src/components/RowActions/RowActions.tsx +++ b/src/components/RowActions/RowActions.tsx @@ -26,6 +26,59 @@ const isActionGroup = ( return Array.isArray((config as TableActionGroup).items); }; +interface RowActionsMenuProps { + size: TableActionsSettings['rowActionsSize']; + item: TValue; + actions: TableActionConfig[]; + onMenuItemClick: () => unknown; +} + +const RowActionsMenu = ({ + item, + actions, + size, + onMenuItemClick, +}: RowActionsMenuProps) => { + const renderPopupMenuItem = (action: TableActionConfig, index: number) => { + if (isActionGroup(action)) { + return ( + + {action.items.map(renderPopupMenuItem)} + + ); + } + + const {text, icon, handler, href, ...restProps} = action; + + const handleMenuItemClick = ( + event: React.MouseEvent, + ) => { + event.stopPropagation(); + handler(item, index, event); + onMenuItemClick(); + }; + + return ( + + {text} + + ); + }; + + return ( + + {actions.map(renderPopupMenuItem)} + + ); +}; + export const RowActions = ({ index: rowIndex, item, @@ -62,40 +115,6 @@ export const RowActions = ({ return null; } - const renderPopupMenuItem = (action: TableActionConfig, index: number) => { - if (isActionGroup(action)) { - return ( - - {action.items.map(renderPopupMenuItem)} - - ); - } - - const {text, icon, handler, href, ...restProps} = action; - - const handleMenuItemClick = ( - event: React.MouseEvent, - ) => { - event.stopPropagation(); - handler(item, index, event); - - setIsPopupOpen(false); - }; - - return ( - - {text} - - ); - }; - return (
({ event.stopPropagation(); }} > - - {actions.map(renderPopupMenuItem)} - + setIsPopupOpen(false)} + />