Skip to content

Commit c5a0875

Browse files
committed
PostTypeSupportCheck: Handle support keys sub-features
1 parent 4c01552 commit c5a0875

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

packages/editor/src/components/post-type-support-check/index.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ import { store as coreStore } from '@wordpress/core-data';
99
*/
1010
import { store as editorStore } from '../../store';
1111

12+
function checkSupport( supports = {}, key ) {
13+
// Check for top-level support keys.
14+
if ( supports[ key ] !== undefined ) {
15+
return !! supports[ key ];
16+
}
17+
18+
const [ topKey, subKey ] = key.split( '.' );
19+
const [ subProperties ] = Array.isArray( supports[ topKey ] )
20+
? supports[ topKey ]
21+
: [];
22+
23+
return Array.isArray( subProperties )
24+
? subProperties.includes( subKey )
25+
: !! subProperties?.[ subKey ];
26+
}
27+
1228
/**
1329
* A component which renders its own children only if the current editor post
1430
* type supports one of the given `supportKeys` prop.
@@ -31,7 +47,7 @@ function PostTypeSupportCheck( { children, supportKeys } ) {
3147
if ( postType ) {
3248
isSupported = (
3349
Array.isArray( supportKeys ) ? supportKeys : [ supportKeys ]
34-
).some( ( key ) => !! postType.supports[ key ] );
50+
).some( ( key ) => checkSupport( postType.supports, key ) );
3551
}
3652

3753
if ( ! isSupported ) {

packages/editor/src/components/post-type-support-check/test/index.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,41 @@ describe( 'PostTypeSupportCheck', () => {
9696

9797
expect( container ).not.toHaveTextContent( 'Supported' );
9898
} );
99+
100+
it( 'renders its children when post type supports a sub-feature', () => {
101+
setupUseSelectMock( {
102+
supports: {
103+
editor: [ [ 'block-comments' ] ],
104+
},
105+
} );
106+
const { container } = render(
107+
<PostTypeSupportCheck supportKeys="editor.block-comments">
108+
Supported
109+
</PostTypeSupportCheck>
110+
);
111+
112+
expect( container ).toHaveTextContent( 'Supported' );
113+
} );
114+
115+
it( 'renders its children when post type supports some of the sub-features', () => {
116+
setupUseSelectMock( {
117+
supports: {
118+
editor: [ [ 'block-comments' ] ],
119+
test: [
120+
{
121+
example: false,
122+
},
123+
],
124+
},
125+
} );
126+
const { container } = render(
127+
<PostTypeSupportCheck
128+
supportKeys={ [ 'editor.block-comments', 'test.example' ] }
129+
>
130+
Supported
131+
</PostTypeSupportCheck>
132+
);
133+
134+
expect( container ).toHaveTextContent( 'Supported' );
135+
} );
99136
} );

0 commit comments

Comments
 (0)