diff --git a/packages/dataviews/CHANGELOG.md b/packages/dataviews/CHANGELOG.md index 397865b1de5183..8229445a68249c 100644 --- a/packages/dataviews/CHANGELOG.md +++ b/packages/dataviews/CHANGELOG.md @@ -15,6 +15,7 @@ ### Enhancements +- DataViews: Add `onReset` prop to control view reset functionality from the view config dropdown. [#75093](https://github.com/WordPress/gutenberg/pull/75093) - DataForm: Add automatic field labeling - forms now automatically mark the minority of fields (required or optional) to reduce visual noise. [#74430](https://github.com/WordPress/gutenberg/pull/74430) - DataViews: Add details form layout validation. [#74996](https://github.com/WordPress/gutenberg/pull/74996) - Add new `adaptiveSelect` DataForm control. [#74937](https://github.com/WordPress/gutenberg/pull/74937) diff --git a/packages/dataviews/README.md b/packages/dataviews/README.md index a0db2568eb2e07..aa7e8ed5718119 100644 --- a/packages/dataviews/README.md +++ b/packages/dataviews/README.md @@ -478,6 +478,33 @@ Optional. Pass an object with a list of `perPageSizes` to control the available An element to display when the `data` prop is empty. Defaults to `

No results

`. +#### `onReset`: `( () => void ) | false` + +Callback function to reset the view to its default state, or `false` to indicate the view is not modified. + +- Type: `function` or `false` +- Optional + +This prop controls the "Reset view" button in the view configuration dropdown: + +- When `undefined` (not provided): No reset functionality is shown. Use this when view persistence is not supported. +- When `false`: The "Reset view" button is shown but disabled. Use this when view persistence is supported but the current view matches the default (not modified). +- When a function: The "Reset view" button is shown and enabled. A blue dot indicator appears on the view options button to signal that the view has been modified. The function is called when the user clicks the reset button. + +Example: + +```jsx +const { view, setView, isModified, resetToDefault } = useView( 'my-view-key' ); + + +``` + ### Styling These are the CSS Custom Properties that can be used to tweak the appearance of the component: diff --git a/packages/dataviews/src/components/dataviews-context/index.ts b/packages/dataviews/src/components/dataviews-context/index.ts index ac1fc1cd024f7b..9061d9ba66c348 100644 --- a/packages/dataviews/src/components/dataviews-context/index.ts +++ b/packages/dataviews/src/components/dataviews-context/index.ts @@ -58,6 +58,7 @@ type DataViewsContextType< Item > = { empty?: ReactNode; hasInfiniteScrollHandler: boolean; itemListLabel?: string; + onReset?: ( () => void ) | false; }; const DataViewsContext = createContext< DataViewsContextType< any > >( { diff --git a/packages/dataviews/src/components/dataviews-view-config/index.tsx b/packages/dataviews/src/components/dataviews-view-config/index.tsx index dbb503b432f590..fe17610cfebe2b 100644 --- a/packages/dataviews/src/components/dataviews-view-config/index.tsx +++ b/packages/dataviews/src/components/dataviews-view-config/index.tsx @@ -14,9 +14,7 @@ import { __experimentalToggleGroupControlOption as ToggleGroupControlOption, __experimentalToggleGroupControlOptionIcon as ToggleGroupControlOptionIcon, SelectControl, - __experimentalGrid as Grid, __experimentalHeading as Heading, - __experimentalText as Text, privateApis as componentsPrivateApis, } from '@wordpress/components'; import { __, _x } from '@wordpress/i18n'; @@ -247,46 +245,36 @@ function ItemsPerPageControl() { ); } -function SettingsSection( { - title, - description, - children, -}: { - title: string; - description?: string; - children: React.ReactNode; -} ) { +function ResetViewButton() { + const { onReset } = useContext( DataViewsContext ); + + // Don't render if no persistence support (onReset is undefined) + if ( onReset === undefined ) { + return null; + } + + const isDisabled = onReset === false; + return ( - -
- - { title } - - { description && ( - - { description } - - ) } -
- - { children } - -
+ ); } export function DataviewsViewConfigDropdown() { - const { view } = useContext( DataViewsContext ); + const { view, onReset } = useContext( DataViewsContext ); const popoverId = useInstanceId( _DataViewsViewConfig, 'dataviews-view-config-dropdown' @@ -294,6 +282,7 @@ export function DataviewsViewConfigDropdown() { const activeLayout = VIEW_LAYOUTS.find( ( layout ) => layout.type === view.type ); + const isModified = typeof onReset === 'function'; return ( { return ( - - ) } - - + } > diff --git a/packages/edit-site/src/components/page-templates/index-legacy.js b/packages/edit-site/src/components/page-templates/index-legacy.js index ff943b2b93f4ac..1eb1ffd858d717 100644 --- a/packages/edit-site/src/components/page-templates/index-legacy.js +++ b/packages/edit-site/src/components/page-templates/index-legacy.js @@ -11,7 +11,6 @@ import { privateApis as editorPrivateApis } from '@wordpress/editor'; import { addQueryArgs } from '@wordpress/url'; import { useEvent } from '@wordpress/compose'; import { useView } from '@wordpress/views'; -import { Button } from '@wordpress/components'; /** * Internal dependencies @@ -134,22 +133,7 @@ export default function PageTemplates() { - { isModified && ( - - ) } - - - } + actions={ } > { + resetToDefault(); + history.invalidate(); + } + : false + } /> ); diff --git a/packages/edit-site/src/components/page-templates/index.js b/packages/edit-site/src/components/page-templates/index.js index aa8d0d2fed4e87..439d6c78d15c78 100644 --- a/packages/edit-site/src/components/page-templates/index.js +++ b/packages/edit-site/src/components/page-templates/index.js @@ -16,7 +16,7 @@ import { addQueryArgs } from '@wordpress/url'; import { useSelect, useDispatch } from '@wordpress/data'; import { useEvent } from '@wordpress/compose'; import { useView } from '@wordpress/views'; -import { Button, Modal } from '@wordpress/components'; +import { Modal } from '@wordpress/components'; import { store as noticesStore } from '@wordpress/notices'; /** @@ -325,22 +325,7 @@ export default function PageTemplates() { - { isModified && ( - - ) } - - - } + actions={ } > { + resetToDefault(); + history.invalidate(); + } + : false + } /> { selectedRegisteredTemplate && duplicateAction && ( - { isModified && ( - - ) } { labels?.add_new_item && canCreateRecord && ( <> - ) } - - + } > { navigate( { search: { diff --git a/routes/pattern-list/stage.tsx b/routes/pattern-list/stage.tsx index 933f2836afd6f9..0a9aa720d60b1d 100644 --- a/routes/pattern-list/stage.tsx +++ b/routes/pattern-list/stage.tsx @@ -263,26 +263,16 @@ function PatternList() { ) } className="pattern-page" actions={ - <> - { isModified && ( - - ) } - { labels?.add_new_item && canCreateRecord && ( - - ) } - + labels?.add_new_item && + canCreateRecord && ( + + ) } hasPadding={ false } > @@ -320,6 +310,7 @@ function PatternList() { } } defaultLayouts={ DEFAULT_LAYOUTS } selection={ selection } + onReset={ isModified ? onReset : false } onChangeSelection={ ( items: string[] ) => { navigate( { search: { diff --git a/routes/post-list/stage.tsx b/routes/post-list/stage.tsx index 5f66aa6c165e4f..358bdda8ea49a3 100644 --- a/routes/post-list/stage.tsx +++ b/routes/post-list/stage.tsx @@ -24,7 +24,6 @@ import { useSelect } from '@wordpress/data'; import { useMemo, useCallback } from '@wordpress/element'; import { privateApis as editorPrivateApis } from '@wordpress/editor'; import type { Post } from '@wordpress/core-data'; -import { __ } from '@wordpress/i18n'; /** * Internal dependencies @@ -277,32 +276,21 @@ function PostList() { subTitle={ postTypeObject.labels?.description } className={ `${ postTypeObject.name.toLowerCase() }-page` } actions={ - <> - { isModified && ( - - ) } - { labels?.add_new_item && - canCreateRecord && - postType !== 'attachment' && ( - - ) } - + labels?.add_new_item && + canCreateRecord && + postType !== 'attachment' && ( + + ) } hasPadding={ false } > @@ -342,6 +330,7 @@ function PostList() { getItemId={ getItemId } getItemLevel={ getItemLevel } selection={ selection } + onReset={ isModified ? onReset : false } onChangeSelection={ ( items: string[] ) => { navigate( { search: { diff --git a/routes/template-list/stage-activation.tsx b/routes/template-list/stage-activation.tsx index 3610a1a8c63238..7785131d5ff252 100644 --- a/routes/template-list/stage-activation.tsx +++ b/routes/template-list/stage-activation.tsx @@ -13,7 +13,6 @@ import { Page } from '@wordpress/admin-ui'; import type { View, Action } from '@wordpress/dataviews'; import { store as coreStore } from '@wordpress/core-data'; import { - Button, Modal, privateApis as componentsPrivateApis, } from '@wordpress/components'; @@ -302,20 +301,7 @@ function TemplateListActivation() { - { isModified && ( - - ) } - - - } + actions={ } hasPadding={ false } > { tabs.length > 1 && ( @@ -345,6 +331,7 @@ function TemplateListActivation() { defaultLayouts={ DEFAULT_LAYOUTS } getItemId={ getItemId } selection={ selection } + onReset={ isModified ? onReset : false } onChangeSelection={ ( items: string[] ) => { navigate( { search: { diff --git a/routes/template-list/stage-legacy.tsx b/routes/template-list/stage-legacy.tsx index 4b42a96f04f135..1786e713a42fc9 100644 --- a/routes/template-list/stage-legacy.tsx +++ b/routes/template-list/stage-legacy.tsx @@ -12,10 +12,7 @@ import { DataViews, filterSortAndPaginate } from '@wordpress/dataviews'; import { Page } from '@wordpress/admin-ui'; import type { View, Action } from '@wordpress/dataviews'; import { store as coreStore } from '@wordpress/core-data'; -import { - Button, - privateApis as componentsPrivateApis, -} from '@wordpress/components'; +import { privateApis as componentsPrivateApis } from '@wordpress/components'; import { useSelect } from '@wordpress/data'; import { useMemo, useCallback } from '@wordpress/element'; import { privateApis as editorPrivateApis } from '@wordpress/editor'; @@ -276,20 +273,7 @@ function TemplateListLegacy() { - { isModified && ( - - ) } - - - } + actions={ } hasPadding={ false } > { tabs.length > 1 && ( @@ -319,6 +303,7 @@ function TemplateListLegacy() { defaultLayouts={ DEFAULT_LAYOUTS } getItemId={ getItemId } selection={ selection } + onReset={ isModified ? onReset : false } onChangeSelection={ ( items: string[] ) => { navigate( { search: { diff --git a/routes/template-part-list/stage.tsx b/routes/template-part-list/stage.tsx index 2aee764cfbad3f..e8b3ef9c48745d 100644 --- a/routes/template-part-list/stage.tsx +++ b/routes/template-part-list/stage.tsx @@ -24,7 +24,6 @@ import { useSelect } from '@wordpress/data'; import { useMemo, useCallback, useState } from '@wordpress/element'; import { privateApis as editorPrivateApis } from '@wordpress/editor'; import type { WpTemplatePart } from '@wordpress/core-data'; -import { __ } from '@wordpress/i18n'; import { CreateTemplatePartModal } from '@wordpress/fields'; /** @@ -254,26 +253,16 @@ function TemplatePartList() { subTitle={ postTypeObject.labels?.description } className="template-part-page" actions={ - <> - { isModified && ( - - ) } - { labels?.add_new_item && canCreateRecord && ( - - ) } - + labels?.add_new_item && + canCreateRecord && ( + + ) } hasPadding={ false } > @@ -312,6 +301,7 @@ function TemplatePartList() { defaultLayouts={ DEFAULT_LAYOUTS } getItemId={ getItemId } selection={ selection } + onReset={ isModified ? onReset : false } onChangeSelection={ ( items: string[] ) => { navigate( { search: { diff --git a/test/e2e/specs/site-editor/pages-view-persistence.spec.js b/test/e2e/specs/site-editor/pages-view-persistence.spec.js index 84b257270ae37e..6cc2e29a5646ee 100644 --- a/test/e2e/specs/site-editor/pages-view-persistence.spec.js +++ b/test/e2e/specs/site-editor/pages-view-persistence.spec.js @@ -26,10 +26,15 @@ test.describe( 'Pages View Persistence', () => { await admin.visitSiteEditor(); await page.getByRole( 'button', { name: 'Pages' } ).click(); - const resetButton = page.getByRole( 'button', { name: 'Reset view' } ); - if ( await resetButton.isVisible() ) { - await resetButton.click(); - await expect( resetButton ).toBeHidden(); + // Check if view is modified by looking for the blue dot indicator + const modifiedIndicator = page.locator( + '.dataviews-view-config__modified-indicator' + ); + if ( await modifiedIndicator.isVisible() ) { + // Open dropdown and reset + await page.getByRole( 'button', { name: 'View options' } ).click(); + await page.getByRole( 'button', { name: 'Reset view' } ).click(); + await expect( modifiedIndicator ).toBeHidden(); } } ); @@ -43,10 +48,11 @@ test.describe( 'Pages View Persistence', () => { // Verify table is visible await expect( page.getByRole( 'table' ) ).toBeVisible(); - // Verify the Reset button appears when view is modified - const resetButton = page.getByRole( 'button', { name: 'Reset view' } ); - await expect( resetButton ).toBeVisible(); - await expect( resetButton ).toBeEnabled(); + // Verify the modified indicator (blue dot) appears when view is modified + const modifiedIndicator = page.locator( + '.dataviews-view-config__modified-indicator' + ); + await expect( modifiedIndicator ).toBeVisible(); // Navigate to Drafts view await page @@ -60,9 +66,8 @@ test.describe( 'Pages View Persistence', () => { // since all tabs share the same persisted view await expect( page.getByRole( 'table' ) ).toBeVisible(); - // Reset button should still be visible on Drafts tab - await expect( resetButton ).toBeVisible(); - await expect( resetButton ).toBeEnabled(); + // Modified indicator should still be visible on Drafts tab + await expect( modifiedIndicator ).toBeVisible(); // Navigate back to All Pages await page @@ -74,15 +79,17 @@ test.describe( 'Pages View Persistence', () => { // Verify table layout persisted await expect( page.getByRole( 'table' ) ).toBeVisible(); - // Verify Reset button is still visible - await expect( resetButton ).toBeVisible(); - await expect( resetButton ).toBeEnabled(); + // Verify modified indicator is still visible + await expect( modifiedIndicator ).toBeVisible(); - // Click the Reset button + // Open dropdown and click the Reset button + await page.getByRole( 'button', { name: 'View options' } ).click(); + const resetButton = page.getByRole( 'button', { name: 'Reset view' } ); + await expect( resetButton ).toBeEnabled(); await resetButton.click(); - // wait for the reset button to be hidden - await expect( resetButton ).toBeHidden(); + // Verify the modified indicator is hidden after reset + await expect( modifiedIndicator ).toBeHidden(); // Verify view returns to list layout await expect( page.getByRole( 'grid' ) ).toBeVisible();