Overview
You can create your custom text formatter for CometChat using theCometChatTextFormatter. 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.
Usage
here are the steps to create your custom text formatter for CometChat using theCometChatTextFormatter:
To integrate the CometChatTextFormatter class into your application:
- Firstly, you need to import
CometChatTextFormatterfrom the CometChat UI Kit react library.
- Now, extend the
CometChatTextFormatterclass to create your custom text formatter class. In this case, let’s create aHashTagTextFormatter.
- set up the
trackCharacter,regexPatternsandregexToReplaceFormatting. For a hashtag formatter, we’re setting the track character to#.
- Set callback functions for key up and key down events.
- Implement the
getFormattedText,getOriginalTextand 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.
- HashTagTextFormatter.ts
- MessageListDemo.tsx
Methods
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:
- getFormattedText
- onKeyUp
- onKeyDown
- registerEventListeners
- getOriginalText