Skip to content
Closed
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
9 changes: 9 additions & 0 deletions packages/dataviews/src/filters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ function _Filters< Item extends AnyItem >( {
return;
}

// Check if this filter is applied by the default view.
const isPreAppliedFilter = view.initialFilters.some(
( f ) =>
f.field === field.id && ALL_OPERATORS.includes( f.operator )
);
if ( isPreAppliedFilter ) {
return;
}

const operators = sanitizeOperators( field );
if ( operators.length === 0 ) {
return;
Expand Down
14 changes: 8 additions & 6 deletions packages/dataviews/src/reset-filters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ export default function ResetFilter( {
( _filter ) => _filter.field === field && _filter.isPrimary
);
const isDisabled =
! view.search &&
! view.filters?.some(
( _filter ) =>
_filter.value !== undefined || ! isPrimary( _filter.field )
);
( ! view.search &&
! view.filters?.some(
( _filter ) =>
_filter.value !== undefined || ! isPrimary( _filter.field )
) ) ||
view.filters === view.initialFilters;

return (
<Button
disabled={ isDisabled }
Expand All @@ -42,7 +44,7 @@ export default function ResetFilter( {
...view,
page: 1,
search: '',
filters: [],
filters: view.initialFilters,
} );
} }
>
Expand Down
5 changes: 5 additions & 0 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ interface ViewBase {
*/
filters: Filter[];

/**
* Initial filters of the view.
*/
initialFilters: Filter[];

/**
* The sorting configuration.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/edit-site/src/components/page-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function useView( postType ) {
DEFAULT_VIEWS[ postType ].find(
( { slug } ) => slug === activeView
)?.view;
defaultView.initialFilters = defaultView.filters;
if ( isCustom === 'false' && layout ) {
return {
...defaultView,
Expand Down
34 changes: 20 additions & 14 deletions packages/edit-site/src/components/page-templates/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export default function PageTemplates() {
const { activeView = 'all', layout } = params;
const defaultView = useMemo( () => {
const usedType = layout ?? DEFAULT_VIEW.type;
return {
const view = {
...DEFAULT_VIEW,
type: usedType,
layout: defaultConfigPerViewType[ usedType ],
Expand All @@ -204,22 +204,28 @@ export default function PageTemplates() {
]
: [],
};
view.initialFilters = view.filters;
return view;
}, [ layout, activeView ] );
const [ view, setView ] = useState( defaultView );
useEffect( () => {
setView( ( currentView ) => ( {
...currentView,
filters:
activeView !== 'all'
? [
{
field: 'author',
operator: OPERATOR_IS_ANY,
value: [ activeView ],
},
]
: [],
} ) );
setView( ( currentView ) => {
const newView = {
...currentView,
filters:
activeView !== 'all'
? [
{
field: 'author',
operator: OPERATOR_IS_ANY,
value: [ activeView ],
},
]
: [],
};
newView.initialFilters = newView.filters;
return newView;
} );
}, [ activeView ] );

const { records, isResolving: isLoadingData } = useEntityRecords(
Expand Down