-
Notifications
You must be signed in to change notification settings - Fork 320
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 selecting / checking values in checkboxes, radios and select by passing current value(s) #2449
Comments
this is nice!
|
This would be a nice addition - what about In the Becoming a teacher service line, we have a filter that essentially does this - originally written by @fofr, and modified by me. You can see an example of it in use here. It allows us to write much more terse UI macros, focusing on what's unique about them. Among other things, if
If |
Here are some docs on how that filter works: Extract from that pageFor example, to collect a user’s email address, you could write the following:
3 separate parameters are needed to store and display data. Each uses the same value, but formatted in a slightly different way:
Radios, checkboxes and select components also need to add a checked or selected attribute to any option that matches the stored value, and date inputs need individual values for day, month and year fields. Decorating formsThe decorate function removes this overhead. This adds name, value, id (or idPrefix) and checked/selected attributes to a GOV.UK form component, with values based on where the data is stored. The above example can now be rewritten like this:
|
Note - @fofr's version and mine are subtly different in that I pass the data it should compare, and @fofr passes a string reference to the location of the data. Using a string reference is how the |
Related component
Checkboxes, radios and select
Context
We should consider allowing the user to pass the current value (or array of values, for checkboxes?), and using that to work out which item should be checked / selected.
For example, by introducing a new top level
value
param:Alternatives
At the minute the only way to pre-select or check specific items when using the Nunjucks macros for these components is to pass a boolean flag for each item, which then maps directly to the
checked
/selected
HTML attribute for the<input>
/<option>
.This means the user has to do the work to map the previous form data back to a boolean, resulting in code that might look something like this:
This is prone to introducing errors (especially when prototyping) – for example, any changes to the
name
of the field or thevalue
of theitem
also need corresponding changes made to the boolean logic, potentially in multiple places.Additional information (if applicable)
We should consider providing this in addition to the existing boolean attribute – if so, we'll need to be clear how the two interact with each other.
We should consider how this interacts with the
checked
helper in the Prototype Kit.The text was updated successfully, but these errors were encountered: