-
Notifications
You must be signed in to change notification settings - Fork 4.7k
DataViews: introduce locked filters #71075
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
Changes from all commits
4253980
299d457
0d1df02
954f70e
5a902bb
8187844
2a92f91
a445d0c
71931f0
166ba06
b69b582
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 |
|---|---|---|
|
|
@@ -36,6 +36,10 @@ export function useFilters( fields: NormalizedField< any >[], view: View ) { | |
|
|
||
| const operators = field.filterBy.operators; | ||
| const isPrimary = !! field.filterBy?.isPrimary; | ||
| const isLocked = | ||
| view.filters?.some( | ||
| ( f ) => f.field === field.id && !! f.isLocked | ||
| ) ?? false; | ||
| filters.push( { | ||
| field: field.id, | ||
| name: field.label, | ||
|
|
@@ -45,18 +49,29 @@ export function useFilters( fields: NormalizedField< any >[], view: View ) { | |
| ), | ||
| operators, | ||
| isVisible: | ||
| isLocked || | ||
| isPrimary || | ||
| !! view.filters?.some( | ||
| ( f ) => | ||
| f.field === field.id && | ||
| ALL_OPERATORS.includes( f.operator ) | ||
| ), | ||
| isPrimary, | ||
| isLocked, | ||
| } ); | ||
|
Member
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 we consider sorting locked filters prominently like primary filters below for a better user experience?
Member
Author
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. Great thinking, thanks. I've made locked filters first, then primary, then the rest at 71931f0 I haven't pushed any other UI changes. |
||
| } ); | ||
| // Sort filters by primary property. We need the primary filters to be first. | ||
| // Then we sort by name. | ||
|
|
||
| // Sort filters by: | ||
| // - locked filters go first | ||
| // - primary filters go next | ||
| // - then, sort by name | ||
| filters.sort( ( a, b ) => { | ||
| if ( a.isLocked && ! b.isLocked ) { | ||
| return -1; | ||
| } | ||
| if ( ! a.isLocked && b.isLocked ) { | ||
| return 1; | ||
| } | ||
| if ( a.isPrimary && ! b.isPrimary ) { | ||
| return -1; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,13 @@ import type { ReactNode, ComponentProps, ReactElement } from 'react'; | |
| * WordPress dependencies | ||
| */ | ||
| import { __experimentalHStack as HStack } from '@wordpress/components'; | ||
| import { useContext, useMemo, useRef, useState } from '@wordpress/element'; | ||
| import { | ||
| useContext, | ||
| useMemo, | ||
| useRef, | ||
| useState, | ||
| useEffect, | ||
| } from '@wordpress/element'; | ||
| import { useResizeObserver } from '@wordpress/compose'; | ||
|
|
||
| /** | ||
|
|
@@ -170,10 +176,23 @@ function DataViews< Item >( { | |
| }, [ selection, data, getItemId ] ); | ||
|
|
||
| const filters = useFilters( _fields, view ); | ||
| const [ isShowingFilter, setIsShowingFilter ] = useState< boolean >( () => | ||
| ( filters || [] ).some( ( filter ) => filter.isPrimary ) | ||
| const hasPrimaryOrLockedFilters = useMemo( | ||
| () => | ||
| ( filters || [] ).some( | ||
| ( filter ) => filter.isPrimary || filter.isLocked | ||
| ), | ||
| [ filters ] | ||
| ); | ||
| const [ isShowingFilter, setIsShowingFilter ] = useState< boolean >( | ||
| hasPrimaryOrLockedFilters | ||
| ); | ||
|
|
||
| useEffect( () => { | ||
| if ( hasPrimaryOrLockedFilters && ! isShowingFilter ) { | ||
| setIsShowingFilter( true ); | ||
|
Member
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. The effect sets
Member
Author
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. The state is previous to this PR, and this change fixes a bug: the current code doesn't consider that fields can be updated (e.g., they are empty when dataviews is mounted and updated later). The bug can be reproduced by having a primary filter in this screen (expected: is visible, actual: is not). I've refactored a bit at b69b582 |
||
| } | ||
| }, [ hasPrimaryOrLockedFilters, isShowingFilter ] ); | ||
|
|
||
| return ( | ||
| <DataViewsContext.Provider | ||
| value={ { | ||
|
|
||
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.
When hovering over the locked status chip, the cursor still appears as a pointer. I would expect a different cursor and for the hover effect to be disabled altogether. Does that make sense?
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.
I updated this at 166ba06 and also prevented locked filters from participating in the tab sequence (makes sense for primary but not for filters you cannot actually interact with).