diff --git a/packages/dataviews/src/bulk-actions-toolbar.tsx b/packages/dataviews/src/bulk-actions-toolbar.tsx index e6b8683832fd27..d4cc3042b63bfe 100644 --- a/packages/dataviews/src/bulk-actions-toolbar.tsx +++ b/packages/dataviews/src/bulk-actions-toolbar.tsx @@ -18,8 +18,9 @@ import { useRegistry } from '@wordpress/data'; * Internal dependencies */ import { ActionWithModal } from './item-actions'; -import type { Action, AnyItem, setSelection } from './types'; +import type { Action, AnyItem } from './types'; import type { ActionTriggerProps } from './item-actions'; +import type { SetSelection } from './private-types'; interface ActionButtonProps< Item extends AnyItem > { action: Action< Item >; @@ -32,14 +33,14 @@ interface ToolbarContentProps< Item extends AnyItem > { selection: string[]; actionsToShow: Action< Item >[]; selectedItems: Item[]; - onSelectionChange: setSelection; + onSelectionChange: SetSelection; } interface BulkActionsToolbarProps< Item extends AnyItem > { data: Item[]; selection: string[]; actions: Action< Item >[]; - onSelectionChange: setSelection; + onSelectionChange: SetSelection; getItemId: ( item: Item ) => string; } @@ -131,7 +132,7 @@ function renderToolbarContent< Item extends AnyItem >( selectedItems: Item[], actionInProgress: string | null, setActionInProgress: ( actionId: string | null ) => void, - onSelectionChange: setSelection + onSelectionChange: SetSelection ) { return ( <> diff --git a/packages/dataviews/src/bulk-actions.tsx b/packages/dataviews/src/bulk-actions.tsx index eedd216bb4e361..52a1f8be4792d0 100644 --- a/packages/dataviews/src/bulk-actions.tsx +++ b/packages/dataviews/src/bulk-actions.tsx @@ -14,7 +14,8 @@ import { useRegistry } from '@wordpress/data'; * Internal dependencies */ import { unlock } from './lock-unlock'; -import type { Action, ActionModal, AnyItem, setSelection } from './types'; +import type { Action, ActionModal, AnyItem } from './types'; +import type { SetSelection } from './private-types'; const { DropdownMenuV2: DropdownMenu, @@ -46,7 +47,7 @@ interface BulkActionsProps< Item extends AnyItem > { data: Item[]; actions: Action< Item >[]; selection: string[]; - onSelectionChange: setSelection; + onSelectionChange: SetSelection; getItemId: ( item: Item ) => string; } diff --git a/packages/dataviews/src/dataviews.tsx b/packages/dataviews/src/dataviews.tsx index 836046b27f23cd..9d35c7e5f6d8e8 100644 --- a/packages/dataviews/src/dataviews.tsx +++ b/packages/dataviews/src/dataviews.tsx @@ -21,14 +21,8 @@ import { VIEW_LAYOUTS } from './layouts'; import BulkActions from './bulk-actions'; import { normalizeFields } from './normalize-fields'; import BulkActionsToolbar from './bulk-actions-toolbar'; -import type { - setSelection, - Action, - AnyItem, - Field, - View, - ViewBaseProps, -} from './types'; +import type { Action, AnyItem, Field, View, ViewBaseProps } from './types'; +import type { SetSelection, SelectionOrUpdater } from './private-types'; interface DataViewsProps< Item extends AnyItem > { view: View; @@ -46,7 +40,7 @@ interface DataViewsProps< Item extends AnyItem > { }; supportedLayouts: string[]; selection?: string[]; - setSelection?: setSelection; + setSelection?: SetSelection; onSelectionChange?: ( items: Item[] ) => void; } @@ -94,9 +88,7 @@ export default function DataViews< Item extends AnyItem >( { : setSelectionProperty; const [ openedFilter, setOpenedFilter ] = useState< string | null >( null ); - function setSelectionWithChange( - value: string[] | ( ( prevValue: string[] ) => string[] ) - ) { + function setSelectionWithChange( value: SelectionOrUpdater ) { const newValue = typeof value === 'function' ? value( selection ) : value; onSelectionChange( diff --git a/packages/dataviews/src/private-types.tsx b/packages/dataviews/src/private-types.tsx new file mode 100644 index 00000000000000..d2d453e63599c9 --- /dev/null +++ b/packages/dataviews/src/private-types.tsx @@ -0,0 +1,2 @@ +export type SelectionOrUpdater = string[] | ( ( prev: string[] ) => string[] ); +export type SetSelection = ( selection: SelectionOrUpdater ) => void; diff --git a/packages/dataviews/src/single-selection-checkbox.tsx b/packages/dataviews/src/single-selection-checkbox.tsx index 96afa98334589d..31b7140b84aabe 100644 --- a/packages/dataviews/src/single-selection-checkbox.tsx +++ b/packages/dataviews/src/single-selection-checkbox.tsx @@ -7,11 +7,12 @@ import { CheckboxControl } from '@wordpress/components'; /** * Internal dependencies */ -import type { Field, AnyItem, setSelection } from './types'; +import type { Field, AnyItem } from './types'; +import type { SetSelection } from './private-types'; interface SingleSelectionCheckboxProps< Item extends AnyItem > { selection: string[]; - onSelectionChange: setSelection; + onSelectionChange: SetSelection; item: Item; getItemId: ( item: Item ) => string; primaryField?: Field< Item >; diff --git a/packages/dataviews/src/types.ts b/packages/dataviews/src/types.ts index 3e243605c210d2..ff06d6600f4fa2 100644 --- a/packages/dataviews/src/types.ts +++ b/packages/dataviews/src/types.ts @@ -1,9 +1,12 @@ /** * External dependencies */ -import type { ReactElement, ReactNode, Dispatch, SetStateAction } from 'react'; +import type { ReactElement, ReactNode } from 'react'; -export type setSelection = Dispatch< SetStateAction< string[] > >; +/** + * Internal dependencies + */ +import type { SetSelection } from './private-types'; export type SortDirection = 'asc' | 'desc'; @@ -378,7 +381,7 @@ export interface ViewBaseProps< Item extends AnyItem > { getItemId: ( item: Item ) => string; isLoading?: boolean; onChangeView( view: View ): void; - onSelectionChange: setSelection; + onSelectionChange: SetSelection; selection: string[]; setOpenedFilter: ( fieldId: string ) => void; view: View; diff --git a/packages/dataviews/src/view-grid.tsx b/packages/dataviews/src/view-grid.tsx index bee451f37a12a3..a05c65a8f6edd3 100644 --- a/packages/dataviews/src/view-grid.tsx +++ b/packages/dataviews/src/view-grid.tsx @@ -22,17 +22,12 @@ 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, - setSelection, -} from './types'; +import type { Action, AnyItem, NormalizedField, ViewGridProps } from './types'; +import type { SetSelection } from './private-types'; interface GridItemProps< Item extends AnyItem > { selection: string[]; - onSelectionChange: setSelection; + onSelectionChange: SetSelection; getItemId: ( item: Item ) => string; item: Item; actions: Action< Item >[]; diff --git a/packages/dataviews/src/view-table.tsx b/packages/dataviews/src/view-table.tsx index ce1e5f1bd8b6c3..55bc8c2671d4a7 100644 --- a/packages/dataviews/src/view-table.tsx +++ b/packages/dataviews/src/view-table.tsx @@ -51,8 +51,8 @@ import type { SortDirection, ViewTable as ViewTableType, ViewTableProps, - setSelection, } from './types'; +import type { SetSelection } from './private-types'; const { DropdownMenuV2: DropdownMenu, @@ -73,7 +73,7 @@ interface HeaderMenuProps< Item extends AnyItem > { interface BulkSelectionCheckboxProps< Item extends AnyItem > { selection: string[]; - onSelectionChange: setSelection; + onSelectionChange: SetSelection; data: Item[]; actions: Action< Item >[]; getItemId: ( item: Item ) => string; @@ -88,7 +88,7 @@ interface TableRowProps< Item extends AnyItem > { primaryField?: NormalizedField< Item >; selection: string[]; getItemId: ( item: Item ) => string; - onSelectionChange: setSelection; + onSelectionChange: SetSelection; } function WithDropDownMenuSeparators( { children }: { children: ReactNode } ) {