-
Notifications
You must be signed in to change notification settings - Fork 339
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
Test for empty items in radios and checkboxes #1494
Comments
I've put together a live demonstration to show what Ed is talking about here: https://govuk-frontend-issue-1494.glitch.me/ - (edit: https://glitch.com/edit/#!/govuk-frontend-issue-1494) |
Should we also consider doing something similar for:
|
If I'm following along correctly... you want to declaratively use that macro by checking if a flag is true and adding in data based on the condition? I've ran into this sort of thing before and I ended up using additional filters for manipulating data within the nunjucks templates. So I expose something like lodash as nunjucks filters which can then be piped together. In this case might create the first part of the array a separate variable using Off the top of my head something like:
With |
@paulmsmith thanks for that! For the context of this issue, I feel like the macro should be more defensive and not output anything where the input is blank. As a byproduct that also solves my issue ;) Separately I'm somewhat sure I've seen a way of doing this in Nunjucks directly - but can't recall where / what it is. |
I've had a go at this and it looks like a sensible suggestion to only output items when they are truthy like Ed has suggested. Will see what the rest of the team thinks, we'd want to do this for all the components I've mentioned above as well. |
I agree, the macro should be only outputting stuff when it has valid data. :) |
Thanks Ed, we've added this to our backlog as part of the improve macros in GOV.UK Frontend epic. If you wanted to contribute this sooner, feel free to raise a pull request. |
A similar thing would be good for tables - testing items inside |
I believe this was addressed in #1512 |
@36degrees Should I raise my above issue with empty conditionals separately? or is that also solved with #1512 |
Ah, sorry @edwardhorsford, my bad. If you can raise a separate issue that'd be great – I don't believe it's solved by #1512 (though we do test for |
I'd like to propose testing if items inside the
items
array for checkboxes and radios are empty. If they are, don't output markup.My use case
I want to conditionally show a radio based on some data.
In Nunjucks I can alter a value of an item in an array by adding a conditional after it - but it still outputs an empty item and I've not worked out how to not provide it.
I could prepare my data in advance - but for a simple thing like this, doing it directly is more readable IMHO. You can see the logic directly.
Example macro:
This mostly works - but right now outputs a blank space and random html doing nothing:
![Screenshot 2019-07-12 at 15 23 36](https://user-images.githubusercontent.com/2204224/61135180-0c133080-a4b9-11e9-9d2a-3542121e440a.png)
![Screenshot 2019-07-12 at 15 24 12](https://user-images.githubusercontent.com/2204224/61135217-20572d80-a4b9-11e9-8faf-4f046799591e.png)
Suggested fix
Either strip out empty items from the
items
array or else test whether they're empty when iterating over them.This feels like a safer thing to do / more defensive. We don't output markup where there's no input.
I had a quick test of this by editing the radios macro directly in my node_modules, and it appears to work well.
The text was updated successfully, but these errors were encountered: