question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature: maxLength

See original GitHub issue

Setting a maxlength for an editor was something that was quite complicated using Draft.js. It’s also a common need - particularly for developers relying on an editor primarily for plaintext functionality (as a replacement for <textarea />).

I see that there’s some WIP work being done on LexicalCharacterLimitPlugin but testing this feature out in the playground seems to show a different behavior than I expected - rather than limiting input it simply highlights input that’s outside the character limit - image

I also tried to poke around the source code for how I might implement this myself. My current, very naive implementation:

const LexicalMaxLengthPlugin = ({ maxLength }) => {
    const [editor] = useLexicalComposerContext();

    useEffect(() => {
        editor.registerTextContentListener(textContent => {
            if (textContent.length > maxLength) {
                editor.update(
                    () => {
                        $getSelection().deleteCharacter(true);
                    },
                    {
                        tag: 'history-merge',
                    },
                );
            }
        });
    }, [editor]);

    return null;
};

this has many problems (I wasn’t really expecting it to work)

  • very slow to delete excessive characters
  • using a bad update pattern
  • even with the history-merge tag, the history stack is broken. A keypress to enter a character beyond the maxlength is still added to the stack.

I’m curious if functionality to mimic the behavior of <textarea maxlength="" can be accomplished using Lexical as it exists today, or if there will need to be some new APIs implemented. In particular without some pretty tedious and potentially slow node manipulation it’s not clear to me how one could truncate excess characters in a single update.

My team is trying to be an early adopter of Lexical - we’re in the process of migrating our application to React 18 and draft.js is partially blocking that. Thanks!

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
trueadmcommented, May 24, 2022
2reactions
trueadmcommented, May 23, 2022

I’m curious if functionality to mimic the behavior of <textarea maxlength=“” can be accomplished using Lexical as it exists today, or if there will need to be some new APIs implemented. In particular without some pretty tedious and potentially slow node manipulation it’s not clear to me how one could truncate excess characters in a single update.

It should definitely be possible to do this. I might take a stab at hacking something together here!

Read more comments on GitHub >

github_iconTop Results From Across the Web

maxlength attribute for input and textarea elements - CanIUse
maxlength attribute for input and textarea elements. - LS. Declares an upper bound on the number of characters the user can input.
Read more >
HTML5 Forms: MaxLength Type Attribute - Wufoo
The maxlength attribute on textarea is new in HTML5. Works for GUI-less inputs like text, email, url, search, tel and password.
Read more >
How to set character limit and maxlength - TinyMCE
You can limit character number and set maxlength in a form with a help of ... Set up a function called “event” inside...
Read more >
maxlength_attribute (html) | Accessibility Support
About this feature. The min and max attributes indicate the allowed range of values for the element. Age of results. Results across all...
Read more >
HTML DOM Input Text maxLength Property - W3Schools
The maxLength property sets or returns the value of the maxlength attribute of a text field. The maxLength attribute specifies the maximum number...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found