Skip to content

Commit

Permalink
DataViews: Remove redundant setSelection prop (WordPress#63648)
Browse files Browse the repository at this point in the history
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
  • Loading branch information
3 people authored and carstingaxion committed Jul 18, 2024
1 parent 49e6720 commit 253cb4b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 20 deletions.
3 changes: 2 additions & 1 deletion packages/dataviews/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

### Breaking Changes

- `onSelectionChange` has been renamed to `onChangeSelection`.
- `onSelectionChange` prop has been renamed to `onChangeSelection` and its argument has been updated to be a list of ids.
- `setSelection` prop has been removed. Please use `onChangeSelection` instead.

## 3.0.0 (2024-07-10)

Expand Down
25 changes: 10 additions & 15 deletions packages/dataviews/src/dataviews.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import type {
ViewBaseProps,
SupportedLayouts,
} from './types';
import type { SetSelection, SelectionOrUpdater } from './private-types';
import type { SelectionOrUpdater } from './private-types';

type ItemWithId = { id: string };

Expand All @@ -50,16 +50,13 @@ type DataViewsProps< Item > = {
};
defaultLayouts: SupportedLayouts;
selection?: string[];
setSelection?: SetSelection;
onChangeSelection?: ( items: Item[] ) => void;
onChangeSelection?: ( items: string[] ) => void;
} & ( Item extends ItemWithId
? { getItemId?: ( item: Item ) => string }
: { getItemId: ( item: Item ) => string } );

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

const defaultOnChangeSelection = () => {};

export default function DataViews< Item >( {
view,
onChangeView,
Expand All @@ -73,25 +70,23 @@ export default function DataViews< Item >( {
paginationInfo,
defaultLayouts,
selection: selectionProperty,
setSelection: setSelectionProperty,
onChangeSelection = defaultOnChangeSelection,
onChangeSelection,
}: DataViewsProps< Item > ) {
const [ selectionState, setSelectionState ] = useState< string[] >( [] );
const isUncontrolled =
selectionProperty === undefined || setSelectionProperty === undefined;
selectionProperty === undefined || onChangeSelection === undefined;
const selection = isUncontrolled ? selectionState : selectionProperty;
const setSelection = isUncontrolled
? setSelectionState
: setSelectionProperty;
const [ openedFilter, setOpenedFilter ] = useState< string | null >( null );

function setSelectionWithChange( value: SelectionOrUpdater ) {
const newValue =
typeof value === 'function' ? value( selection ) : value;
onChangeSelection(
data.filter( ( item ) => newValue.includes( getItemId( item ) ) )
);
return setSelection( value );
if ( ! isUncontrolled ) {
setSelectionState( newValue );
}
if ( onChangeSelection ) {
onChangeSelection( newValue );
}
}

const ViewComponent = VIEW_LAYOUTS.find( ( v ) => v.type === view.type )
Expand Down
4 changes: 2 additions & 2 deletions packages/edit-site/src/components/page-templates/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,11 @@ export default function PageTemplates() {
const history = useHistory();
const onChangeSelection = useCallback(
( items ) => {
setSelection( items );
if ( view?.type === LAYOUT_LIST ) {
history.push( {
...params,
postId: items.length === 1 ? items[ 0 ].id : undefined,
postId: items.length === 1 ? items[ 0 ] : undefined,
} );
}
},
Expand Down Expand Up @@ -374,7 +375,6 @@ export default function PageTemplates() {
onChangeView={ onChangeView }
onChangeSelection={ onChangeSelection }
selection={ selection }
setSelection={ setSelection }
defaultLayouts={ defaultLayouts }
/>
</Page>
Expand Down
4 changes: 2 additions & 2 deletions packages/edit-site/src/components/post-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,15 @@ export default function PostList( { postType } ) {
const [ selection, setSelection ] = useState( [ postId ] );
const onChangeSelection = useCallback(
( items ) => {
setSelection( items );
const { params } = history.getLocationWithParams();
if (
( params.isCustom ?? 'false' ) === 'false' &&
view?.type === LAYOUT_LIST
) {
history.push( {
...params,
postId: items.length === 1 ? items[ 0 ].id : undefined,
postId: items.length === 1 ? items[ 0 ] : undefined,
} );
}
},
Expand Down Expand Up @@ -611,7 +612,6 @@ export default function PostList( { postType } ) {
view={ view }
onChangeView={ setView }
selection={ selection }
setSelection={ setSelection }
onChangeSelection={ onChangeSelection }
getItemId={ getItemId }
defaultLayouts={ defaultLayouts }
Expand Down

0 comments on commit 253cb4b

Please sign in to comment.