Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: DataViews: Active page is not highlighted properly in list view. #62378

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion packages/dataviews/src/dataviews.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ interface DataViewsProps< Item extends AnyItem > {
totalPages: number;
};
supportedLayouts: string[];
selection?: string[];
setSelection?: ( selection: string[] ) => void;
onSelectionChange?: ( items: Item[] ) => void;
}

Expand Down Expand Up @@ -72,9 +74,22 @@ export default function DataViews< Item extends AnyItem >( {
isLoading = false,
paginationInfo,
supportedLayouts,
selection: selectionProperty,
setSelection: setSelectionProperty,
onSelectionChange = defaultOnSelectionChange,
}: DataViewsProps< Item > ) {
const [ selection, setSelection ] = useState< string[] >( [] );
const [ selectionState, setSelectionState ] = useState< string[] >( [] );
let selection, setSelection;
if (
selectionProperty !== undefined &&
setSelectionProperty !== undefined
) {
selection = selectionProperty;
setSelection = setSelectionProperty;
} else {
selection = selectionState;
setSelection = setSelectionState;
}
const [ openedFilter, setOpenedFilter ] = useState< string | null >( null );

useEffect( () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/dataviews/src/view-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ export default function ViewList< Item extends AnyItem >(
} = props;
const baseId = useInstanceId( ViewList, 'view-list' );
const selectedItem = data?.findLast( ( item ) =>
selection.includes( item.id )
selection.includes( getItemId( item ) )
);

const mediaField = fields.find(
Expand Down
38 changes: 38 additions & 0 deletions packages/edit-site/src/components/page-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,44 @@ function FeaturedImage( { item, viewType } ) {
);
}

function usePostIdLinkInSelection(
selection,
setSelection,
isLoadingItems,
items
) {
const {
params: { postId },
} = useLocation();
const [ postIdToSelect, setPostIdToSelect ] = useState( postId );
useEffect( () => {
if ( postId ) {
setPostIdToSelect( postId );
}
}, [ postId ] );

useEffect( () => {
if ( ! postIdToSelect ) {
return;
}
// Only try to select an item if the loading is complete and we have items.
if ( ! isLoadingItems && items && items.length ) {
// If the item is not in the current selection, select it.
if ( selection.length !== 1 || selection[ 0 ] !== postIdToSelect ) {
setSelection( [ postIdToSelect ] );
}
setPostIdToSelect( undefined );
}
}, [ postIdToSelect, selection, setSelection, isLoadingItems, items ] );
}

export default function PagePages() {
const postType = 'page';
const [ view, setView ] = useView( postType );
const history = useHistory();

const [ selection, setSelection ] = useState( [] );

const onSelectionChange = useCallback(
( items ) => {
const { params } = history.getLocationWithParams();
Expand Down Expand Up @@ -266,6 +299,8 @@ export default function PagePages() {
totalPages,
} = useEntityRecords( 'postType', postType, queryArgs );

usePostIdLinkInSelection( selection, setSelection, isLoadingPages, pages );

const { records: authors, isResolving: isLoadingAuthors } =
useEntityRecords( 'root', 'user', { per_page: -1 } );

Expand Down Expand Up @@ -523,7 +558,10 @@ export default function PagePages() {
isLoading={ isLoadingPages || isLoadingAuthors }
view={ view }
onChangeView={ onChangeView }
selection={ selection }
setSelection={ setSelection }
onSelectionChange={ onSelectionChange }
getItemId={ ( item ) => item.id.toString() }
/>
</Page>
);
Expand Down
Loading