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 Latest Comments block to use function component #23557

Merged
merged 2 commits into from
Oct 14, 2020
Merged
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
131 changes: 53 additions & 78 deletions packages/block-library/src/latest-comments/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
ToggleControl,
} from '@wordpress/components';
import ServerSideRender from '@wordpress/server-side-render';
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
Expand All @@ -25,83 +24,59 @@ const MIN_COMMENTS = 1;
*/
const MAX_COMMENTS = 100;

class LatestComments extends Component {
constructor() {
super( ...arguments );
export default function LatestComments( { attributes, setAttributes } ) {
const {
commentsToShow,
displayAvatar,
displayDate,
displayExcerpt,
} = attributes;
Copy link
Contributor

Choose a reason for hiding this comment

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

You can destructure attributes in the above destructuring.

{ attributes, attributes: { commentsToshow, ....... } setAttributes }

Copy link
Member

Choose a reason for hiding this comment

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

Actually, it would be { attributes: { commentsToShow, (etc) }, setAttributes }. No need to duplicate attributes; actually, I think that would cause a linting error.


this.setCommentsToShow = this.setCommentsToShow.bind( this );

// Create toggles for each attribute; we create them here rather than
// passing `this.createToggleAttribute( 'displayAvatar' )` directly to
// `onChange` to avoid re-renders.
this.toggleDisplayAvatar = this.createToggleAttribute(
'displayAvatar'
);
this.toggleDisplayDate = this.createToggleAttribute( 'displayDate' );
this.toggleDisplayExcerpt = this.createToggleAttribute(
'displayExcerpt'
);
}

createToggleAttribute( propName ) {
return () => {
const value = this.props.attributes[ propName ];
const { setAttributes } = this.props;

setAttributes( { [ propName ]: ! value } );
};
}

setCommentsToShow( commentsToShow ) {
this.props.setAttributes( { commentsToShow } );
}

render() {
const {
commentsToShow,
displayAvatar,
displayDate,
displayExcerpt,
} = this.props.attributes;

return (
<>
<InspectorControls>
<PanelBody title={ __( 'Latest comments settings' ) }>
<ToggleControl
label={ __( 'Display avatar' ) }
checked={ displayAvatar }
onChange={ this.toggleDisplayAvatar }
/>
<ToggleControl
label={ __( 'Display date' ) }
checked={ displayDate }
onChange={ this.toggleDisplayDate }
/>
<ToggleControl
label={ __( 'Display excerpt' ) }
checked={ displayExcerpt }
onChange={ this.toggleDisplayExcerpt }
/>
<RangeControl
label={ __( 'Number of comments' ) }
value={ commentsToShow }
onChange={ this.setCommentsToShow }
min={ MIN_COMMENTS }
max={ MAX_COMMENTS }
required
/>
</PanelBody>
</InspectorControls>
<Disabled>
<ServerSideRender
block="core/latest-comments"
attributes={ this.props.attributes }
return (
<>
<InspectorControls>
<PanelBody title={ __( 'Latest comments settings' ) }>
<ToggleControl
label={ __( 'Display avatar' ) }
checked={ displayAvatar }
onChange={ () =>
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe you could keep the generic toggle function, since all three toggle on change like:

const toggleAttribute = ( propName ) => () =>
	setAttributes( { [ propName ]: ! attributes[ propName ] } );

Copy link
Member

Choose a reason for hiding this comment

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

I'm not convinced it would make much of a difference, performance-wise.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's more about reusability and not performance.

setAttributes( { displayAvatar: ! displayAvatar } )
}
/>
<ToggleControl
label={ __( 'Display date' ) }
checked={ displayDate }
onChange={ () =>
setAttributes( { displayDate: ! displayDate } )
}
/>
<ToggleControl
label={ __( 'Display excerpt' ) }
checked={ displayExcerpt }
onChange={ () =>
setAttributes( {
displayExcerpt: ! displayExcerpt,
} )
}
/>
</Disabled>
</>
);
}
<RangeControl
label={ __( 'Number of comments' ) }
value={ commentsToShow }
onChange={ ( value ) =>
setAttributes( { commentsToShow: value } )
}
min={ MIN_COMMENTS }
max={ MAX_COMMENTS }
required
/>
</PanelBody>
</InspectorControls>
<Disabled>
<ServerSideRender
block="core/latest-comments"
attributes={ attributes }
/>
</Disabled>
</>
);
}

export default LatestComments;