Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Add word count and indicator when exceeded max (#19367)

Keegan George9c29d688e7c8
diff --git a/app/assets/javascripts/discourse/app/components/char-counter.hbs b/app/assets/javascripts/discourse/app/components/char-counter.hbs
new file mode 100644
index 00000000000..3ab7e455dd7
--- /dev/null
+++ b/app/assets/javascripts/discourse/app/components/char-counter.hbs
@@ -0,0 +1,12 @@
+<div
+ class={{concat-class "char-counter" (if (gt @value.length @max) "exceeded")}}
+ ...attributes
+>
+ {{yield}}
+ <small class="char-counter__ratio">
+ {{@value.length}}/{{@max}}
+ </small>
+ <span aria-live="polite" class="sr-only">
+ {{if (gt @value.length @max) (i18n "char_counter.exceeded")}}
+ </span>
+</div>
\ No newline at end of file
diff --git a/app/assets/javascripts/discourse/tests/integration/components/char-counter-test.js b/app/assets/javascripts/discourse/tests/integration/components/char-counter-test.js
new file mode 100644
index 00000000000..86520006133
--- /dev/null
+++ b/app/assets/javascripts/discourse/tests/integration/components/char-counter-test.js
@@ -0,0 +1,48 @@
+import { module, test } from "qunit";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import { fillIn, render } from "@ember/test-helpers";
+import { hbs } from "ember-cli-htmlbars";
+
+module("Integration | Component | char-counter", function (hooks) {
+ setupRenderingTest(hooks);
+
+ test("shows the number of characters", async function (assert) {
+ this.value = "Hello World";
+ this.max = 12;
+
+ await render(
+ hbs`<CharCounter @value={{this.value}} @max={{this.max}}></CharCounter>`
+ );
+
+ assert.dom(this.element).includesText("11/12");
+ });
+
+ test("updating value updates counter", async function (assert) {
+ this.max = 50;
+
+ await render(
+ hbs`<CharCounter @value={{this.charCounterContent}} @max={{this.max}}><textarea {{on "input" (action (mut this.charCounterContent) value="target.value")}}></textarea></CharCounter>`
+ );
+
+ assert
+ .dom(this.element)
+ .includesText("/50", "initial value appears as expected");
+
+ await fillIn("textarea", "Hello World, this is a longer string");
+
+ assert
+ .dom(this.element)
+ .includesText("36/50", "updated value appears as expected");
+ });
+
+ test("exceeding max length", async function (assert) {
+ this.max = 10;
+ this.value = "Hello World";
+
+ await render(
+ hbs`<CharCounter @value={{this.value}} @max={{this.max}}></CharCounter>`
+ );
+
+ assert.dom(".char-counter.exceeded").exists("exceeded class is applied");
+ });
+});
diff --git a/app/assets/stylesheets/common/components/_index.scss b/app/assets/stylesheets/common/components/_index.scss
index b33546edc10..f9201fa36ce 100644
--- a/app/assets/stylesheets/common/components/_index.scss
+++ b/app/assets/stylesheets/common/components/_index.scss
@@ -4,6 +4,7 @@
@import "bookmark-modal";
@import "buttons";
@import "color-input";
+@import "char-counter";
@import "conditional-loading-section";
@import "convert-to-public-topic-modal";
@import "d-tooltip";
diff --git a/app/assets/stylesheets/common/components/char-counter.scss b/app/assets/stylesheets/common/components/char-counter.scss
new file mode 100644
index 00000000000..bb95b30d146
--- /dev/null
+++ b/app/assets/stylesheets/common/components/char-counter.scss
@@ -0,0 +1,18 @@
+.char-counter {
+ &__ratio {
+ display: block;
+ text-align: right;
+ margin-top: 0.5rem;
+ }
+
+ &.exceeded {
+ > textarea {
+ border-color: var(--danger);
+ outline-color: var(--danger);
+ }
+
+ &__ratio {
+ color: var(--danger);
+ }
+ }
+}
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index d855d9c87aa..b2c7ed75611 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -4453,6 +4453,9 @@ en:
until: "Until:"
+ char_counter:
+ exceeded: "The maximum number of characters allowed has been exceeded."
+
# This section is exported to the javascript for i18n in the admin section
admin_js:
type_to_filter: "type to filter..."
diff --git a/plugins/chat/assets/javascripts/discourse/controllers/chat-channel-edit-description.js b/plugins/chat/assets/javascripts/discourse/controllers/chat-channel-edit-description.js
index 85e834963ae..664a1993bad 100644
--- a/plugins/chat/assets/javascripts/discourse/controllers/chat-channel-edit-description.js
+++ b/plugins/chat/assets/javascripts/discourse/controllers/chat-channel-edit-description.js
@@ -1,28 +1,30 @@
import Controller from "@ember/controller";
import { action, computed } from "@ember/object";
import ModalFunctionality from "discourse/mixins/modal-functionality";
+import { tracked } from "@glimmer/tracking";
import { inject as service } from "@ember/service";
+const DESCRIPTION_MAX_LENGTH = 280;
+
export default class ChatChannelEditDescriptionController extends Controller.extend(
ModalFunctionality
) {
@service chatApi;
- editedDescription = "";
+ @tracked editedDescription = this.model.description || "";
@computed("model.description", "editedDescription")
get isSaveDisabled() {
return (
this.model.description === this.editedDescription ||
- this.editedDescription?.length > 280
+ this.editedDescription?.length > DESCRIPTION_MAX_LENGTH
);
}
- onShow() {
- this.set("editedDescription", this.model.description || "");
+ get descriptionMaxLength() {
+ return DESCRIPTION_MAX_LENGTH;
}
onClose() {
- this.set("editedDescription", "");
this.clearFlash();
}
@@ -46,6 +48,6 @@ export default class ChatChannelEditDescriptionController extends Controller.ext
@action
onChangeChatChannelDescription(description) {
this.clearFlash();
- this.set("editedDescription", description);
+ this.editedDescription = description;
}
}
diff --git a/plugins/chat/assets/javascripts/discourse/templates/modal/chat-channel-edit-description.hbs b/plugins/chat/assets/javascripts/discourse/templates/modal/chat-channel-edit-description.hbs
index b988acd671e..059e2d147bf 100644
--- a/plugins/chat/assets/javascripts/discourse/templates/modal/chat-channel-edit-description.hbs
+++ b/plugins/chat/assets/javascripts/discourse/templates/modal/chat-channel-edit-description.hbs
@@ -1,15 +1,24 @@
<DModalBody @title="chat.channel_edit_description_modal.title">
- <textarea
- class="chat-channel-edit-description-modal__description-input"
- placeholder={{i18n "chat.channel_edit_description_modal.input_placeholder"}}
- {{on
- "input"
- (action "onChangeChatChannelDescription" value="target.value")
- }}
- >{{this.model.description}}</textarea>
<span class="chat-channel-edit-description-modal__description">
{{i18n "chat.channel_edit_description_modal.description"}}
</span>
+ <CharCounter
+ @value={{this.editedDescription}}
+ @max={{this.descriptionMaxLength}}
+ >
+ <textarea
+ {{on
+ "input"
+ (action this.onChangeChatChannelDescription value="target.value")
+ }}
+ class="chat-channel-edit-description-modal__description-input"
+ placeholder={{i18n
+ "chat.channel_edit_description_modal.input_placeholder"
+ }}
+ >
+ {{this.editedDescription}}
+ </textarea>
+ </CharCounter>
</DModalBody>
<div class="modal-footer">
diff --git a/plugins/chat/assets/stylesheets/common/chat-channel-info.scss b/plugins/chat/assets/stylesheets/common/chat-channel-info.scss
index bc6d069e71f..ca7ce1acd2a 100644
--- a/plugins/chat/assets/stylesheets/common/chat-channel-info.scss
+++ b/plugins/chat/assets/stylesheets/common/chat-channel-info.scss
@@ -142,6 +142,15 @@ input.channel-members-view__search-input {
}
// Channel info edit description modal
+.chat-channel-edit-description-modal {
+ .exceeded-word-count {
+ .chat-channel-edit-description-modal__description-input {
+ outline: 1px solid var(--danger);
+ border: 1px solid var(--danger);
+ }
+ }
+}
+
.chat-channel-edit-description-modal__description-input {
display: flex;
margin: 0;
@@ -150,6 +159,6 @@ input.channel-members-view__search-input {
.chat-channel-edit-description-modal__description {
display: flex;
- padding: 0.75rem 0 0.5rem;
+ padding-bottom: 0.75rem;
color: var(--primary-medium);
}
diff --git a/plugins/styleguide/assets/javascripts/discourse/templates/styleguide/molecules/char-counter.hbs b/plugins/styleguide/assets/javascripts/discourse/templates/styleguide/molecules/char-counter.hbs
new file mode 100644
index 00000000000..635e1955ef1
--- /dev/null
+++ b/plugins/styleguide/assets/javascripts/discourse/templates/styleguide/molecules/char-counter.hbs
@@ -0,0 +1,10 @@
+<StyleguideExample @title="char-counter">
+ <CharCounter @max="50" @value={{this.charCounterContent}}>
+ <textarea
+ placeholder={{i18n "styleguide.sections.char_counter.placeholder"}}
+ {{on "input" (action (mut this.charCounterContent) value="target.value")}}
+ class="styleguide--char-counter"
+ >
+ </textarea>
+ </CharCounter>
+</StyleguideExample>
\ No newline at end of file
diff --git a/plugins/styleguide/assets/stylesheets/styleguide.scss b/plugins/styleguide/assets/stylesheets/styleguide.scss
index c9aa99c6d02..432c58c766e 100644
--- a/plugins/styleguide/assets/stylesheets/styleguide.scss
+++ b/plugins/styleguide/assets/stylesheets/styleguide.scss
@@ -144,6 +144,12 @@
color: var(--primary-medium);
margin: 0 0 0 10px;
}
+
+ &--char-counter {
+ display: block;
+ width: 100%;
+ margin-bottom: 0;
+ }
}
.buttons-examples {
diff --git a/plugins/styleguide/config/locales/client.en.yml b/plugins/styleguide/config/locales/client.en.yml
index f6f3506b691..6a2780abb09 100644
--- a/plugins/styleguide/config/locales/client.en.yml
+++ b/plugins/styleguide/config/locales/client.en.yml
@@ -88,3 +88,7 @@ en:
description: "Description"
header: "Header"
hover_to_see: "Hover to see a tooltip"
+ char_counter:
+ title: "Character Counter"
+ placeholder: "Enter your text here..."
+