Open andymantell opened 2 years ago
@andymantell thanks for adding this!
When implementing this feature, we did debate various options for scoping the grouping of checkboxes, including using the "module" scope (ie the parent with data-module="checkboxes"), however we [avoided this](https://github.com/alphagov/govuk-frontend/pull/2151#discussion_r650941963) as there is a use-case where you might want to divide a group of checkboxes into sub-groups using either headings, which you can currently do by calling the
govukCheckboxesmacro multiple times, but using the same
name` value.
Another alternative which could be explored again is relying on the <fieldset>
to group the checkboxes, which is at least the more semantic HTML way of doing this. The only caveat is that you might have nested fieldsets, in which case it would be unclear which level the grouping should be applied at.
So on balance, I suspect that the idea of having a manual-override of using the data attribute is probably the most flexible solution (and backwards-compatible).
I would suggest naming the attribute as something like data-behaviour-group
, as the checkbox
bit is redundant, and using the behaviour
rather than exclusive
allows the same attribute to be used for as-yet-unimplemented future behaviours (like a "select all", which might be behaviour="inclusive"
).
As the macro already supports arbitrary attributes on the <input>
, I think the only change this would require is a bit of extra logic in the javascript?
On nhsuk-frontend I've used exclusive
and exclusiveGroup
params on the nunjucks macro and render some data attributes from those (And never use name
at all). But yes, you could definitely just do it with the existing attributes
param and adjust the JS to suit. It might be a little fiddly? If you're defaulting to name
, but allowing data-behaviour-group
or whatever then you'd have to work out which one was being used when you're unticking groups of checkboxes 🤔
Description of the issue
The new "none of these" checkbox pattern (https://design-system.service.gov.uk/components/checkboxes/with-none-option/index.html) assumes that all the checkboxes in the group will have the same
name
attribute. However this is not necessarily the case in all frameworks. In .Net it would be quite reasonable for each checkbox input to be a different property on the underlying model, which maps to a differentname
in the html output.Actual vs expected behaviour
Ideally it would be better if it was possible to configure the grouping of checkboxes manually. Over on nhsuk-frontend I have done this by grouping them with a data attribute rather than name. Like this (heavily abridged) example:
so they each have different names, but all share a
data-checkbox-exclusive-group
ofcommunication-preferences
I spoke to @frankieroberto about this on Slack and he suggested maybe defaulting to grouping by name, but allowing people to group explicitly if necessary. This could be a small feature release then, rather than necessitating a breaking change.