-
Notifications
You must be signed in to change notification settings - Fork 319
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
Fix Nunjucks default()
values when null
, false
or ""
is provided
#4327
Conversation
๐ StatsFile sizes
Modules
View stats and visualisations on the review app Action run for 61b64d4 |
default()
for โfalseyโ valuesdefault()
for โfalseyโ values
7d5e453
to
07b70ff
Compare
@@ -47,7 +47,7 @@ | |||
{# Allow selecting by text content (the value for an option when no value attribute is specified) #} | |||
{% set effectiveValue = item.value | default(item.text) %} | |||
<option {%- if item.value !== undefined %} value="{{ item.value }}"{% endif %} | |||
{{-" selected" if item.selected | default(effectiveValue == params.value if params.value else false, true) }} | |||
{{-" selected" if item.selected | default((effectiveValue == params.value and item.selected != false) if params.value else false, true) }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When will this trigger? My brain's spinning in circles...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a weird case where govukSelect()
can have a selected value but the item itself is unselected ๐คฏ
So we want to keep the falsey value as long as it's actually false
and not null
or ""
govuk-frontend/packages/govuk-frontend/src/govuk/components/select/select.yaml
Lines 350 to 358 in 72c543a
items: | |
- value: red | |
text: Red | |
- value: green | |
text: Green | |
selected: false | |
- value: blue | |
text: Blue | |
value: green |
It's covered by this test case:
govuk-frontend/packages/govuk-frontend/src/govuk/components/select/template.test.js
Lines 101 to 106 in 72c543a
it('allows item.selected to override value', () => { | |
const $ = render('select', examples['item selected overrides value']) | |
const $selectedItem = $('option[value="green"]') | |
expect($selectedItem.attr('selected')).toBeUndefined() | |
}) |
Looks good, I just need an explainer on the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was going to do a foot emoji and a bug emoji for squashing the bug, but let's go with releasing the bug into the garden instead.
๐ ๐๏ธ
default()
for โfalseyโ valuesdefault()
values when null
, false
or ""
is provided
Thanks @domoscargin just added a CHANGELOG entry too |
Fix Nunjucks `default()` values when `null`, `false` or `""` is provided
This PR ensures Nunjucks
default()
handles all possible "falsey" valuesIt closes #4305 which shows that
undefined
values fall back todefaults()
but notnull
,false
,""
etcNunjucks default example
For example, passing
redirectUrl: null
into Exit this page renders a default submit button ๐ฎThe fallback
href
value isn't used: