| section | extensions | ||||
|---|---|---|---|---|---|
| subsection | Data view | ||||
| id | Toolbar | ||||
| title | Data view toolbar | ||||
| source | react | ||||
| sortValue | 2 | ||||
| propComponents |
|
||||
| sourceLink | https://github.com/patternfly/react-data-view/blob/main/packages/module/patternfly-docs/content/extensions/data-view/examples/Toolbar/Toolbar.md |
import { useMemo } from 'react'; import { BrowserRouter, useSearchParams } from 'react-router-dom'; import { useDataViewPagination, useDataViewSelection, useDataViewFilters } from '@patternfly/react-data-view/dist/dynamic/Hooks'; import { DataView } from '@patternfly/react-data-view/dist/dynamic/DataView'; import { BulkSelect, BulkSelectValue, ErrorState, ResponsiveAction, ResponsiveActions, SkeletonTableHead, SkeletonTableBody } from '@patternfly/react-component-groups'; import { DataViewToolbar } from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar'; import { DataViewTable } from '@patternfly/react-data-view/dist/dynamic/DataViewTable'; import { DataViewFilters } from '@patternfly/react-data-view/dist/dynamic/DataViewFilters'; import { DataViewTextFilter } from '@patternfly/react-data-view/dist/dynamic/DataViewTextFilter'; import { DataViewCheckboxFilter } from '@patternfly/react-data-view/dist/dynamic/DataViewCheckboxFilter';
The data view toolbar component renders a default opinionated data view toolbar above or below the data section.
Data view toolbar can contain pagination, bulk select, filters, actions, or other custom child content. To pass child items to the toolbar, use the toolbar item component or predefined <DataViewToolbar> props for specific use cases.
You can further customize toolbar interactions by referring to the additional documentation:
To support additional user needs, you can pass relevant actions to the toolbar via actions. Add standard PatternFly actions (like buttons) or choose predefined responsive actions which ensure the responsive behavior of multiple actions in 1 toolbar.
To help users navigate data records that span multiple pages, add pagination support to your toolbar.
The data view toolbar can display a pagination using the pagination prop. You can also pass a custom ouiaId to the toolbar for testing purposes. You can also persist pagination values in the URL to make it easier to share or bookmark specific pages of your data.
The useDataViewPagination hook manages the pagination state of the data view.
Initial values:
perPageinitial value.- Optional
pageinitial value. - Optional
searchParamsobject. - Optional
setSearchParamsfunction.
While the hook works seamlessly with the React Router library, you do not need to use it to take advantage of URL persistence. The searchParams and setSearchParams props can be managed using native browser APIs (URLSearchParams and window.history.pushState) or any other routing library of your choice. If you don't pass these two props, the pagination state will be stored internally without the URL usage.
You can also pass custom pageParam or perPageParam names, renaming the pagination parameters in the URL.
The retrieved values are named to match the PatternFly pagination component props.
Return values:
- Current
pagenumber. onSetPageto modify current page.- Items
perPagevalue. onPerPageSelectto modify per page value.
This example uses the URL to persist the pagination state.
To allow users to select data records inside the data view, add selection support that displays a row's selection state.
The data view toolbar can display a bulk selection component by using the predefined component group extension bulk select component.
The useDataViewSelection hook manages the selection state of the data view.
Initial values:
- Optional
initialSelectedarray of record's identifiers selected by default. matchOptionfunction to check if the record is selected.- When no
matchOptionis passed, theArray.prototype.includes()operation is performed on theselectedarray.
- When no
Return values:
selectedarray of currently selected records.isSelectedfunction returning the selection state for the record.onSelectcallback to modify the selection state. This accepts theisSelectingflag (indicates if records are being selected or deselected) anditems(affected records).setSelectedfunction to directly set the selected items array. This is useful for programmatically setting a specific selection state without needing to use theonSelectcallback.
To allow users to filter data records in the data view, add filtering support that displays the applied filter chips.
The data view toolbar can include a set of filters by passing a React node to the filters property. You can use the predefined components <DataViewFilters>, <DataViewTextFilter>, and <DataViewCheckboxFilter> to customize and handle filtering directly in the toolbar. The <DataViewFilters> component is a wrapper that allows conditional filtering using multiple attributes. If you need just a single filter, you can use <DataViewTextFilter>, <DataViewCheckboxFilter>, or a different filter component alone. Props of these filter components are listed in the props section of this page.
You can either pass a value and onChange event to every filter separately, or you can pass values and onChange to the <DataViewFilters> wrapper, which makes them available to its children. Props directly passed to child filters have a higher priority than the "inherited" ones.
The useDataViewFilters hook manages the filter state of the data view. It allows you to define default filter values, synchronize filter state with URL parameters, and handle filter changes efficiently.
Initial values:
initialFiltersobject with default filter values (if the filter param allows multiple values, pass an array).- Optional
searchParamsobject for managing URL-based filter state. - Optional
setSearchParamsfunction to update the URL when filters are modified.
The useDataViewFilters hook works well with the React Router library to support URL-based filtering. Alternatively, you can manage the filter state in the URL using URLSearchParams and window.history.pushState APIs, or other routing libraries. If no URL parameters are provided, the filter state is managed internally.
Return values:
filtersobject representing the current filter values.onSetFiltersfunction to update the filter state.clearAllFiltersfunction to reset all filters to their initial values.
This example demonstrates the setup and usage of filters within the data view. It includes text filters for different attributes, the ability to clear all filters, and persistence of filter state in the URL.