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

DataViews: Fix focus loss when removing all filters or resetting #67003

Merged
merged 4 commits into from
Nov 14, 2024
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
7 changes: 5 additions & 2 deletions packages/dataviews/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Bug Fixes

- Fix focus loss when removing all filters or resetting ([#67003](https://github.com/WordPress/gutenberg/pull/67003)).

## 4.7.0 (2024-10-30)

## 4.6.0 (2024-10-16)
Expand All @@ -23,8 +27,7 @@
## Internal

- The "move left/move right" controls in the table layout (popup displayed on cliking header) are always visible. ([#64646](https://github.com/WordPress/gutenberg/pull/64646)). Before this, its visibility depending on filters, enableSorting, and enableHiding.
- Filters no longer display the elements' description. ([#64674](https://github.com/WordPress/gutenberg/pull/64674))

- Filters no longer display the elements' description. ([#64674](https://github.com/WordPress/gutenberg/pull/64674))

## Enhancements

Expand Down
111 changes: 73 additions & 38 deletions packages/dataviews/src/components/dataviews-filters/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useRef,
useMemo,
useCallback,
useEffect,
} from '@wordpress/element';
import { __experimentalHStack as HStack, Button } from '@wordpress/components';
import { funnel } from '@wordpress/icons';
Expand Down Expand Up @@ -70,7 +71,7 @@ export function useFilters( fields: NormalizedField< any >[], view: View ) {
}, [ fields, view ] );
}

export function FilterVisibilityToggle( {
export function FiltersToggle( {
filters,
view,
onChangeView,
Expand All @@ -85,6 +86,7 @@ export function FilterVisibilityToggle( {
isShowingFilter: boolean;
setIsShowingFilter: React.Dispatch< React.SetStateAction< boolean > >;
} ) {
const buttonRef = useRef< HTMLButtonElement >( null );
const onChangeViewWithFilterVisibility = useCallback(
( _view: View ) => {
onChangeView( _view );
Expand All @@ -98,48 +100,81 @@ export function FilterVisibilityToggle( {
if ( filters.length === 0 ) {
return null;
}
if ( ! hasVisibleFilters ) {
return (
<AddFilterMenu
filters={ filters }
view={ view }
onChangeView={ onChangeViewWithFilterVisibility }
setOpenedFilter={ setOpenedFilter }
trigger={
<Button
className="dataviews-filters__visibility-toggle"
size="compact"
icon={ funnel }
label={ __( 'Add filter' ) }
isPressed={ false }
aria-expanded={ false }
/>
}
/>
);
}

const addFilterButtonProps = {
label: __( 'Add filter' ),
'aria-expanded': false,
isPressed: false,
};
const toggleFiltersButtonProps = {
label: _x( 'Filter', 'verb' ),
'aria-expanded': isShowingFilter,
isPressed: isShowingFilter,
onClick: () => {
if ( ! isShowingFilter ) {
setOpenedFilter( null );
}
setIsShowingFilter( ! isShowingFilter );
},
};
const buttonComponent = (
<Button
ref={ buttonRef }
className="dataviews-filters__visibility-toggle"
size="compact"
icon={ funnel }
{ ...( hasVisibleFilters
? toggleFiltersButtonProps
: addFilterButtonProps ) }
/>
);
return (
<div className="dataviews-filters__container-visibility-toggle">
<Button
className="dataviews-filters__visibility-toggle"
size="compact"
icon={ funnel }
label={ _x( 'Filter', 'verb' ) }
onClick={ () => {
if ( ! isShowingFilter ) {
setOpenedFilter( null );
}
setIsShowingFilter( ! isShowingFilter );
} }
isPressed={ isShowingFilter }
aria-expanded={ isShowingFilter }
/>
{ hasVisibleFilters && !! view.filters?.length && (
{ ! hasVisibleFilters ? (
<AddFilterMenu
filters={ filters }
view={ view }
onChangeView={ onChangeViewWithFilterVisibility }
setOpenedFilter={ setOpenedFilter }
trigger={ buttonComponent }
/>
) : (
<FilterVisibilityToggle
buttonRef={ buttonRef }
filtersCount={ view.filters?.length }
>
{ buttonComponent }
</FilterVisibilityToggle>
) }
</div>
);
}

function FilterVisibilityToggle( {
buttonRef,
filtersCount,
children,
}: {
buttonRef: React.RefObject< HTMLButtonElement >;
filtersCount?: number;
children: React.ReactNode;
} ) {
// Focus the `add filter` button when unmounts.
useEffect(
() => () => {
buttonRef.current?.focus();
},
[ buttonRef ]
);
return (
<>
{ children }
{ !! filtersCount && (
<span className="dataviews-filters-toggle__count">
{ view.filters?.length }
{ filtersCount }
</span>
) }
</div>
</>
);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/dataviews/src/components/dataviews/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import DataViewsContext from '../dataviews-context';
import {
default as DataViewsFilters,
useFilters,
FilterVisibilityToggle,
FiltersToggle,
} from '../dataviews-filters';
import DataViewsLayout from '../dataviews-layout';
import DataViewsFooter from '../dataviews-footer';
Expand Down Expand Up @@ -135,7 +135,7 @@ export default function DataViews< Item >( {
className="dataviews__search"
>
{ search && <DataViewsSearch label={ searchLabel } /> }
<FilterVisibilityToggle
<FiltersToggle
filters={ filters }
view={ view }
onChangeView={ onChangeView }
Expand Down
Loading