From 077e9df2a6bb88db0ea7cab3be6fc9ab868055b9 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 9 Jan 2022 00:57:17 +0100 Subject: [PATCH] Port comment character count functionality to TypeScript --- .../features/answerbox/comment/count.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 app/javascript/retrospring/features/answerbox/comment/count.ts diff --git a/app/javascript/retrospring/features/answerbox/comment/count.ts b/app/javascript/retrospring/features/answerbox/comment/count.ts new file mode 100644 index 00000000..2650bb3d --- /dev/null +++ b/app/javascript/retrospring/features/answerbox/comment/count.ts @@ -0,0 +1,19 @@ +export function commentCharacterCountHandler(event: Event): void { + const input = event.target as HTMLInputElement; + const id = input.dataset.aId; + const counter = document.querySelector(`#ab-comment-charcount-${id}`); + const group = document.querySelector(`[name=ab-comment-new-group][data-a-id="${id}"]`); + + if (group.classList.contains('has-error')) { + group.classList.remove('has-error'); + } + + counter.innerHTML = String(160 - input.value.length); + if (Number(counter.innerHTML) < 0) { + counter.classList.remove('text-muted'); + counter.classList.add('text-danger'); + } else { + counter.classList.remove('text-danger'); + counter.classList.add('text-muted'); + } +} \ No newline at end of file