UNPKG

2.57 kBJavaScriptView Raw
1/**
2 * WordPress dependencies
3 */
4import { InspectorControls } from '@wordpress/block-editor';
5import {
6 Disabled,
7 PanelBody,
8 RangeControl,
9 ToggleControl,
10} from '@wordpress/components';
11import { ServerSideRender } from '@wordpress/editor';
12import { Component, Fragment } from '@wordpress/element';
13import { __ } from '@wordpress/i18n';
14
15/**
16 * Minimum number of comments a user can show using this block.
17 *
18 * @type {number}
19 */
20const MIN_COMMENTS = 1;
21/**
22 * Maximum number of comments a user can show using this block.
23 *
24 * @type {number}
25 */
26const MAX_COMMENTS = 100;
27
28class LatestComments extends Component {
29 constructor() {
30 super( ...arguments );
31
32 this.setCommentsToShow = this.setCommentsToShow.bind( this );
33
34 // Create toggles for each attribute; we create them here rather than
35 // passing `this.createToggleAttribute( 'displayAvatar' )` directly to
36 // `onChange` to avoid re-renders.
37 this.toggleDisplayAvatar = this.createToggleAttribute( 'displayAvatar' );
38 this.toggleDisplayDate = this.createToggleAttribute( 'displayDate' );
39 this.toggleDisplayExcerpt = this.createToggleAttribute( 'displayExcerpt' );
40 }
41
42 createToggleAttribute( propName ) {
43 return () => {
44 const value = this.props.attributes[ propName ];
45 const { setAttributes } = this.props;
46
47 setAttributes( { [ propName ]: ! value } );
48 };
49 }
50
51 setCommentsToShow( commentsToShow ) {
52 this.props.setAttributes( { commentsToShow } );
53 }
54
55 render() {
56 const {
57 commentsToShow,
58 displayAvatar,
59 displayDate,
60 displayExcerpt,
61 } = this.props.attributes;
62
63 return (
64 <Fragment>
65 <InspectorControls>
66 <PanelBody title={ __( 'Latest Comments Settings' ) }>
67 <ToggleControl
68 label={ __( 'Display Avatar' ) }
69 checked={ displayAvatar }
70 onChange={ this.toggleDisplayAvatar }
71 />
72 <ToggleControl
73 label={ __( 'Display Date' ) }
74 checked={ displayDate }
75 onChange={ this.toggleDisplayDate }
76 />
77 <ToggleControl
78 label={ __( 'Display Excerpt' ) }
79 checked={ displayExcerpt }
80 onChange={ this.toggleDisplayExcerpt }
81 />
82 <RangeControl
83 label={ __( 'Number of Comments' ) }
84 value={ commentsToShow }
85 onChange={ this.setCommentsToShow }
86 min={ MIN_COMMENTS }
87 max={ MAX_COMMENTS }
88 required
89 />
90 </PanelBody>
91 </InspectorControls>
92 <Disabled>
93 <ServerSideRender
94 block="core/latest-comments"
95 attributes={ this.props.attributes }
96 />
97 </Disabled>
98 </Fragment>
99 );
100 }
101}
102
103export default LatestComments;