Skip to content

Commit

Permalink
Make better choices when using isControlAllowed and only call it once…
Browse files Browse the repository at this point in the history
… for each section.
  • Loading branch information
ryanwelcher authored and ntsekouras committed Dec 20, 2022
1 parent f55f487 commit 4fa1681
Showing 1 changed file with 88 additions and 91 deletions.
179 changes: 88 additions & 91 deletions packages/block-library/src/query/edit/inspector-controls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,21 @@ export default function QueryInspectorControls( {
showColumnsControl ||
showOrderControl ||
showStickyControl;
const showTaxControl =
!! taxonomies?.length &&
isControlAllowed( allowedControls, 'taxQuery' );
const showAuthorControl = isControlAllowed( allowedControls, 'author' );
const showSearchControl = isControlAllowed( allowedControls, 'search' );
const showParentControl =
isControlAllowed( allowedControls, 'parents' ) &&
isPostTypeHierarchical;

const showFiltersPanel =
showTaxControl ||
showAuthorControl ||
showSearchControl ||
showParentControl;

return (
<>
{ showSettingsPanel && (
Expand Down Expand Up @@ -179,98 +194,80 @@ export default function QueryInspectorControls( {
</PanelBody>
</InspectorControls>
) }
{ ! inherit &&
( isControlAllowed( allowedControls, 'taxQuery' ) ||
isControlAllowed( allowedControls, 'author' ) ||
isControlAllowed( allowedControls, 'search' ) ||
isControlAllowed( allowedControls, 'parents' ) ) && (
<InspectorControls>
<ToolsPanel
className="block-library-query-toolspanel__filters"
label={ __( 'Filters' ) }
resetAll={ () => {
setQuery( {
author: '',
parents: [],
search: '',
taxQuery: null,
} );
setQuerySearch( '' );
} }
>
{ !! taxonomies?.length &&
isControlAllowed(
allowedControls,
'taxQuery'
) && (
<ToolsPanelItem
label={ __( 'Taxonomies' ) }
hasValue={ () =>
Object.values(
taxQuery || {}
).some(
( terms ) => !! terms.length
)
}
onDeselect={ () =>
setQuery( { taxQuery: null } )
}
>
<TaxonomyControls
onChange={ setQuery }
query={ query }
/>
</ToolsPanelItem>
) }
{ isControlAllowed( allowedControls, 'author' ) && (
<ToolsPanelItem
hasValue={ () => !! authorIds }
label={ __( 'Authors' ) }
onDeselect={ () =>
setQuery( { author: '' } )
}
>
<AuthorControl
value={ authorIds }
onChange={ setQuery }
/>
</ToolsPanelItem>
) }
{ isControlAllowed( allowedControls, 'search' ) && (
<ToolsPanelItem
hasValue={ () => !! querySearch }
{ ! inherit && showFiltersPanel && (
<InspectorControls>
<ToolsPanel
className="block-library-query-toolspanel__filters"
label={ __( 'Filters' ) }
resetAll={ () => {
setQuery( {
author: '',
parents: [],
search: '',
taxQuery: null,
} );
setQuerySearch( '' );
} }
>
{ showTaxControl && (
<ToolsPanelItem
label={ __( 'Taxonomies' ) }
hasValue={ () =>
Object.values( taxQuery || {} ).some(
( terms ) => !! terms.length
)
}
onDeselect={ () =>
setQuery( { taxQuery: null } )
}
>
<TaxonomyControls
onChange={ setQuery }
query={ query }
/>
</ToolsPanelItem>
) }
{ showAuthorControl && (
<ToolsPanelItem
hasValue={ () => !! authorIds }
label={ __( 'Authors' ) }
onDeselect={ () => setQuery( { author: '' } ) }
>
<AuthorControl
value={ authorIds }
onChange={ setQuery }
/>
</ToolsPanelItem>
) }
{ showSearchControl && (
<ToolsPanelItem
hasValue={ () => !! querySearch }
label={ __( 'Keyword' ) }
onDeselect={ () => setQuerySearch( '' ) }
>
<TextControl
label={ __( 'Keyword' ) }
onDeselect={ () => setQuerySearch( '' ) }
>
<TextControl
label={ __( 'Keyword' ) }
value={ querySearch }
onChange={ setQuerySearch }
/>
</ToolsPanelItem>
) }
{ isPostTypeHierarchical &&
isControlAllowed(
allowedControls,
'parents'
) && (
<ToolsPanelItem
hasValue={ () => !! parents?.length }
label={ __( 'Parents' ) }
onDeselect={ () =>
setQuery( { parents: [] } )
}
>
<ParentControl
parents={ parents }
postType={ postType }
onChange={ setQuery }
/>
</ToolsPanelItem>
) }
</ToolsPanel>
</InspectorControls>
) }
value={ querySearch }
onChange={ setQuerySearch }
/>
</ToolsPanelItem>
) }
{ showParentControl && (
<ToolsPanelItem
hasValue={ () => !! parents?.length }
label={ __( 'Parents' ) }
onDeselect={ () => setQuery( { parents: [] } ) }
>
<ParentControl
parents={ parents }
postType={ postType }
onChange={ setQuery }
/>
</ToolsPanelItem>
) }
</ToolsPanel>
</InspectorControls>
) }
</>
);
}

1 comment on commit 4fa1681

@github-actions
Copy link

@github-actions github-actions bot commented on 4fa1681 Dec 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3738671417
📝 Reported issues:

Please sign in to comment.