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

Menu granular subcomponents: Refactor "Add filter" dataviews menu #67634

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
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
Loading