File tree Expand file tree Collapse file tree 2 files changed +54
-1
lines changed
packages/editor/src/components/post-type-support-check Expand file tree Collapse file tree 2 files changed +54
-1
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,22 @@ import { store as coreStore } from '@wordpress/core-data';
9
9
*/
10
10
import { store as editorStore } from '../../store' ;
11
11
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
+
12
28
/**
13
29
* A component which renders its own children only if the current editor post
14
30
* type supports one of the given `supportKeys` prop.
@@ -31,7 +47,7 @@ function PostTypeSupportCheck( { children, supportKeys } ) {
31
47
if ( postType ) {
32
48
isSupported = (
33
49
Array . isArray ( supportKeys ) ? supportKeys : [ supportKeys ]
34
- ) . some ( ( key ) => ! ! postType . supports [ key ] ) ;
50
+ ) . some ( ( key ) => checkSupport ( postType . supports , key ) ) ;
35
51
}
36
52
37
53
if ( ! isSupported ) {
Original file line number Diff line number Diff line change @@ -96,4 +96,41 @@ describe( 'PostTypeSupportCheck', () => {
96
96
97
97
expect ( container ) . not . toHaveTextContent ( 'Supported' ) ;
98
98
} ) ;
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
+ } ) ;
99
136
} ) ;
You can’t perform that action at this time.
0 commit comments