Skip to content

Commit

Permalink
Refactor: Reuse code of the editor placeholder across Post Comments a…
Browse files Browse the repository at this point in the history
…nd Post Comments Form (#40560)

* Create a separate template for the Comments Form

* Use the form in the Post Comments block

* Add the missing "return" in the CommentForm
  • Loading branch information
adamziel committed Jun 21, 2022
1 parent 2c2970e commit 7e7d6cd
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 74 deletions.
43 changes: 6 additions & 37 deletions packages/block-library/src/post-comments-form/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import {
} from '@wordpress/block-editor';
import { useEntityProp } from '@wordpress/core-data';
import { __, sprintf } from '@wordpress/i18n';
import {
__experimentalUseDisabled as useDisabled,
useInstanceId,
} from '@wordpress/compose';

/**
* Internal dependencies
*/
import CommentsForm from './form';

export default function PostCommentsFormEdit( {
attributes,
Expand All @@ -40,10 +41,6 @@ export default function PostCommentsFormEdit( {

const isInSiteEditor = postType === undefined || postId === undefined;

const disabledFormRef = useDisabled();

const instanceId = useInstanceId( PostCommentsFormEdit );

return (
<>
<BlockControls group="block">
Expand Down Expand Up @@ -76,35 +73,7 @@ export default function PostCommentsFormEdit( {
) }

{ ( 'open' === commentStatus || isInSiteEditor ) && (
<div>
<h3>{ __( 'Leave a Reply' ) }</h3>
<form
noValidate
className="comment-form"
ref={ disabledFormRef }
>
<p>
<label htmlFor={ `comment-${ instanceId }` }>
{ __( 'Comment' ) }
</label>
<textarea
id={ `comment-${ instanceId }` }
name="comment"
cols="45"
rows="8"
/>
</p>
<p>
<input
name="submit"
className="submit wp-block-button__link"
label={ __( 'Post Comment' ) }
value={ __( 'Post Comment' ) }
readOnly
/>
</p>
</form>
</div>
<CommentsForm />
) }
</div>
</>
Expand Down
43 changes: 43 additions & 0 deletions packages/block-library/src/post-comments-form/form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
__experimentalUseDisabled as useDisabled,
useInstanceId,
} from '@wordpress/compose';

const CommentsForm = () => {
const disabledFormRef = useDisabled();
const instanceId = useInstanceId( CommentsForm );

return (
<div className="comment-respond">
<h3 className="comment-reply-title">{ __( 'Leave a Reply' ) }</h3>
<form noValidate className="comment-form" ref={ disabledFormRef }>
<p>
<label htmlFor={ `comment-${ instanceId }` }>
{ __( 'Comment' ) }
</label>
<textarea
id={ `comment-${ instanceId }` }
name="comment"
cols="45"
rows="8"
/>
</p>
<p className="form-submit wp-block-button">
<input
name="submit"
type="submit"
className="submit wp-block-button__link"
label={ __( 'Post Comment' ) }
value={ __( 'Post Comment' ) }
/>
</p>
</form>
</div>
);
};

export default CommentsForm;
45 changes: 8 additions & 37 deletions packages/block-library/src/post-comments/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ import {
import { __, sprintf } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { useEntityProp, store as coreStore } from '@wordpress/core-data';
import {
__experimentalUseDisabled as useDisabled,
useInstanceId,
} from '@wordpress/compose';

import { createInterpolateElement } from '@wordpress/element';
import { __experimentalUseDisabled as useDisabled } from '@wordpress/compose';

/**
* Internal dependencies
*/
import CommentsForm from '../post-comments-form/form';

export default function PostCommentsEdit( {
attributes: { textAlign },
Expand Down Expand Up @@ -89,8 +92,6 @@ export default function PostCommentsEdit( {

const disabledRef = useDisabled();

const textareaId = useInstanceId( PostCommentsEdit );

return (
<>
<BlockControls group="block">
Expand Down Expand Up @@ -238,37 +239,7 @@ export default function PostCommentsEdit( {
</div>
</div>

<div className="comment-respond">
<h3 className="comment-reply-title">
{ __( 'Leave a Reply' ) }
</h3>

<form className="comment-form" noValidate>
<p className="comment-form-comment">
<label
htmlFor={ `comment-${ textareaId }` }
>
{ __( 'Comment' ) }{ ' ' }
<span className="required">*</span>
</label>
<textarea
id={ `comment-${ textareaId }` }
name="comment"
cols="45"
rows="8"
required
/>
</p>
<p className="form-submit wp-block-button">
<input
name="submit"
type="submit"
className="submit wp-block-button__link"
value={ __( 'Post Comment' ) }
/>
</p>
</form>
</div>
<CommentsForm />
</div>
) }
</div>
Expand Down

0 comments on commit 7e7d6cd

Please sign in to comment.