Skip to content

Commit

Permalink
Remove duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Jun 25, 2024
1 parent 0ff6ede commit 3c453a9
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 35 deletions.
14 changes: 4 additions & 10 deletions packages/dataviews/src/bulk-actions-toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { useRegistry } from '@wordpress/data';
* Internal dependencies
*/
import { ActionWithModal } from './item-actions';
import type { Action, AnyItem } from './types';
import type { Action, AnyItem, setSelection } from './types';
import type { ActionTriggerProps } from './item-actions';

interface ActionButtonProps< Item extends AnyItem > {
Expand All @@ -32,18 +32,14 @@ interface ToolbarContentProps< Item extends AnyItem > {
selection: string[];
actionsToShow: Action< Item >[];
selectedItems: Item[];
onSelectionChange: (
selection: string[] | ( ( prevState: string[] ) => string[] )
) => void;
onSelectionChange: setSelection;
}

interface BulkActionsToolbarProps< Item extends AnyItem > {
data: Item[];
selection: string[];
actions: Action< Item >[];
onSelectionChange: (
selection: string[] | ( ( prevState: string[] ) => string[] )
) => void;
onSelectionChange: setSelection;
getItemId: ( item: Item ) => string;
}

Expand Down Expand Up @@ -135,9 +131,7 @@ function renderToolbarContent< Item extends AnyItem >(
selectedItems: Item[],
actionInProgress: string | null,
setActionInProgress: ( actionId: string | null ) => void,
onSelectionChange: (
selection: string[] | ( ( prevState: string[] ) => string[] )
) => void
onSelectionChange: setSelection
) {
return (
<>
Expand Down
6 changes: 2 additions & 4 deletions packages/dataviews/src/bulk-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { useRegistry } from '@wordpress/data';
* Internal dependencies
*/
import { unlock } from './lock-unlock';
import type { Action, ActionModal, AnyItem } from './types';
import type { Action, ActionModal, AnyItem, setSelection } from './types';

const {
DropdownMenuV2: DropdownMenu,
Expand Down Expand Up @@ -46,9 +46,7 @@ interface BulkActionsProps< Item extends AnyItem > {
data: Item[];
actions: Action< Item >[];
selection: string[];
onSelectionChange: (
selection: string[] | ( ( prevState: string[] ) => string[] )
) => void;
onSelectionChange: setSelection;
getItemId: ( item: Item ) => string;
}

Expand Down
13 changes: 9 additions & 4 deletions packages/dataviews/src/dataviews.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ import { VIEW_LAYOUTS } from './layouts';
import BulkActions from './bulk-actions';
import { normalizeFields } from './normalize-fields';
import BulkActionsToolbar from './bulk-actions-toolbar';
import type { Action, AnyItem, Field, View, ViewBaseProps } from './types';
import type {
Action,
AnyItem,
Field,
View,
ViewBaseProps,
setSelection,
} from './types';

interface DataViewsProps< Item extends AnyItem > {
view: View;
Expand All @@ -40,9 +47,7 @@ interface DataViewsProps< Item extends AnyItem > {
};
supportedLayouts: string[];
selection?: string[];
setSelection?: (
selection: string[] | ( ( prevState: string[] ) => string[] )
) => void;
setSelection?: setSelection;
onSelectionChange?: ( items: Item[] ) => void;
}

Expand Down
6 changes: 2 additions & 4 deletions packages/dataviews/src/single-selection-checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import { CheckboxControl } from '@wordpress/components';
/**
* Internal dependencies
*/
import type { Field, AnyItem } from './types';
import type { Field, AnyItem, setSelection } from './types';

interface SingleSelectionCheckboxProps< Item extends AnyItem > {
selection: string[];
onSelectionChange: (
selection: string[] | ( ( prevState: string[] ) => string[] )
) => void;
onSelectionChange: setSelection;
item: Item;
getItemId: ( item: Item ) => string;
primaryField?: Field< Item >;
Expand Down
8 changes: 5 additions & 3 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
*/
import type { ReactElement, ReactNode } from 'react';

export type setSelection = (
selection: string[] | ( ( prevState: string[] ) => string[] )
) => void;

export type SortDirection = 'asc' | 'desc';

/**
Expand Down Expand Up @@ -376,9 +380,7 @@ export interface ViewBaseProps< Item extends AnyItem > {
getItemId: ( item: Item ) => string;
isLoading?: boolean;
onChangeView( view: View ): void;
onSelectionChange: (
selection: string[] | ( ( prevState: string[] ) => string[] )
) => void;
onSelectionChange: setSelection;
selection: string[];
setOpenedFilter: ( fieldId: string ) => void;
view: View;
Expand Down
12 changes: 8 additions & 4 deletions packages/dataviews/src/view-grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ import { __ } from '@wordpress/i18n';
import ItemActions from './item-actions';
import SingleSelectionCheckbox from './single-selection-checkbox';
import { useHasAPossibleBulkAction } from './bulk-actions';
import type { Action, AnyItem, NormalizedField, ViewGridProps } from './types';
import type {
Action,
AnyItem,
NormalizedField,
ViewGridProps,
setSelection,
} from './types';

interface GridItemProps< Item extends AnyItem > {
selection: string[];
data: Item[];
onSelectionChange: (
selection: string[] | ( ( prevState: string[] ) => string[] )
) => void;
onSelectionChange: setSelection;
getItemId: ( item: Item ) => string;
item: Item;
actions: Action< Item >[];
Expand Down
9 changes: 3 additions & 6 deletions packages/dataviews/src/view-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import type {
SortDirection,
ViewTable as ViewTableType,
ViewTableProps,
setSelection,
} from './types';

const {
Expand All @@ -72,9 +73,7 @@ interface HeaderMenuProps< Item extends AnyItem > {

interface BulkSelectionCheckboxProps< Item extends AnyItem > {
selection: string[];
onSelectionChange: (
selection: string[] | ( ( prevState: string[] ) => string[] )
) => void;
onSelectionChange: setSelection;
data: Item[];
actions: Action< Item >[];
getItemId: ( item: Item ) => string;
Expand All @@ -89,9 +88,7 @@ interface TableRowProps< Item extends AnyItem > {
primaryField?: NormalizedField< Item >;
selection: string[];
getItemId: ( item: Item ) => string;
onSelectionChange: (
selection: string[] | ( ( prevState: string[] ) => string[] )
) => void;
onSelectionChange: setSelection;
data: Item[];
}

Expand Down

0 comments on commit 3c453a9

Please sign in to comment.