Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Request for Adding useCharacterCount Hook #7063

Open
h1un opened this issue Jan 18, 2025 · 2 comments · May be fixed by #7075
Open

Feature: Request for Adding useCharacterCount Hook #7063

h1un opened this issue Jan 18, 2025 · 2 comments · May be fixed by #7075
Labels
enhancement Improvement over existing feature

Comments

@h1un
Copy link

h1un commented Jan 18, 2025

Description

I have implemented a new useCharacterCount hook inspired by the existing useCharacterLimit functionality. This hook helps track the number of characters in real-time within text input fields or rich text editors.

Although useCharacterLimit and useCharacterCount are not typically used together, an issue arises when both hooks are used simultaneously, causing the following error:

ToolbarContext.tsx:100 Warning: Maximum update depth exceeded. This can happen when a component calls setState inside useEffect, but useEffect either doesn't have a dependency array, or one of the dependencies changes on every render.
I am currently facing difficulties resolving this issue and would appreciate help fixing it so that both hooks can work together. Also, I would like to ask whether it would be appropriate to submit a pull request for this.

You can find the implementation of the useCharacterCount hook here.

Impact

Text Input Validation: Track the number of characters a user types in real-time and display an alert when the character count exceeds a set limit.
Rich Text Editing: Track character count in a rich text editor as users type, which can be useful for applications like document editing.
Form Validation: Dynamically validate form inputs based on character count, ensuring the form is only submitted if the character count is within the allowed range.

I request that the useCharacterCount hook be improved to work seamlessly alongside useCharacterLimit, resolving the issue when both hooks are used together.

@h1un h1un added the enhancement Improvement over existing feature label Jan 18, 2025
@etrepum
Copy link
Collaborator

etrepum commented Jan 20, 2025

It sounds like your useCharacterCount hook is doing things it shouldn't do, you don't need to use editor.update at all to count the characters.

@h1un h1un linked a pull request Jan 21, 2025 that will close this issue
@h1un
Copy link
Author

h1un commented Jan 21, 2025

I’ve made the necessary updates based on your feedback. The useCharacterCount hook has been refactored to separate the character counting logic from editor state modification. I’ve removed the usage of editor.update and ensured that the hook only listens for text content changes and updates the character count accordingly.

Thank you for your suggestions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvement over existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants