Skip to content

Commit

Permalink
Rename onSelectionChange to onChangeSelection
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Jul 3, 2024
1 parent e39ae77 commit 0dcb62e
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 48 deletions.
2 changes: 2 additions & 0 deletions packages/dataviews/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- `onSelectionChange` has been renamed to `onChangeSelection`.

## 2.2.0 (2024-06-26)

## 2.1.0 (2024-06-15)
Expand Down
2 changes: 1 addition & 1 deletion packages/dataviews/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ Whether the data is loading. `false` by default.

Array of layouts supported. By default, all are: `table`, `grid`, `list`.

### `onSelectionChange`: `function`
### `onChangeSelection`: `function`

Callback that signals the user selected one of more items, and takes them as parameter. So far, only the `list` view implements it.

Expand Down
18 changes: 9 additions & 9 deletions packages/dataviews/src/bulk-actions-toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ interface ToolbarContentProps< Item > {
selection: string[];
actionsToShow: Action< Item >[];
selectedItems: Item[];
onSelectionChange: SetSelection;
onChangeSelection: SetSelection;
}

interface BulkActionsToolbarProps< Item > {
data: Item[];
selection: string[];
actions: Action< Item >[];
onSelectionChange: SetSelection;
onChangeSelection: SetSelection;
getItemId: ( item: Item ) => string;
}

