Is there a way to show a word count in or around the message box?

1 Answer 55 Views
Chat
Chris
Top achievements
Rank 1
Chris asked on 14 Jul 2023, 03:17 PM

Hello,

I'm using an internal API that has rate and size limiting and I think it would be beneficial to show the user a used character out of total characters placeholder. Something similar to this as an example.

1 Answer, 1 is accepted

Sort by
0
Stoyan
Telerik team
answered on 19 Jul 2023, 02:18 PM

Hello Christopher,

Although, a character counter isn't supported out-of-the-box by the Chat here is how you can subscribe to its TypingStart Event and add a custom element that contains a character counter:

  1. Subscribe to the TypingStart Event
    .Events(e => e.TypingStart("onTypingStart"))
  2. Insert the new element and subscribe to the input event of the Chat's input element.
  3. In the input handler update the counter and optionally hide it when the user deletes their message.
        function onTypingStart(e){
            var wrapperElement = e.sender.element.parent();
            var input = wrapperElement.find("input");
            if($("#char-count").length<1){
                $("<div id='counter' style='margin-top:10px;'><span id='char-count'>0</span>/<span>4000</span></div>").insertAfter(wrapperElement[0]);
                input.on("input",countCharacters);
                input.trigger("input");
            }
        }
        function countCharacters(e){
            var inputString = $(e.currentTarget).val();
            if(inputString.length>0){
                if($("#counter").css("display")!="none"){
                    $("#char-count").text(inputString.length);
                }else{
                    $("#counter").show();
                }
            }else{
                $("#counter").hide();
            }
        }

For your convenience I have applied the above to this Telerik REPL example: https://netcorerepl.telerik.com/cHarPNvo16ZYReB335

Regards,
Stoyan
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages. If you're new to the Telerik family, be sure to check out our getting started resources, as well as the only REPL playground for creating, saving, running, and sharing server-side code.
Tags
Chat
Asked by
Chris
Top achievements
Rank 1
Answers by
Stoyan
Telerik team
Share this question
or