Skip to content

Commit 4a2bb5e

Browse files
ur300paskal
andauthored
#1833 - Toolbar buttons are stuck to the main comment form (#1948)
* 1833 - Toolbar buttons are stuck to the main comment form * Add readonly and JSDoc to CommentForm textareaId properties Improve code quality based on review feedback: mark textareaId as readonly since it should never change after construction, and add JSDoc to static textareaCounter explaining its purpose. --------- Co-authored-by: Dmitry Verkhoturov <paskal.07@gmail.com>
1 parent d01b738 commit 4a2bb5e

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

frontend/apps/remark42/app/components/comment-form/comment-form.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function setup(overrideProps: Partial<Props> = {}, overrideConfig: Partial<typeo
4040
describe('<CommentForm />', () => {
4141
afterEach(() => {
4242
// reset textarea id in order to have `textarea_1` for every test
43-
CommentForm.textareaId = 0;
43+
CommentForm.textareaCounter = 0;
4444
localStorage.clear();
4545
});
4646

frontend/apps/remark42/app/components/comment-form/comment-form.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ const ImageMimeRegex = /image\//i;
5858
export class CommentForm extends Component<Props, State> {
5959
/** reference to textarea element */
6060
textareaRef = createRef<HTMLTextAreaElement>();
61-
static textareaId = 0;
61+
/** global counter for generating unique textarea IDs across all instances */
62+
static textareaCounter = 0;
63+
/** unique textarea ID for this instance */
64+
readonly textareaId: string;
6265

6366
state = {
6467
preview: null,
@@ -75,7 +78,8 @@ export class CommentForm extends Component<Props, State> {
7578

7679
const savedComment = getPersistedComment(props.id);
7780
this.state.text = props.value ?? savedComment ?? '';
78-
CommentForm.textareaId += 1;
81+
CommentForm.textareaCounter += 1;
82+
this.textareaId = `textarea_${CommentForm.textareaCounter}`;
7983
}
8084

8185
componentWillReceiveProps(nextProps: Props) {
@@ -416,7 +420,6 @@ export class CommentForm extends Component<Props, State> {
416420
edit: <FormattedMessage id="commentForm.save" defaultMessage="Save" />,
417421
reply: <FormattedMessage id="commentForm.reply" defaultMessage="Reply" />,
418422
};
419-
const textareaId = `textarea_${CommentForm.textareaId}`;
420423
const label = buttonText || Labels[mode || 'main'];
421424
const placeholderMessage = intl.formatMessage(messages.placeholder);
422425
const isSimpleView = StaticStore.config.simple_view;
@@ -443,14 +446,14 @@ export class CommentForm extends Component<Props, State> {
443446
intl={intl}
444447
allowUpload={Boolean(uploadImage)}
445448
uploadImages={this.uploadImages}
446-
textareaId={textareaId}
449+
textareaId={this.textareaId}
447450
/>
448451
</div>
449452
)}
450453
<div className="comment-form__field-wrapper">
451454
<TextExpander>
452455
<TextareaAutosize
453-
id={textareaId}
456+
id={this.textareaId}
454457
ref={this.textareaRef}
455458
onPaste={this.onPaste}
456459
className="comment-form__field"

0 commit comments

Comments
 (0)