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

Tidy up and refactor the Character Count JavaScript #2807

Merged
merged 7 commits into from
Aug 26, 2022

Conversation

36degrees
Copy link
Member

Tidying this up with the aim of making it easier to set config (e.g. maxwords, threshold) by passing a config object at initialisation in the future.

Suggest reviewing commit by commit as quite a lot of the code has been moved around.

This abstraction isn't useful as the defaults can't be overridden anywhere, the two properties are only used once, and it means we need to jump around the code to understand what's going on.
We already read all of the `data-*` attributes into the `this.options` object, so use them instead of using `getAttribute`.

This also avoids implying that `this.options.maxwords` and `$module.getAttribute('data-maxwords')` are different things – they just represent the same data attribute on the module in different ways.

Avoid the indirection introduced by the countAttribute variable by setting this.maxLength conditionally based on whether `this.options.maxwords` is set.

Merge with the return statement if neither maxwords nor maxlength are set.
@36degrees 36degrees added this to Needs review 🔍 in Design System Sprint Board via automation Aug 24, 2022
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-2807 August 24, 2022 08:59 Inactive
Copy link
Contributor

@domoscargin domoscargin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

A few small formatting comments, but nothing blocking.

@@ -1,5 +1,5 @@
import '../../vendor/polyfills/Function/prototype/bind.mjs'
import '../../vendor/polyfills/Event.mjs' // addEventListener and event.target normaliziation
import '../../vendor/polyfills/Event.mjs' // addEventListener and event.target normalization
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be super pedantic and say this should even be "normalisation"...

@@ -10,7 +10,9 @@ function CharacterCount ($module) {
this.lastInputTimestamp = null
}

// Initialize component
/**
* Initialise component
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation

* fields by directly changing its `value`. These changes don't trigger events
* in JavaScript, so we need to poll to handle when and if they occur.
*
* Once the and a keyup event hasn't been detected for at least 1000 ms (1s),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Once the and a keyup event hasn't been detected for at least 1000 ms (1s),
* Once the keyup event hasn't been detected for at least 1000 ms (1s),

/**
* Bind change events
*
* Set up events on the $textarea so that the counts update when the user
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes it sound like internal counts are updating I think.

"... count messages update..."?

Also rename some functions to clarify what they actually do:

- `formattedUpdateMessage` => `getCountMessage`
- `checkIfValueChanged` => `updateIfValueChanged`

Rename `element` to `$element` in the `getDataset` method to reflect the fact that it's an HTMLElement.

Return directly from `count` function to avoid the unneccesary `length` variable.
Try to put the methods in a logical order so that methods that call each other are in close proximity and roughly in the order that they will be called.
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-2807 August 25, 2022 15:36 Inactive
Copy link
Contributor

@domoscargin domoscargin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good to me!

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-2807 August 26, 2022 09:56 Inactive
@36degrees 36degrees merged commit ad369a4 into main Aug 26, 2022
Design System Sprint Board automation moved this from Needs review 🔍 to Done 🏁 Aug 26, 2022
@36degrees 36degrees deleted the character-count-tidy branch August 26, 2022 10:08
@36degrees 36degrees moved this from Done 🏁 to Ready to release 🚀 in Design System Sprint Board Aug 26, 2022
@romaricpascal romaricpascal mentioned this pull request Nov 10, 2022
@36degrees 36degrees moved this from Ready to release 🚀 to Done 🏁 in Design System Sprint Board Nov 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

None yet

3 participants