admin管理员组文章数量:1335840
I have this custom post type with two meta fields that I need to show as toggle controls (RestrictControl
and SubscribeControl
) in the PluginDocumentSettingPanel
sidebar in the Gutenberg editor. The thing is that I only want to show SubscribeControl
when RestrictControl
is set. This is the code I've got currently but it's not working. When I toggle RestrictControl
on and off both controls are always displayed.
Any ideas?
let RestrictControl = ({ restrict, onUpdateRestrict }) => (
<ToggleControl
label={ __( 'Restrict viewing?' ) }
checked={ restrict }
onChange={ restrict => onUpdateRestrict( restrict ) }
/>
);
let SubscribeControl = ({ subscribe, onUpdateSubscribe }) => (
<ToggleControl
label={ __( 'Add to newsletter?' ) }
checked={ subscribe }
onChange={ subscribe => onUpdateSubscribe( subscribe ) }
/>
);
const OptionsPanel = () => {
const postType = select( 'core/editor' ).getCurrentPostType();
if ( 'custom_type' !== postType ) {
return null;
}
return (
<PluginDocumentSettingPanel
name='options'
title={ __( 'Options' ) }
className='options-panel'
>
<RestrictControl />
{ restrict && (
<SubscribeControl />
) }
</PluginDocumentSettingPanel>
);
}
registerPlugin( 'options-panel', {
render: OptionsPanel,
})
I have this custom post type with two meta fields that I need to show as toggle controls (RestrictControl
and SubscribeControl
) in the PluginDocumentSettingPanel
sidebar in the Gutenberg editor. The thing is that I only want to show SubscribeControl
when RestrictControl
is set. This is the code I've got currently but it's not working. When I toggle RestrictControl
on and off both controls are always displayed.
Any ideas?
let RestrictControl = ({ restrict, onUpdateRestrict }) => (
<ToggleControl
label={ __( 'Restrict viewing?' ) }
checked={ restrict }
onChange={ restrict => onUpdateRestrict( restrict ) }
/>
);
let SubscribeControl = ({ subscribe, onUpdateSubscribe }) => (
<ToggleControl
label={ __( 'Add to newsletter?' ) }
checked={ subscribe }
onChange={ subscribe => onUpdateSubscribe( subscribe ) }
/>
);
const OptionsPanel = () => {
const postType = select( 'core/editor' ).getCurrentPostType();
if ( 'custom_type' !== postType ) {
return null;
}
return (
<PluginDocumentSettingPanel
name='options'
title={ __( 'Options' ) }
className='options-panel'
>
<RestrictControl />
{ restrict && (
<SubscribeControl />
) }
</PluginDocumentSettingPanel>
);
}
registerPlugin( 'options-panel', {
render: OptionsPanel,
})
Share
Improve this question
edited May 25, 2020 at 9:35
leemon
asked May 23, 2020 at 11:17
leemonleemon
2,0324 gold badges25 silver badges51 bronze badges
1 Answer
Reset to default 5 +50There are 2 main issues in your code:
What is that
restrict
in yourOptionsPanel
component? Where and how you defined it?Both the
onUpdateRestrict
andonUpdateSubscribe
are not functions, so clicking on the toggle controls will result in a JavaScript error.
And if you can show your complete code, I can probably help you fix your code. However, I suggest you to try the compose
package to build the OptionsPanel
component because doing it that way can greatly help reduce the source and make things quite easy for us.
So with the compose
package, the steps are:
Note: I'm using these two meta keys: restrict_viewing
and subscribe_to_news
, so be sure to change them to the correct ones. (I.e. Use the proper meta keys.)
Load all WordPress dependencies:
import { PluginDocumentSettingPanel } from '@wordpress/edit-post'; import { ToggleControl } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { registerPlugin } from '@wordpress/plugins'; import { withSelect, withDispatch } from '@wordpress/data'; import { compose } from '@wordpress/compose';
Create the primary component which basically simply renders the plugin/sidebar panel plus the two toggle controls (the
RestrictControl
&SubscribeControl
in the question).function OptionsPanelComponent( { // These props are passed by applyWithSelect(). (see step #3) currentPostType, // current post type restrictViewing, // current state of the meta restrict_viewing subscribeToNews, // current state of the meta subscribe_to_news // And these are passed by applyWithDispatch(). (see step #4) onUpdateRestrict, // function which updates the meta restrict_viewing onUpdateSubscribe, // function which updates the meta subscribe_to_news } ) { if ( 'custom_type' !== currentPostType ) { return null; } return ( <PluginDocumentSettingPanel name="options" title={ __( 'Options' ) } className="options-panel" > <ToggleControl label={ __( 'Restrict viewing?' ) } checked={ restrictViewing } onChange={ onUpdateRestrict } /> { restrictViewing && ( <ToggleControl label={ __( 'Add to newsletter?' ) } checked={ subscribeToNews } onChange={ onUpdateSubscribe } /> ) } </PluginDocumentSettingPanel> ); }
Use
wp.data.withSelect
to 'select'/retrieve data from the current post (that's being edited) and pass the data to the above component (OptionsPanelComponent
).Note: The
select
is always passed to the function and thatselect
is equivalent towp.data.select
.const applyWithSelect = withSelect( ( select ) => { const { getEditedPostAttribute, getCurrentPostType } = select( 'core/editor' ); const meta = getEditedPostAttribute( 'meta' ); return { currentPostType: getCurrentPostType(), restrictViewing: meta.restrict_viewing, subscribeToNews: meta.subscribe_to_news, }; } );
Use
wp.data.withDispatch
to define the functions for updating the post data and pass the functions to the component in step #2 above (OptionsPanelComponent
).Note: The
dispatch
is always passed to the function and thatdispatch
is equivalent towp.data.dispatch
.const applyWithDispatch = withDispatch( ( dispatch ) => { const { editPost } = dispatch( 'core/editor' ); return { onUpdateRestrict( restrict ) { const meta = { restrict_viewing: restrict }; editPost( { meta } ); }, onUpdateSubscribe( subscribe ) { const meta = { subscribe_to_news: subscribe }; editPost( { meta } ); }, }; } );
Then finally, use
wpposepose()
to 'compose'/wrap all the three components above (they might be defined usingfunction
orconst
, but they really are components). And this 'composed' component will be the one we pass as therender
callback forwp.plugins.registerPlugin
.const OptionsPanel = compose( applyWithSelect, applyWithDispatch )( OptionsPanelComponent ); registerPlugin( 'options-panel', { render: OptionsPanel, } );
Full Code I used for testing purposes
You can find it here, or you can download the full plugin which I created based on this example.
And I have tried & tested that on WordPress 5.4.1 with the default post
post type with the two meta keys mentioned earlier in this answer.
Alternate Solution
If you don't want to use compose()
, then you can try this one. But I'm adding that just because I thought it would be useful to myself in the future. :)
本文标签: javascriptShow control conditionally in Gutenberg
版权声明:本文标题:javascript - Show control conditionally in Gutenberg 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742398862a2467477.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论