DataViewsPicker renders a dataset allowing users to select one or multiple
items. It shares the layouts, search, and filtering of DataViews but is
geared toward choosing items rather than managing them.
import { DataViewsPicker } from '@wordpress/dataviews';
LinksPermalink to this section
PropsPermalink to this section
| Name | Default | Description |
|---|---|---|
view Required |
The current view configuration: layout type, filters, sorting, pagination, search term, and visible fields. | |
onChangeView Required |
Callback invoked with the new view whenever the user changes it (filtering, sorting, switching layout, changing page, etc.). Consumers own the view state and must store the new value. | |
fields Required |
The fields describing each item’s data: how to get and render a value, plus its sorting and filtering capabilities. | |
actions | [] |
The button actions that can be performed on items. Unlike |
search | true |
Whether the global search input is displayed. |
searchLabel | undefined |
The accessible label and placeholder for the search input. |
data Required |
The dataset to render, already filtered, sorted, and paginated according to the current view. | |
isLoading | false |
Whether the data is loading, in which case a loading state is shown. |
paginationInfo Required |
Pagination totals for the full dataset (not just the current page). | |
defaultLayouts | {
pickerGrid: true,
pickerTable: true,
} |
The layouts the user can switch between, mapping each supported layout
type to view settings applied when switching to it (or |
selection Required |
The currently selected items, as a list of item ids. Selection is always controlled in pickers, so this prop is required. | |
onChangeSelection Required |
Callback invoked with the new list of selected item ids whenever the selection changes. | |
children |
Custom component tree rendered instead of the default layout
composition, using the internal | |
config | { perPageSizes: [ 10, 20, 50, 100 ] } |
Static configuration of the component’s UI. |
itemListLabel |
The accessible label for the list of items rendered by the layout. | |
empty |
Content rendered when the dataset is empty (no items match the current view). | |
onReset |
Callback to reset the view to its initial state, wired to the
“Reset view” button in the view options popover. When provided, the
view options toggle also shows a “modified” indicator. Pass | |
getItemId Required | ( item: ItemWithId ) => item.id |
Returns a unique id for an item. Required when items have no
string |
ExamplesPermalink to this section
DefaultPermalink to this section
const Default = ( {
perPageSizes = [ 10, 25, 50, 100 ],
isMultiselectable,
isGrouped,
infiniteScrollEnabled,
mediaFit,
mediaFitControl,
}: {
perPageSizes: number[];
isMultiselectable: boolean;
isGrouped: boolean;
infiniteScrollEnabled: boolean;
mediaFit?: MediaFit;
mediaFitControl?: boolean;
} ) => (
<DataViewsPickerContent
perPageSizes={ perPageSizes }
isMultiselectable={ isMultiselectable }
isGrouped={ isGrouped }
infiniteScrollEnabled={ infiniteScrollEnabled }
mediaFit={ mediaFit }
mediaFitControl={ mediaFitControl }
/>
);
With ModalPermalink to this section
const WithModal = ( {
perPageSizes = [ 10, 25, 50, 100 ],
isMultiselectable,
isGrouped,
infiniteScrollEnabled,
mediaFit,
mediaFitControl,
}: {
perPageSizes: number[];
isMultiselectable: boolean;
isGrouped: boolean;
infiniteScrollEnabled: boolean;
mediaFit?: MediaFit;
mediaFitControl?: boolean;
} ) => {
const [ isModalOpen, setIsModalOpen ] = useState( false );
const [ selectedItems, setSelectedItems ] = useState< SpaceObject[] >( [] );
const modalActions: ActionButton< SpaceObject >[] = [
{
id: 'cancel',
label: 'Cancel',
supportsBulk: isMultiselectable,
callback() {
setIsModalOpen( false );
},
},
{
id: 'confirm',
label: 'Confirm',
isPrimary: true,
supportsBulk: isMultiselectable,
callback( items ) {
setSelectedItems( items );
setIsModalOpen( false );
},
},
];
return (
<>
<Stack direction="row" justify="left" gap="sm">
<Button
variant="primary"
__next40pxDefaultSize
onClick={ () => setIsModalOpen( true ) }
>
Open Picker Modal
</Button>
<Button
onClick={ () => setSelectedItems( [] ) }
disabled={ ! selectedItems.length }
accessibleWhenDisabled
__next40pxDefaultSize
>
Clear Selection
</Button>
</Stack>
{ selectedItems.length > 0 && (
<p>
Selected:{ ' ' }
{ selectedItems
.map( ( item ) => item.name.title )
.join( ', ' ) }
</p>
) }
{ isModalOpen && (
<>
<style>{ `
.components-modal__content {
padding: 0;
}
.components-modal__frame.is-full-screen .components-modal__content {
margin-bottom: 0;
}
` }</style>
<Modal
title="Select Items"
onRequestClose={ () => setIsModalOpen( false ) }
isFullScreen={ false }
size="fill"
>
<DataViewsPickerContent
perPageSizes={ perPageSizes }
isMultiselectable={ isMultiselectable }
isGrouped={ isGrouped }
infiniteScrollEnabled={ infiniteScrollEnabled }
mediaFit={ mediaFit }
mediaFitControl={ mediaFitControl }
actions={ modalActions }
selection={ selectedItems.map( ( item ) =>
String( item.id )
) }
/>
</Modal>
</>
) }
</>
);
};