Skip to content

Commit

Permalink
add selection label functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Nov 29, 2023
1 parent 5450cac commit 68cc432
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
2 changes: 2 additions & 0 deletions packages/edit-site/src/components/dataviews/dataviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default function DataViews( {
supportedLayouts,
selection,
setSelection,
getSelectionLabel,
} ) {
const ViewComponent = viewTypeMap[ view.type ];
const _fields = useMemo( () => {
Expand Down Expand Up @@ -92,6 +93,7 @@ export default function DataViews( {
isLoading={ isLoading }
selection={ selection }
setSelection={ setSelection }
getSelectionLabel={ getSelectionLabel }
/>

<div>
Expand Down
17 changes: 11 additions & 6 deletions packages/edit-site/src/components/dataviews/view-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ function ViewList( {
paginationInfo,
selection,
setSelection,
getSelectionLabel,
} ) {
const areAllSelected = selection && selection.length === data.length;
const columns = useMemo( () => {
Expand Down Expand Up @@ -280,15 +281,19 @@ function ViewList( {
const item = props.row.original;
const isSelected = selection.includes( item.id );
//console.log({ item, isSelected });
let selectionLabel;
if ( getSelectionLabel ) {
selectionLabel = getSelectionLabel( isSelected, item );
} else {
selectionLabel = isSelected
? __( 'Deselect item' )
: __( 'Select a new item' );
}
return (
<CheckboxControl
__nextHasNoMarginBottom
checked={ isSelected }
label={
isSelected
? __( 'Deselect item' )
: __( 'Select a new item' )
}
label={ selectionLabel }
onChange={ () => {
if ( ! isSelected ) {
const newSelection = [
Expand All @@ -309,7 +314,7 @@ function ViewList( {
},
enableHiding: false,
width: 40,
className: 'dataviews-list-view__selection-column'
className: 'dataviews-list-view__selection-column',
} );
}
if ( actions?.length ) {
Expand Down
15 changes: 14 additions & 1 deletion packages/edit-site/src/components/page-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
__experimentalHeading as Heading,
__experimentalVStack as VStack,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';
import { useEntityRecords, store as coreStore } from '@wordpress/core-data';
import { decodeEntities } from '@wordpress/html-entities';
import { useState, useMemo, useCallback, useEffect } from '@wordpress/element';
Expand Down Expand Up @@ -326,6 +326,19 @@ export default function PagePages() {
onChangeView={ onChangeView }
selection={ selection }
setSelection={ setSelection }
getSelectionLabel={ ( isSelected, item ) =>
isSelected
? sprintf(
// translators: %s: The title of the page.
__( 'Deselect page: %s' ),
item.title?.rendered || item.slug
)
: sprintf(
// translators: %s: The title of the page.
__( 'Select page: %s' ),
item.title?.rendered || item.slug
)
}
/>
</Page>
{ viewTypeSupportsMap[ view.type ].preview && (
Expand Down

0 comments on commit 68cc432

Please sign in to comment.