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

Check component item arrays are not empty before outputting markup #1638

Merged
merged 3 commits into from
Nov 20, 2019

Conversation

nataliecarey
Copy link
Contributor

Overview

When using the Nunjucks components there are a number of optional arrays, there's one behaviour when you provide something and another when you provide nothing. At HMRC we've found some confusion over the difference between providing nothing and providing an empty array.

This change standardises that behaviour so that an empty array is treated as nothing being provided.

Scope

This only affects the Nunjucks interface in its handling of empty arrays, users who aren't providing empty arrays will not be affected by this change.

There are no CSS or JS changes

Tests

We have added tests for the new behaviour.

Copy link

@silasl silasl left a comment

Choose a reason for hiding this comment

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

This PR addresses the issue raised here #1618

@NickColley
Copy link
Contributor

NickColley commented Nov 12, 2019

Spoke to Nat and they've said that they're going to be away for a week but @silasl (hi 👋 ) can help out in that time.

We'll review this as a team tomorrow in our weekly triage session.

@36degrees
Copy link
Contributor

It'd be good to remove the change to package-lock.json, which I believe is unrelated and just down to different versions of npm being used.

(I've not had a chance to review the rest of the PR yet)

@36degrees
Copy link
Contributor

Thanks @natcarey!

@kellylee-gds kellylee-gds removed the awaiting triage Needs triaging by team label Nov 13, 2019
@NickColley NickColley added this to the v3.4.0 milestone Nov 13, 2019
@NickColley NickColley changed the title Made empty arrays behave the same as no array. Check component item arrays are not empty before outputting markup Nov 13, 2019
Copy link
Contributor

@NickColley NickColley left a comment

Choose a reason for hiding this comment

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

This looks great to me! Thanks a lot.

I tried to help out and add a CHANGELOG entry myself but I don't have permission to push to your branch.

- [Pull request #1638: Check component item arrays are not empty before outputting markup](https://github.com/alphagov/govuk-frontend/pull/1638).

I think if we update the CHANGELOG and get another approval from someone else on the team we can get this merged and released in v3.4.0

Copy link
Member

@hannalaakso hannalaakso left a comment

Choose a reason for hiding this comment

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

This is a useful change, thanks for all your work @natcarey.

Please note that we shouldn't be committing any package files in this PR. They will get generated as part of the release process. So those need to be removed before this can be merged.

Copy link
Contributor

@NickColley NickColley left a comment

Choose a reason for hiding this comment

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

We had a small review internally, two things I missed:

  1. some changes have been made in the package directory instead of the src directory
  2. we are wondering if there are more places we need to do this: for instance accordion, breadcrumbs, select also have an array of items

Oops sorry for the double feedback 😇

@NickColley NickColley removed this from the v3.4.0 milestone Nov 14, 2019
@NickColley
Copy link
Contributor

We're going to release 3.4.0 then do a small patch release with this once you've had time to look at this :)

@nataliecarey
Copy link
Contributor Author

I've updated with that feedback and I'm working as usual this week.

Thanks all.

@nataliecarey
Copy link
Contributor Author

I just spotted this comment "we are wondering if there are more places we need to do this: for instance accordion, breadcrumbs, select also have an array of items", I haven't addressed that yet. I'll look at that this morning.

@nataliecarey
Copy link
Contributor Author

@NickColley said:

we are wondering if there are more places we need to do this: for instance accordion, breadcrumbs, select also have an array of items

I don't think these are affected by the same problem.

These examples are going to behave strangely when an empty array or no array is provided but the behaviour of an empty array matches the behaviour of no array. In the components I've updated there is a difference between the handling of "nothing" and an empty array.

Adding conditions around the template would help avoid a nonsense output but it isn't needed for consistency between these two behaviours. For example govukAccordion has a for loop which iterates the items and checks each one is not empty; If there's an empty array or no array the outer div (.govuk-accordion) is still output but it will be empty. A condition of {% if params.items | length %} would help with this but it changes the behaviour for both an empty array and no array.

CHANGELOG.md Outdated
@@ -49,6 +49,7 @@ You can now add attributes to the `<body>` element of a page, by using the [`bod
- [Pull request #1609: Update hex value for secondary text to improve contrast](https://github.com/alphagov/govuk-frontend/pull/1609).
- [Pull request #1620: Only add underline to back link when href exists ](https://github.com/alphagov/govuk-frontend/pull/1620).
- [Pull request #1631: Fix classes on character count when in error state](https://github.com/alphagov/govuk-frontend/pull/1631).
- [Pull request #1638: Check component item arrays are not empty before outputting markup](https://github.com/alphagov/govuk-frontend/pull/1638).
Copy link
Contributor

Choose a reason for hiding this comment

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

Something I missed, this changelog entry needs to be in an 'unreleased section' instead of being in the previous release

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Morning. Just to check I've got this right - I'm putting it on Line 6 so the start of the file reads:

# Changelog

## Unreleased

### New features
- [Pull request #1638: Check component item arrays are not empty before outputting markup](https://github.com/alphagov/govuk-frontend/pull/1638).

Is that right?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah that looks great but 'New features' should be 'Fixes'

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, this probably needs updating again as v3.4.0 has now been released.

Copy link
Member

@hannalaakso hannalaakso left a comment

Choose a reason for hiding this comment

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

This is looking great 🙌 Just need @NickColley to give a second team thumbs up.

I don't think these are affected by the same problem. These examples are going to behave strangely when an empty array or no array is provided but the behaviour of an empty array matches the behaviour of no array. In the components I've updated there is a difference between the handling of "nothing" and an empty array.

I think @natcarey is correct here. The other components that utilise the items array are accordion, checkboxes, radios and select. With these ones there is no redundant markup output if items is empty, like there is with the ones fixed in this PR. I guess we could decide to make it so that whenever you use arrays in templates you should check the length, regardless of whether anything is output if the array is empty but I think that's separate from this work as the scope of this PR was to "Check component item arrays are not empty before outputting markup".

Copy link
Contributor

@NickColley NickColley left a comment

Choose a reason for hiding this comment

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

Great work thanks Nat! Also thanks @hannalaakso for the sharp eye on the review 😅

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.

6 participants