Skip to main content

Overview

You can create your custom text formatter for CometChat using the CometChatTextFormatter. CometChatTextFormatter is an abstract utility class that serves as a foundational structure for enabling text formatting in the message composer and text message bubbles. It can be extended to create custom formatter classes, tailored to suit specific application needs, making it a valuable tool for text customization and enhancement in chat interfaces.

Features

  • Text Formatting:: Enables automatic formatting of text within messages based on specified styles and settings, enhancing the visual presentation of chat content.
  • Customizable Styles:: Tailor text styles, including colors, fonts, and background colors, to match the desired appearance for formatted text.
  • Dynamic Text Replacement:: Utilizes regular expression patterns to identify and replace specific text patterns with formatted content, offering flexibility in text manipulation.
  • Input Field Integration:: Seamlessly integrates with the text input field, allowing for real-time monitoring and processing of user input for formatting purposes.
  • Callback Functions:: Implement callback functions for key up and key down events, providing hooks for custom actions or formatting logic based on user interactions.
Always wrap formatted data in a span with a unique CSS class (for example, “custom-hashtag”). This lets the UI Kit render it as-is instead of sanitizing it along with other tags.

Usage

here are the steps to create your custom text formatter for CometChat using the CometChatTextFormatter: To integrate the CometChatTextFormatter class into your application:
  1. Firstly, you need to import CometChatTextFormatter from the CometChat UI Kit react library.
  1. Now, extend the CometChatTextFormatter class to create your custom text formatter class. In this case, let’s create a HashTagTextFormatter.
  1. set up the trackCharacter, regexPatterns and regexToReplaceFormatting. For a hashtag formatter, we’re setting the track character to #.
  1. Set callback functions for key up and key down events.
  1. Implement the getFormattedText, getOriginalText and custom logic to format text methods.

Example

Below is an example demonstrating how to use a custom formatter class in components such as CometChatConversations, CometChatMessageList, CometChatMessageComposer.

Methods

The textContent or innerHtml of the reference element should not be directly modified in a formatter. The formatters should instead call the reRender callback to force the composer to call the getFormattedText function of the formatter instead.
Upon calling reRender, the composer will invoke the getFormattedText function for all text formatters, following the same order in which they were received as props.

Methods to be overriden for your custom formatter implementations: