Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor header toolbar items to use @wordpress/data hooks #23315

Merged
merged 1 commit into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions packages/editor/src/components/editor-history/redo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
*/
import { __ } from '@wordpress/i18n';
import { Button } from '@wordpress/components';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { useSelect, useDispatch } from '@wordpress/data';
import { displayShortcut } from '@wordpress/keycodes';
import { redo as redoIcon } from '@wordpress/icons';
import { forwardRef } from '@wordpress/element';

function EditorHistoryRedo( { hasRedo, redo, innerRef, ...props } ) {
function EditorHistoryRedo( props, ref ) {
const hasRedo = useSelect( ( select ) =>
select( 'core/editor' ).hasEditorRedo()
);
Comment on lines +13 to +14
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting that you forgot to add a dependency array to this and the other useSelect calls to memoize the callbacks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, thanks for noticing! I'll create a follow-up PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We really need a lint rule, it’s impossible to catch all cases in review.

const { redo } = useDispatch( 'core/editor' );
return (
<Button
{ ...props }
ref={ innerRef }
ref={ ref }
icon={ redoIcon }
label={ __( 'Redo' ) }
shortcut={ displayShortcut.primaryShift( 'z' ) }
Expand All @@ -27,15 +30,4 @@ function EditorHistoryRedo( { hasRedo, redo, innerRef, ...props } ) {
);
}

const EnhancedEditorHistoryRedo = compose( [
withSelect( ( select ) => ( {
hasRedo: select( 'core/editor' ).hasEditorRedo(),
} ) ),
withDispatch( ( dispatch ) => ( {
redo: dispatch( 'core/editor' ).redo,
} ) ),
] )( EditorHistoryRedo );

export default forwardRef( ( props, ref ) => (
<EnhancedEditorHistoryRedo { ...props } innerRef={ ref } />
) );
export default forwardRef( EditorHistoryRedo );
24 changes: 8 additions & 16 deletions packages/editor/src/components/editor-history/undo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
*/
import { __ } from '@wordpress/i18n';
import { Button } from '@wordpress/components';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { useSelect, useDispatch } from '@wordpress/data';
import { displayShortcut } from '@wordpress/keycodes';
import { undo as undoIcon } from '@wordpress/icons';
import { forwardRef } from '@wordpress/element';

function EditorHistoryUndo( { hasUndo, undo, innerRef, ...props } ) {
function EditorHistoryUndo( props, ref ) {
const hasUndo = useSelect( ( select ) =>
select( 'core/editor' ).hasEditorUndo()
);
const { undo } = useDispatch( 'core/editor' );
return (
<Button
{ ...props }
ref={ innerRef }
ref={ ref }
icon={ undoIcon }
label={ __( 'Undo' ) }
shortcut={ displayShortcut.primary( 'z' ) }
Expand All @@ -27,15 +30,4 @@ function EditorHistoryUndo( { hasUndo, undo, innerRef, ...props } ) {
);
}

const EnhancedEditorHistoryUndo = compose( [
withSelect( ( select ) => ( {
hasUndo: select( 'core/editor' ).hasEditorUndo(),
} ) ),
withDispatch( ( dispatch ) => ( {
undo: dispatch( 'core/editor' ).undo,
} ) ),
] )( EditorHistoryUndo );

export default forwardRef( ( props, ref ) => (
<EnhancedEditorHistoryUndo { ...props } innerRef={ ref } />
) );
export default forwardRef( EditorHistoryUndo );
24 changes: 7 additions & 17 deletions packages/editor/src/components/table-of-contents/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { __ } from '@wordpress/i18n';
import { Dropdown, Button } from '@wordpress/components';
import { withSelect } from '@wordpress/data';
import { useSelect } from '@wordpress/data';
import { info } from '@wordpress/icons';
import { forwardRef } from '@wordpress/element';

Expand All @@ -12,12 +12,10 @@ import { forwardRef } from '@wordpress/element';
*/
import TableOfContentsPanel from './panel';

function TableOfContents( {
hasBlocks,
hasOutlineItemsDisabled,
innerRef,
...props
} ) {
function TableOfContents( { hasOutlineItemsDisabled, ...props }, ref ) {
const hasBlocks = useSelect(
( select ) => !! select( 'core/block-editor' ).getBlockCount()
);
return (
<Dropdown
position="bottom"
Expand All @@ -26,7 +24,7 @@ function TableOfContents( {
renderToggle={ ( { isOpen, onToggle } ) => (
<Button
{ ...props }
ref={ innerRef }
ref={ ref }
onClick={ hasBlocks ? onToggle : undefined }
icon={ info }
aria-expanded={ isOpen }
Expand All @@ -45,12 +43,4 @@ function TableOfContents( {
);
}

const TableOfContentsWithSelect = withSelect( ( select ) => {
return {
hasBlocks: !! select( 'core/block-editor' ).getBlockCount(),
};
} )( TableOfContents );

export default forwardRef( ( props, ref ) => (
<TableOfContentsWithSelect { ...props } innerRef={ ref } />
) );
export default forwardRef( TableOfContents );
29 changes: 13 additions & 16 deletions packages/editor/src/components/table-of-contents/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { withSelect } from '@wordpress/data';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import WordCount from '../word-count';
import DocumentOutline from '../document-outline';

function TableOfContentsPanel( {
headingCount,
paragraphCount,
numberOfBlocks,
hasOutlineItemsDisabled,
onRequestClose,
} ) {
function TableOfContentsPanel( { hasOutlineItemsDisabled, onRequestClose } ) {
const { headingCount, paragraphCount, numberOfBlocks } = useSelect(
( select ) => {
const { getGlobalBlockCount } = select( 'core/block-editor' );
return {
headingCount: getGlobalBlockCount( 'core/heading' ),
paragraphCount: getGlobalBlockCount( 'core/paragraph' ),
numberOfBlocks: getGlobalBlockCount(),
};
}
);
return (
/*
* Disable reason: The `list` ARIA role is redundant but
Expand Down Expand Up @@ -72,11 +76,4 @@ function TableOfContentsPanel( {
);
}

export default withSelect( ( select ) => {
const { getGlobalBlockCount } = select( 'core/block-editor' );
return {
headingCount: getGlobalBlockCount( 'core/heading' ),
paragraphCount: getGlobalBlockCount( 'core/paragraph' ),
numberOfBlocks: getGlobalBlockCount(),
};
} )( TableOfContentsPanel );
export default TableOfContentsPanel;