Expand Down Expand Up @@ -132,7 +132,7 @@ function renderToolbarContent< Item >(
selectedItems: Item[],
actionInProgress: string | null,
setActionInProgress: ( actionId: string | null ) => void,
onSelectionChange: SetSelection
onChangeSelection: SetSelection
) {
return (
<>
Expand Down Expand Up @@ -172,7 +172,7 @@ function renderToolbarContent< Item >(
label={ __( 'Cancel' ) }
disabled={ !! actionInProgress }
onClick={ () => {
onSelectionChange( EMPTY_ARRAY );
onChangeSelection( EMPTY_ARRAY );
} }
/>
</ToolbarGroup>
Expand All @@ -184,7 +184,7 @@ function ToolbarContent< Item >( {
selection,
actionsToShow,
selectedItems,
onSelectionChange,
onChangeSelection,
}: ToolbarContentProps< Item > ) {
const [ actionInProgress, setActionInProgress ] = useState< string | null >(
null
Expand All @@ -200,7 +200,7 @@ function ToolbarContent< Item >( {
selectedItems,
actionInProgress,
setActionInProgress,
onSelectionChange
onChangeSelection
);
} else if ( ! buttons.current ) {
buttons.current = renderToolbarContent(
Expand All @@ -209,7 +209,7 @@ function ToolbarContent< Item >( {
selectedItems,
actionInProgress,
setActionInProgress,
onSelectionChange
onChangeSelection
);
}
return buttons.current;
Expand All @@ -219,7 +219,7 @@ export default function BulkActionsToolbar< Item >( {
data,
selection,
actions = EMPTY_ARRAY,
onSelectionChange,
onChangeSelection,
getItemId,
}: BulkActionsToolbarProps< Item > ) {
const isReducedMotion = useReducedMotion();
Expand Down Expand Up @@ -267,7 +267,7 @@ export default function BulkActionsToolbar< Item >( {
selection={ selection }
actionsToShow={ actionsToShow }
selectedItems={ selectedItems }
onSelectionChange={ onSelectionChange }
onChangeSelection={ onChangeSelection }
/>
</div>
</Toolbar>
Expand Down
8 changes: 4 additions & 4 deletions packages/dataviews/src/bulk-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ interface BulkActionsProps< Item > {
data: Item[];
actions: Action< Item >[];
selection: string[];
onSelectionChange: SetSelection;
onChangeSelection: SetSelection;
getItemId: ( item: Item ) => string;
}

Expand Down Expand Up @@ -177,7 +177,7 @@ export default function BulkActions< Item >( {
data,
actions,
selection,
onSelectionChange,
onChangeSelection,
getItemId,
}: BulkActionsProps< Item > ) {
const bulkActions = useMemo(
Expand Down Expand Up @@ -249,7 +249,7 @@ export default function BulkActions< Item >( {
disabled={ areAllSelected }
hideOnClick={ false }
onClick={ () => {
onSelectionChange(
onChangeSelection(
selectableItems.map( ( item ) =>
getItemId( item )
)
Expand All @@ -263,7 +263,7 @@ export default function BulkActions< Item >( {
disabled={ selection.length === 0 }
hideOnClick={ false }
onClick={ () => {
onSelectionChange( [] );
onChangeSelection( [] );
} }
>
{ __( 'Deselect' ) }
Expand Down
14 changes: 7 additions & 7 deletions packages/dataviews/src/dataviews.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ type DataViewsProps< Item > = {
supportedLayouts: string[];
selection?: string[];
setSelection?: SetSelection;
onSelectionChange?: ( items: Item[] ) => void;
onChangeSelection?: ( items: Item[] ) => void;
} & ( Item extends ItemWithId
? { getItemId?: ( item: Item ) => string }
: { getItemId: ( item: Item ) => string } );

const defaultGetItemId = ( item: ItemWithId ) => item.id;

const defaultOnSelectionChange = () => {};
const defaultOnChangeSelection = () => {};

export default function DataViews< Item >( {
view,
Expand All @@ -68,7 +68,7 @@ export default function DataViews< Item >( {
supportedLayouts,
selection: selectionProperty,
setSelection: setSelectionProperty,
onSelectionChange = defaultOnSelectionChange,
onChangeSelection = defaultOnChangeSelection,
}: DataViewsProps< Item > ) {
const [ selectionState, setSelectionState ] = useState< string[] >( [] );
const isUncontrolled =
Expand All @@ -82,7 +82,7 @@ export default function DataViews< Item >( {
function setSelectionWithChange( value: SelectionOrUpdater ) {
const newValue =
typeof value === 'function' ? value( selection ) : value;
onSelectionChange(
onChangeSelection(
data.filter( ( item ) => newValue.includes( getItemId( item ) ) )
);
return setSelection( value );
Expand Down Expand Up @@ -133,7 +133,7 @@ export default function DataViews< Item >( {
<BulkActions
actions={ actions }
data={ data }
onSelectionChange={ setSelectionWithChange }
onChangeSelection={ setSelectionWithChange }
selection={ _selection }
getItemId={ getItemId }
/>
Expand All @@ -152,7 +152,7 @@ export default function DataViews< Item >( {
getItemId={ getItemId }
isLoading={ isLoading }
onChangeView={ onChangeView }
onSelectionChange={ setSelectionWithChange }
onChangeSelection={ setSelectionWithChange }
selection={ _selection }
setOpenedFilter={ setOpenedFilter }
view={ view }
Expand All @@ -168,7 +168,7 @@ export default function DataViews< Item >( {
data={ data }
actions={ actions }
selection={ _selection }
onSelectionChange={ setSelectionWithChange }
onChangeSelection={ setSelectionWithChange }
getItemId={ getItemId }
/>
) }
Expand Down
6 changes: 3 additions & 3 deletions packages/dataviews/src/single-selection-checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { SetSelection } from './private-types';

interface SingleSelectionCheckboxProps< Item > {
selection: string[];
onSelectionChange: SetSelection;
onChangeSelection: SetSelection;
item: Item;
getItemId: ( item: Item ) => string;
primaryField?: Field< Item >;
Expand All @@ -21,7 +21,7 @@ interface SingleSelectionCheckboxProps< Item > {

export default function SingleSelectionCheckbox< Item >( {
selection,
onSelectionChange,
onChangeSelection,
item,
getItemId,
primaryField,
Expand Down Expand Up @@ -54,7 +54,7 @@ export default function SingleSelectionCheckbox< Item >( {
return;
}

onSelectionChange(
onChangeSelection(
selection.includes( id )
? selection.filter( ( itemId ) => id !== itemId )
: [ ...selection, id ]
Expand Down
2 changes: 1 addition & 1 deletion packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ export interface ViewBaseProps< Item > {
getItemId: ( item: Item ) => string;
isLoading?: boolean;
onChangeView( view: View ): void;
onSelectionChange: SetSelection;
onChangeSelection: SetSelection;
selection: string[];
setOpenedFilter: ( fieldId: string ) => void;
view: View;
Expand Down
12 changes: 6 additions & 6 deletions packages/dataviews/src/view-grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import type { SetSelection } from './private-types';

interface GridItemProps< Item > {
selection: string[];
onSelectionChange: SetSelection;
onChangeSelection: SetSelection;
getItemId: ( item: Item ) => string;
item: Item;
actions: Action< Item >[];
Expand All @@ -40,7 +40,7 @@ interface GridItemProps< Item > {

function GridItem< Item >( {
selection,
onSelectionChange,
onChangeSelection,
getItemId,
item,
actions,
Expand All @@ -67,7 +67,7 @@ function GridItem< Item >( {
if ( ! hasBulkAction ) {
return;
}
onSelectionChange(
onChangeSelection(
selection.includes( id )
? selection.filter( ( itemId ) => id !== itemId )
: [ ...selection, id ]
Expand All @@ -85,7 +85,7 @@ function GridItem< Item >( {
<SingleSelectionCheckbox
item={ item }
selection={ selection }
onSelectionChange={ onSelectionChange }
onChangeSelection={ onChangeSelection }
getItemId={ getItemId }
primaryField={ primaryField }
disabled={ ! hasBulkAction }
Expand Down Expand Up @@ -175,7 +175,7 @@ export default function ViewGrid< Item >( {
fields,
getItemId,
isLoading,
onSelectionChange,
onChangeSelection,
selection,
view,
}: ViewGridProps< Item > ) {
Expand Down Expand Up @@ -221,7 +221,7 @@ export default function ViewGrid< Item >( {
<GridItem
key={ getItemId( item ) }
selection={ selection }
onSelectionChange={ onSelectionChange }
onChangeSelection={ onChangeSelection }
getItemId={ getItemId }
item={ item }
actions={ actions }
Expand Down
4 changes: 2 additions & 2 deletions packages/dataviews/src/view-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ export default function ViewList< Item >( props: ViewListProps< Item > ) {
fields,
getItemId,
isLoading,
onSelectionChange,
onChangeSelection,
selection,
view,
} = props;
Expand All @@ -334,7 +334,7 @@ export default function ViewList< Item >( props: ViewListProps< Item > ) {
);

const onSelect = ( item: Item ) =>
onSelectionChange( [ getItemId( item ) ] );
onChangeSelection( [ getItemId( item ) ] );

const getItemDomId = useCallback(
( item?: Item ) =>
Expand Down
22 changes: 11 additions & 11 deletions packages/dataviews/src/view-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ interface HeaderMenuProps< Item > {

interface BulkSelectionCheckboxProps< Item > {
selection: string[];
onSelectionChange: SetSelection;
onChangeSelection: SetSelection;
data: Item[];
actions: Action< Item >[];
getItemId: ( item: Item ) => string;
Expand All @@ -87,7 +87,7 @@ interface TableRowProps< Item > {
primaryField?: NormalizedField< Item >;
selection: string[];
getItemId: ( item: Item ) => string;
onSelectionChange: SetSelection;
onChangeSelection: SetSelection;
}

function WithDropDownMenuSeparators( { children }: { children: ReactNode } ) {
Expand Down Expand Up @@ -246,7 +246,7 @@ const HeaderMenu: < Item >(

function BulkSelectionCheckbox< Item >( {
selection,
onSelectionChange,
onChangeSelection,
data,
actions,
getItemId,
Expand Down Expand Up @@ -274,9 +274,9 @@ function BulkSelectionCheckbox< Item >( {
indeterminate={ ! areAllSelected && !! selectedItems.length }
onChange={ () => {
if ( areAllSelected ) {
onSelectionChange( [] );
onChangeSelection( [] );
} else {
onSelectionChange(
onChangeSelection(
selectableItems.map( ( item ) => getItemId( item ) )
);
}
Expand All @@ -297,7 +297,7 @@ function TableRow< Item >( {
primaryField,
selection,
getItemId,
onSelectionChange,
onChangeSelection,
}: TableRowProps< Item > ) {
const hasPossibleBulkAction = useHasAPossibleBulkAction( actions, item );
const isSelected = hasPossibleBulkAction && selection.includes( id );
Expand Down Expand Up @@ -337,7 +337,7 @@ function TableRow< Item >( {
! isTouchDevice.current &&
document.getSelection()?.type !== 'Range'
) {
onSelectionChange(
onChangeSelection(
selection.includes( id )
? selection.filter( ( itemId ) => id !== itemId )
: [ ...selection, id ]
Expand All @@ -356,7 +356,7 @@ function TableRow< Item >( {
<SingleSelectionCheckbox
item={ item }
selection={ selection }
onSelectionChange={ onSelectionChange }
onChangeSelection={ onChangeSelection }
getItemId={ getItemId }
primaryField={ primaryField }
disabled={ ! hasPossibleBulkAction }
Expand Down Expand Up @@ -415,7 +415,7 @@ function ViewTable< Item >( {
getItemId,
isLoading = false,
onChangeView,
onSelectionChange,
onChangeSelection,
selection,
setOpenedFilter,
view,
Expand Down Expand Up @@ -485,7 +485,7 @@ function ViewTable< Item >( {
>
<BulkSelectionCheckbox
selection={ selection }
onSelectionChange={ onSelectionChange }
onChangeSelection={ onChangeSelection }
data={ data }
actions={ actions }
getItemId={ getItemId }
Expand Down Expand Up @@ -562,7 +562,7 @@ function ViewTable< Item >( {
primaryField={ primaryField }
selection={ selection }
getItemId={ getItemId }
onSelectionChange={ onSelectionChange }
onChangeSelection={ onChangeSelection }
/>
) ) }
</tbody>
Expand Down
Loading

0 comments on commit 0dcb62e

Please sign in to comment.