-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DataViews: allow users to add filters dynamically (#55992)
- Loading branch information
Showing
7 changed files
with
171 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
packages/edit-site/src/components/dataviews/add-filter.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
privateApis as componentsPrivateApis, | ||
Button, | ||
Icon, | ||
} from '@wordpress/components'; | ||
import { chevronRightSmall, plus } from '@wordpress/icons'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { unlock } from '../../lock-unlock'; | ||
import { OPERATOR_IN } from './in-filter'; | ||
|
||
const { | ||
DropdownMenuV2, | ||
DropdownSubMenuV2, | ||
DropdownSubMenuTriggerV2, | ||
DropdownMenuItemV2, | ||
} = unlock( componentsPrivateApis ); | ||
|
||
const VALID_OPERATORS = [ OPERATOR_IN ]; | ||
|
||
export default function AddFilter( { fields, view, onChangeView } ) { | ||
const filters = []; | ||
fields.forEach( ( field ) => { | ||
if ( ! field.filters ) { | ||
return; | ||
} | ||
|
||
field.filters.forEach( ( filter ) => { | ||
if ( VALID_OPERATORS.some( ( operator ) => operator === filter ) ) { | ||
filters.push( { | ||
field: field.id, | ||
name: field.header, | ||
operator: filter, | ||
elements: field.elements || [], | ||
isVisible: view.filters.some( | ||
( f ) => f.field === field.id && f.operator === filter | ||
), | ||
} ); | ||
} | ||
} ); | ||
} ); | ||
|
||
if ( filters.length === 0 ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<DropdownMenuV2 | ||
label={ __( 'Add filter' ) } | ||
trigger={ | ||
<Button | ||
disabled={ filters.length === view.filters?.length } | ||
__experimentalIsFocusable | ||
icon={ plus } | ||
variant="tertiary" | ||
size="compact" | ||
> | ||
{ __( 'Add filter' ) } | ||
</Button> | ||
} | ||
> | ||
{ filters.map( ( filter ) => { | ||
if ( filter.isVisible ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<DropdownSubMenuV2 | ||
key={ filter.field } | ||
trigger={ | ||
<DropdownSubMenuTriggerV2 | ||
suffix={ <Icon icon={ chevronRightSmall } /> } | ||
> | ||
{ filter.name } | ||
</DropdownSubMenuTriggerV2> | ||
} | ||
> | ||
{ filter.elements.map( ( element ) => ( | ||
<DropdownMenuItemV2 | ||
key={ element.value } | ||
onSelect={ () => { | ||
onChangeView( ( currentView ) => ( { | ||
...currentView, | ||
page: 1, | ||
filters: [ | ||
...currentView.filters, | ||
{ | ||
field: filter.field, | ||
operator: 'in', | ||
value: element.value, | ||
}, | ||
], | ||
} ) ); | ||
} } | ||
role="menuitemcheckbox" | ||
> | ||
{ element.label } | ||
</DropdownMenuItemV2> | ||
) ) } | ||
</DropdownSubMenuV2> | ||
); | ||
} ) } | ||
</DropdownMenuV2> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 17 additions & 17 deletions
34
packages/edit-site/src/components/dataviews/reset-filters.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { BaseControl, Button } from '@wordpress/components'; | ||
import { Button } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
export default ( { view, onChangeView } ) => { | ||
return ( | ||
<BaseControl> | ||
<Button | ||
disabled={ view.search === '' && view.filters?.length === 0 } | ||
variant="tertiary" | ||
onClick={ () => { | ||
onChangeView( ( currentView ) => ( { | ||
...currentView, | ||
page: 1, | ||
search: '', | ||
filters: [], | ||
} ) ); | ||
} } | ||
> | ||
{ __( 'Reset filters' ) } | ||
</Button> | ||
</BaseControl> | ||
<Button | ||
disabled={ view.search === '' && view.filters?.length === 0 } | ||
__experimentalIsFocusable | ||
size="compact" | ||
variant="tertiary" | ||
onClick={ () => { | ||
onChangeView( ( currentView ) => ( { | ||
...currentView, | ||
page: 1, | ||
search: '', | ||
filters: [], | ||
} ) ); | ||
} } | ||
> | ||
{ __( 'Reset filters' ) } | ||
</Button> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters