-
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
Changes from 28 commits
ec83c64
24228c2
084b656
cdaba96
b988d54
efd6a62
130c916
5935df8
a9f9741
385e0ff
e78c854
7271f0a
f3a6a9b
9254fd9
6ea1af6
c7fd8c0
e57ffa3
cae5879
9005dd6
e357584
753c053
16f5315
e3777f2
1e99ee9
6daf5f2
353ca40
d4d8004
0a655be
c10be61
e9c62fb
fb601d2
7906c6f
f361400
b61ae18
a5628ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/** | ||
* 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: [ | ||
{ | ||
value: '', | ||
label: __( 'All' ), | ||
}, | ||
...( 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 | ||
aria-disabled={ filters.length === view.filters?.length } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I always thought this behavior should be default in buttons, any reason to require There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately I don't have much context around it, since these changes were discussed before I started working on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call and I was on that discussion as well. I guess now, I may be more on the side of let's try and remove that prop and make it default and see how it goes. |
||
__next40pxDefaultSize={ true } | ||
icon={ plus } | ||
variant="tertiary" | ||
> | ||
{ __( 'Add filter' ) } | ||
</Button> | ||
} | ||
> | ||
{ filters.map( ( filter ) => { | ||
if ( filter.isVisible ) { | ||
return ( | ||
<DropdownMenuItemV2 | ||
key={ filter.field } | ||
disabled={ true } | ||
> | ||
{ filter.name } | ||
</DropdownMenuItemV2> | ||
); | ||
} | ||
|
||
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> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,25 @@ | ||
/** | ||
* 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 | ||
aria-disabled={ view.search === '' && view.filters?.length === 0 } | ||
oandregal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
__next40pxDefaultSize={ true } | ||
youknowriad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
variant="tertiary" | ||
onClick={ () => { | ||
onChangeView( ( currentView ) => ( { | ||
...currentView, | ||
page: 1, | ||
search: '', | ||
filters: [], | ||
} ) ); | ||
} } | ||
> | ||
{ __( 'Reset filters' ) } | ||
</Button> | ||
); | ||
}; |
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.