From 732783f508d87dfe066599795fc166399ee49424 Mon Sep 17 00:00:00 2001 From: Cee Chen Date: Tue, 14 Nov 2023 01:56:45 -0800 Subject: [PATCH] [optional][tech debt] Convert from class components to function components - now that all the onFocus/onBlur logic is gone, these components can more easily be function components + fix typescript complaints --- .../basic_table/collapsed_item_actions.tsx | 265 +++++++++--------- .../basic_table/custom_item_action.tsx | 23 +- 2 files changed, 136 insertions(+), 152 deletions(-) diff --git a/src/components/basic_table/collapsed_item_actions.tsx b/src/components/basic_table/collapsed_item_actions.tsx index 74266116866c..bc93d2ce2965 100644 --- a/src/components/basic_table/collapsed_item_actions.tsx +++ b/src/components/basic_table/collapsed_item_actions.tsx @@ -6,17 +6,25 @@ * Side Public License, v 1. */ -import React, { Component, ReactNode, ReactElement } from 'react'; +import React, { + useState, + useCallback, + useMemo, + ReactNode, + ReactElement, +} from 'react'; + import { isString } from '../../services/predicate'; import { EuiContextMenuItem, EuiContextMenuPanel } from '../context_menu'; import { EuiPopover } from '../popover'; import { EuiButtonIcon } from '../button'; import { EuiToolTip } from '../tool_tip'; import { EuiI18n } from '../i18n'; + import { Action, CustomItemAction } from './action_types'; import { ItemIdResolved } from './table_types'; -export interface CollapsedItemActionsProps { +export interface CollapsedItemActionsProps { actions: Array>; item: T; itemId: ItemIdResolved; @@ -24,143 +32,124 @@ export interface CollapsedItemActionsProps { className?: string; } -interface CollapsedItemActionsState { - popoverOpen: boolean; -} - -function actionIsCustomItemAction( +const actionIsCustomItemAction = ( action: Action -): action is CustomItemAction { - return action.hasOwnProperty('render'); -} - -export class CollapsedItemActions extends Component< - CollapsedItemActionsProps, - CollapsedItemActionsState -> { - state = { popoverOpen: false }; - - togglePopover = () => { - this.setState((prevState) => ({ popoverOpen: !prevState.popoverOpen })); - }; - - closePopover = () => { - this.setState({ popoverOpen: false }); - }; - - onClickItem = (onClickAction?: () => void) => { - this.closePopover(); +): action is CustomItemAction => action.hasOwnProperty('render'); + +export const CollapsedItemActions = ({ + actions, + itemId, + item, + actionEnabled, + className, +}: CollapsedItemActionsProps) => { + const [popoverOpen, setPopoverOpen] = useState(false); + const [allDisabled, setAllDisabled] = useState(true); + + const onClickItem = useCallback((onClickAction?: () => void) => { + setPopoverOpen(false); onClickAction?.(); - }; - - render() { - const { actions, itemId, item, actionEnabled, className } = this.props; - - const isOpen = this.state.popoverOpen; - - let allDisabled = true; - const controls = actions.reduce( - (controls, action, index) => { - const key = `action_${itemId}_${index}`; - const available = action.available ? action.available(item) : true; - if (!available) { - return controls; - } - const enabled = actionEnabled(action); - allDisabled = allDisabled && !enabled; - if (actionIsCustomItemAction(action)) { - const customAction = action as CustomItemAction; - const actionControl = customAction.render(item, enabled); - controls.push( - // Do not put the `onClick` on the EuiContextMenuItem itself - otherwise - // it renders a