风俗renderAppenders
要求您自己创建插入块的所有功能。这是我不久前创建的一个只允许插入单个块的代码,您可以根据需要修改代码。
/**
* WordPress Imports
*/
const { createBlock } = wp.blocks;
const { Button } = wp.components;
const { withSelect, withDispatch } = wp.data;
const { compose } = wp.compose;
const { __ } = wp.i18n;
/**
* Custom Appender meant to be used when there is only one type of block that can be inserted to an InnerBlocks instance.
*
* @param buttonText
* @param onClick
* @param clientId
* @param allowedBlock
* @param innerBlocks
* @param {Object} props
*/
const SingleBlockTypeAppender = ( { buttonText = __( \'Add Item\' ), onClick, clientId, allowedBlock, innerBlocks, ...props } ) => {
return(
<Button onClick={ onClick } { ...props} >
{buttonText}
</Button>
);
};
export default compose( [
withSelect( ( select, ownProps ) => {
return {
innerBlocks: select( \'core/block-editor\' ).getBlock( ownProps.clientId ).innerBlocks
};
} ),
withDispatch( ( dispatch, ownProps ) => {
return {
onClick() {
const newBlock = createBlock( ownProps.allowedBlock );
dispatch( \'core/block-editor\' ).insertBlock( newBlock, ownProps.innerBlocks.length, ownProps.clientId );
}
};
} )
] )( SingleBlockTypeAppender );
然后,您可以这样使用它:
<InnerBlocks
renderAppender={
() =>
<SingleBlockTypeAppender
isDefault
isLarge
buttonText="Add Block"
allowedBlock="block/slug"
clientId={ this.props.clientId }
/>
}
/>
希望这有帮助!