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

Remove maxlength attribute from textarea after character count JavaScript has been initialised #2590

Merged

Conversation

andymantell
Copy link
Contributor

@andymantell andymantell commented Apr 7, 2022

If a maxlength attribute is specified on the textarea, this should be removed after the character-count JS has kicked in. At the moment however, the JS is trying to remove the attribute from the wrapping div which does not do anything.

andymantell added a commit to andymantell/govuk-frontend that referenced this pull request Apr 7, 2022
@andymantell andymantell marked this pull request as ready for review April 7, 2022 15:23
@andymantell
Copy link
Contributor Author

Only saw the big refactor going on over in #2577 after I'd opened this. Might just be something to fold into that? Up to you anyway /shrug

@vanitabarrett vanitabarrett added 🐛 bug Something isn't working the way it should (including incorrect wording in documentation) character count labels Apr 8, 2022
@36degrees
Copy link
Contributor

Great spot @andymantell – thanks!

Digging in to the history, it looks like this might have been broken since the component was introduced in #959 as we've always tried to remove the maxlength from the $module.

I think originally we had planned to set the character limit using the maxlength attribute. We to avoid maxlength elsewhere because if users hit the limit they are unable to enter any more characters but don't get any feedback to tell them why. So we opted to use a data attribute instead, which has no effect when the JS doesn't kick in, and rely on server-side validation to enforce the limit.

There was some discussion about the value of removing the maxlength attribute in that PR, presumably as the code was refactored when that decision was made?

I definitely agree that the current code is 'broken' as it doesn't do anything useful, but I am wondering if we should just remove it. As far as I know, the only way to set maxlength using the Nunjucks macro is to set it as part of the attributes object, and we would generally prefer users didn't set maxlength at all.

@andymantell Are you setting maxlength and therefore reliant on this behaviour and its fix? Or did you just spot the mistake?

@andymantell
Copy link
Contributor Author

I've got maxlength set at the moment, because I'm starting building something on 111 online from a no-JS perspective and half figured it was best to have it in there. But I am very open to being convinced that I shouldn't use it at all. Of course I'll still need some serverside validation anyway.

So yeah I'm not reliant on it at all - if that's the recommendation, I'm quite happy to drop it from what I'm building and we can close this off.

@andymantell
Copy link
Contributor Author

andymantell commented Apr 8, 2022

Although I guess the purpose of this line of code is still valid - that if someone like myself has added maxlength in for whatever reason (even if that reason is questionable), you really don't want it left in there as it scuppers the character count code.

Therefore, you should maybe keep it in? And forcibly remove it from the textarea.

@vanitabarrett vanitabarrett added this to the [NEXT] milestone Apr 12, 2022
@vanitabarrett
Copy link
Contributor

Thanks for raising this PR @andymantell . A couple of us have had a discussion about this and while I think we're a bit 50/50 on whether to accept the PR and fix the logic or just remove it entirely, the easiest option for us right now would be to fix what this line was originally intended to do.

@andymantell are you ok to rebase this PR against the latest changes in main before I give it a review?

@andymantell
Copy link
Contributor Author

andymantell commented Apr 14, 2022 via email

@andymantell andymantell force-pushed the fix/character-count-maxlength branch from 3ca3db5 to 2ec4c14 Compare April 19, 2022 17:12
@andymantell
Copy link
Contributor Author

andymantell commented Apr 19, 2022

@vanitabarrett This is up to date with main now and conflicts resolved 👍🏻

@vanitabarrett
Copy link
Contributor

Looks good to me. This just needs to be approved by someone else in the team, and then it can be merged.

@vanitabarrett vanitabarrett requested a review from a team April 20, 2022 09:02
@querkmachine querkmachine self-requested a review April 20, 2022 09:07
@vanitabarrett vanitabarrett merged commit cea3233 into alphagov:main Apr 20, 2022
@andymantell
Copy link
Contributor Author

Thanks all 👍🏻

@EoinShaughnessy EoinShaughnessy changed the title Maxlength attribute should not be present on textarea after character-count JavaScript has kicked in maxlength attribute should not be present on textarea after character count JavaScript has kicked in Apr 25, 2022
@EoinShaughnessy EoinShaughnessy changed the title maxlength attribute should not be present on textarea after character count JavaScript has kicked in Remove maxlength attribute from textarea after character count JavaScript has kicked in Apr 25, 2022
@EoinShaughnessy EoinShaughnessy changed the title Remove maxlength attribute from textarea after character count JavaScript has kicked in Remove maxlength attribute from textarea after character count JavaScript has been initialised Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working the way it should (including incorrect wording in documentation) character count
Projects
Development

Successfully merging this pull request may close these issues.

4 participants