Skip to content

Commit

Permalink
Refactor column block to use hooks (#23143)
Browse files Browse the repository at this point in the history
* Refactor column block to use hooks

* Update inline comment according to guidelines

Co-authored-by: Zebulan Stanphill <zebulanstanphill@protonmail.com>

Co-authored-by: Zebulan Stanphill <zebulanstanphill@protonmail.com>
  • Loading branch information
oxyc and ZebulanStanphill authored Jun 16, 2020
1 parent cb10e2d commit c663493
Showing 1 changed file with 29 additions and 39 deletions.
68 changes: 29 additions & 39 deletions packages/block-library/src/column/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,43 @@ import {
__experimentalBlock as Block,
} from '@wordpress/block-editor';
import { PanelBody, RangeControl } from '@wordpress/components';
import { withDispatch, withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { useSelect, useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';

function ColumnEdit( {
attributes,
attributes: { verticalAlignment, width },
setAttributes,
updateAlignment,
hasChildBlocks,
clientId,
} ) {
const { verticalAlignment, width } = attributes;

const classes = classnames( 'block-core-columns', {
[ `is-vertically-aligned-${ verticalAlignment }` ]: verticalAlignment,
} );

const { hasChildBlocks, rootClientId } = useSelect(
( select ) => {
const { getBlockOrder, getBlockRootClientId } = select(
'core/block-editor'
);

return {
hasChildBlocks: getBlockOrder( clientId ).length > 0,
rootClientId: getBlockRootClientId( clientId ),
};
},
[ clientId ]
);

const { updateBlockAttributes } = useDispatch( 'core/block-editor' );

const updateAlignment = ( value ) => {
// Update own alignment.
setAttributes( { verticalAlignment: value } );
// Reset parent Columns block.
updateBlockAttributes( rootClientId, {
verticalAlignment: null,
} );
};

const hasWidth = Number.isFinite( width );

return (
Expand Down Expand Up @@ -76,35 +97,4 @@ function ColumnEdit( {
);
}

export default compose(
withSelect( ( select, ownProps ) => {
const { clientId } = ownProps;
const { getBlockOrder } = select( 'core/block-editor' );

return {
hasChildBlocks: getBlockOrder( clientId ).length > 0,
};
} ),
withDispatch( ( dispatch, ownProps, registry ) => {
return {
updateAlignment( verticalAlignment ) {
const { clientId, setAttributes } = ownProps;
const { updateBlockAttributes } = dispatch(
'core/block-editor'
);
const { getBlockRootClientId } = registry.select(
'core/block-editor'
);

// Update own alignment.
setAttributes( { verticalAlignment } );

// Reset Parent Columns Block
const rootClientId = getBlockRootClientId( clientId );
updateBlockAttributes( rootClientId, {
verticalAlignment: null,
} );
},
};
} )
)( ColumnEdit );
export default ColumnEdit;

0 comments on commit c663493

Please sign in to comment.