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

Impossible to enforce max character length? #246

Open
DaltheCow opened this issue Sep 28, 2021 · 1 comment
Open

Impossible to enforce max character length? #246

DaltheCow opened this issue Sep 28, 2021 · 1 comment

Comments

@DaltheCow
Copy link

I somewhat understand the issues related how the react-contenteditable has to prevent rerenders in certain scenarios (or something like that from 161. Couldn't say if this is related to that per se but anyways...

I'm trying to enforce a max text length on my title field content editable, doing something like this inside handleChange
setTitle(e?.currentTarget?.innerHTML.slice(0, 300));

It doesn't stop the the text from rolling past 300 characters so I added a hacky forceUpdate() call after calling setTitle

const [, updateState] = React.useState();
  const forceUpdate = React.useCallback(() => updateState({}), []);

...inside handleChange
{
   setTitle(e?.currentTarget?.innerHTML.slice(0, 300));
   forceUpdate();
}

And this sort of works. But of course it is an awful practice and leads to bugs. It was more of just a test.

But without the forceUpdate the content editable doesn't enforce the max length while typing. It does however clip the text when i move the mouse in and out of hovering the content editable. I assume this is all related to the content editable preventing rerenders.

Just wondering if there are any work arounds at all to enforce a max text length. I don't think this is remotely possible but an alternative I'd be happy with is turning all the characters past 300 red. I think that sounds far more impossible though!! Thanks for any input, even if it is just saying max length this isn't possible.

@DaltheCow
Copy link
Author

Current solution is to just add in a twitter style counter (-(num characters over limit) shows up next to the text when you type too many

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

No branches or pull requests

1 participant