-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
||
/** | ||
|
@@ -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; | ||
|
||
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={ () => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ] } ); There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 duplicateattributes
; actually, I think that would cause a linting error.