Skip to content

Commit

Permalink
Refactor "Add filter" dataviews menu (#67634)
Browse files Browse the repository at this point in the history
  • Loading branch information
ciampo committed Dec 12, 2024
1 parent 0b022f5 commit 4182a36
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 40 deletions.
82 changes: 43 additions & 39 deletions packages/dataviews/src/components/dataviews-filters/add-filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,40 @@ export function AddFilterMenu( {
view,
onChangeView,
setOpenedFilter,
trigger,
triggerProps,
}: AddFilterProps & {
trigger: React.ReactNode;
triggerProps: React.ComponentProps< typeof Menu.TriggerButton >;
} ) {
const inactiveFilters = filters.filter( ( filter ) => ! filter.isVisible );
return (
<Menu trigger={ trigger }>
{ inactiveFilters.map( ( filter ) => {
return (
<Menu.Item
key={ filter.field }
onClick={ () => {
setOpenedFilter( filter.field );
onChangeView( {
...view,
page: 1,
filters: [
...( view.filters || [] ),
{
field: filter.field,
value: undefined,
operator: filter.operators[ 0 ],
},
],
} );
} }
>
<Menu.ItemLabel>{ filter.name }</Menu.ItemLabel>
</Menu.Item>
);
} ) }
<Menu>
<Menu.TriggerButton { ...triggerProps } />
<Menu.Popover>
{ inactiveFilters.map( ( filter ) => {
return (
<Menu.Item
key={ filter.field }
onClick={ () => {
setOpenedFilter( filter.field );
onChangeView( {
...view,
page: 1,
filters: [
...( view.filters || [] ),
{
field: filter.field,
value: undefined,
operator: filter.operators[ 0 ],
},
],
} );
} }
>
<Menu.ItemLabel>{ filter.name }</Menu.ItemLabel>
</Menu.Item>
);
} ) }
</Menu.Popover>
</Menu>
);
}
Expand All @@ -78,18 +81,19 @@ function AddFilter(
const inactiveFilters = filters.filter( ( filter ) => ! filter.isVisible );
return (
<AddFilterMenu
trigger={
<Button
accessibleWhenDisabled
size="compact"
className="dataviews-filters-button"
variant="tertiary"
disabled={ ! inactiveFilters.length }
ref={ ref }
>
{ __( 'Add filter' ) }
</Button>
}
triggerProps={ {
render: (
<Button
accessibleWhenDisabled
size="compact"
className="dataviews-filters-button"
variant="tertiary"
disabled={ ! inactiveFilters.length }
ref={ ref }
/>
),
children: __( 'Add filter' ),
} }
{ ...{ filters, view, onChangeView, setOpenedFilter } }
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export function FiltersToggle( {
view={ view }
onChangeView={ onChangeViewWithFilterVisibility }
setOpenedFilter={ setOpenedFilter }
trigger={ buttonComponent }
triggerProps={ { render: buttonComponent } }
/>
) : (
<FilterVisibilityToggle
Expand Down

0 comments on commit 4182a36

Please sign in to comment.