-
Notifications
You must be signed in to change notification settings - Fork 323
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
Allow character count config set via data attributes to be passed when initialising the component in JavaScript #2837
Comments
Fun bit with this component there's a bit more precedence than just data-attributes over JavaScript configuration. Internally, the component also gives precedence to For example when running: new CharacterCount($element, {maxwords: 150}).init(); with <div class="govuk-character-count" data-module="govuk-character-count" data-maxlength="200">
<!-- Omitted for clarity -->
</div> In that case, I'd expect that the presence of Just wanted to check that it's what we all expect of the component behaviour as that requires a slightly different merging strategy compared to the components we've added config to so far. In terms of practicalities, I'm thinking of cloning and cleaning the JavaScript config of its |
As long as there's a way to 'switch back' to character-counting mode I think we should do whatever's simplest – if we end up introducing a 'special case' for this component it'll limit our ability to move the config-merging logic into a base component later. What if we made it so that you can disable the So this combination of JS config and data attributes would count characters: new CharacterCount($element, {maxwords: 150}).init(); <div class="govuk-character-count" data-module="govuk-character-count" data-maxlength="200" data-maxwords="false">
<!-- Omitted for clarity -->
</div> |
This would cancel out the Doing some pre-processing doesn't necessarily prevent abstracting the merging of configurations. We could for example pass the shared merging logic a function to do some pre-processing before merging, letting components cleanup, disambiguate similar keys or what not. I've got a Ruby example of such API in this library I worked on ( |
OK. I am wondering if we should revisit the API for the character count to try and avoid this being a problem. We could potentially have a config option which determines what 'type' of count to use ( That would also set us up to allow a custom counting function to be passed in the future, closing #1364. This might be something we could look at now – we would need to maintain the existing behaviour but we could mark parts of the current API (probably Alternatively we could just write this up as something to revisit in the future. |
Revisiting the API sounds a good idea long term. Not only it would solve that conflict between This would obviously be quite a breaking change to the API, so we should put some deprecations in place. I guess whether we do this part of this chunk of work depends on whether we're planning further releases between the upcoming one and 5.0. |
At the minute, I'm expecting that we'll start thinking about 5.0 once we've got this release out, so I'm not anticipating much in the way of further 4.x releases, unless there are things that come out of the remaining 'future improvements' sessions that we think we want to ship before 5.0, or we have any contributions that are ready to release before 5.0 (summary card, hide this page). |
What
The character count component currently uses data attributes to configure the component:
data-maxlength
to determine the character limit displayed to the userdata-maxwords
to determine the word count displayed to the user, if it makes more sense to count words than charactersdata-threshold
to determine a cut-off point at which the count message should be displayedUpdate the component so that this config can also be passed as an object when initialising the component in JavaScript.
As per #1708 (comment), any config passed via data attributes takes precedence over config passed when initialising a component.
Why
This aligns the general concept of 'component config' with the approach we're using for internationalisation.
Who needs to work on this
Developers
Who needs to review this
Developers
Done when
initAll
The text was updated successfully, but these errors were encountered: