Skip to content

Commit

Permalink
[setup] DRY out utils to action_types
Browse files Browse the repository at this point in the history
+ `collapsed_item_actions` is repeating a util already in there unnecessarily, DRY it out too
  • Loading branch information
cee-chen committed Nov 17, 2023
1 parent b038a8c commit 6cf4211
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
11 changes: 8 additions & 3 deletions src/components/basic_table/action_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,13 @@ export interface CustomItemAction<T> {

export type Action<T> = DefaultItemAction<T> | CustomItemAction<T>;

export const isCustomItemAction = (
action: DefaultItemAction<any> | CustomItemAction<any>
): action is CustomItemAction<any> => {
export const isCustomItemAction = <T>(
action: DefaultItemAction<T> | CustomItemAction<T>
): action is CustomItemAction<T> => {
return action.hasOwnProperty('render');
};

export const callWithItemIfFunction =
<T>(item: T) =>
<U>(prop: U | ((item: T) => U)): U =>
typeof prop === 'function' ? (prop as Function)(item) : prop;
13 changes: 7 additions & 6 deletions src/components/basic_table/collapsed_item_actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ import { EuiButtonIcon } from '../button';
import { EuiToolTip } from '../tool_tip';
import { EuiI18n } from '../i18n';

import { Action, CustomItemAction } from './action_types';
import {
Action,
CustomItemAction,
isCustomItemAction,
callWithItemIfFunction,
} from './action_types';
import { ItemIdResolved } from './table_types';

export interface CollapsedItemActionsProps<T extends {}> {
Expand All @@ -32,10 +37,6 @@ export interface CollapsedItemActionsProps<T extends {}> {
className?: string;
}

const actionIsCustomItemAction = <T extends {}>(
action: Action<T>
): action is CustomItemAction<T> => action.hasOwnProperty('render');

export const CollapsedItemActions = <T extends {}>({
actions,
itemId,
Expand All @@ -59,7 +60,7 @@ export const CollapsedItemActions = <T extends {}>({
const enabled = actionEnabled(action);
if (enabled) setAllDisabled(false);

if (actionIsCustomItemAction(action)) {
if (isCustomItemAction<T>(action)) {
const customAction = action as CustomItemAction<T>;
const actionControl = customAction.render(item, enabled);
controls.push(
Expand Down
12 changes: 6 additions & 6 deletions src/components/basic_table/default_item_action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import React, { ReactElement } from 'react';

import { isString } from '../../services/predicate';
import {
EuiButtonEmpty,
Expand All @@ -15,10 +16,14 @@ import {
EuiButtonIconProps,
} from '../button';
import { EuiToolTip } from '../tool_tip';
import { DefaultItemAction as Action } from './action_types';
import { useGeneratedHtmlId } from '../../services/accessibility';
import { EuiScreenReaderOnly } from '../accessibility';

import {
DefaultItemAction as Action,
callWithItemIfFunction,
} from './action_types';

export interface DefaultItemActionProps<T> {
action: Action<T>;
enabled: boolean;
Expand Down Expand Up @@ -109,8 +114,3 @@ export const DefaultItemAction = <T,>({
button
);
};

const callWithItemIfFunction =
<T,>(item: T) =>
<U,>(prop: U | ((item: T) => U)): U =>
typeof prop === 'function' ? (prop as Function)(item) : prop;
2 changes: 1 addition & 1 deletion src/components/basic_table/expanded_item_actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const ExpandedItemActions = <T extends {}>({
expandedItemActions__completelyHide: moreThanThree && index < 2,
});

if (isCustomItemAction(action)) {
if (isCustomItemAction<T>(action)) {
// custom action has a render function
tools.push(
<CustomItemAction
Expand Down

0 comments on commit 6cf4211

Please sign in to comment.