-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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: allow users to add filters dynamically #55992
Merged
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
ec83c64
Show/Hide filters based on ViewActions menu
oandregal 24228c2
Make author & status filters not hidable
oandregal 084b656
Allow existing filters to be removable per demo purposes
oandregal cdaba96
Remove enableFiltering prop
oandregal b988d54
Add/Remove filters from the new affordance
oandregal efd6a62
Remove FiltersVisibility menu
oandregal 130c916
No filters visible by default
oandregal 5935df8
Add filter value directly from AddFilter affordance
oandregal a9f9741
AddFilter only lists the hidden filters
oandregal 385e0ff
Add new filters from columns if no present
oandregal e78c854
Reset filters also hides them back
oandregal 7271f0a
Fix dropdown opening when trigger disabled
oandregal f3a6a9b
Set visibleFilters from sidebar views
oandregal 9254fd9
Remove visibleFilters and render those that are set
oandregal 6ea1af6
Fix rebase
oandregal c7fd8c0
Use icon
oandregal e57ffa3
Do not remove filter when all is selected
oandregal cae5879
Fix focus loss on adding the last filter
oandregal 9005dd6
Absorb reset filters as part of the AddFilter button
oandregal e357584
Do not render AddFilter if no filters available
oandregal 753c053
Simplify Filters
oandregal 16f5315
Fix + icon
oandregal e3777f2
Reset page upon selecting a filter
oandregal 1e99ee9
Fix alignment for Search & AddFilters
oandregal 6daf5f2
Fix for SelectControl as well
oandregal 353ca40
Use __nextHasNoMarginBottom
oandregal d4d8004
Extract ResetButton to where it was
oandregal 0a655be
Disable AddFilter when no more filters to add
oandregal c10be61
Use disabled and __experimentalIsFocusable instead of aria-disabled i…
oandregal e9c62fb
Remove filter from list if it is being used
oandregal fb601d2
Remove all from the element list
oandregal 7906c6f
Remove true booleans
oandregal f361400
Use compact size for buttons
oandregal b61ae18
Remove unnecessary __next40pxDefaultSize prop in buttons
oandregal a5628ab
Do not show ResetFilters if there are no filters to be used
oandregal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', | ||
oandregal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We no longer need this, the visible filters are derived from the
filters
state.