File tree 9 files changed +69
-6
lines changed
9 files changed +69
-6
lines changed Original file line number Diff line number Diff line change @@ -77,7 +77,7 @@ params:
77
77
- name : checked
78
78
type : boolean
79
79
required : false
80
- description : If `true`, the checkbox will be checked when the page loads.
80
+ description : Whether the checkbox should be checked when the page loads. Takes precedence over the top-level `values` option .
81
81
- name : conditional
82
82
type : boolean
83
83
required : false
@@ -899,3 +899,17 @@ examples:
899
899
text : British
900
900
- value : irish
901
901
text : Irish
902
+
903
+ - name : item checked overrides values
904
+ hidden : true
905
+ data :
906
+ name : colors
907
+ items :
908
+ - value : red
909
+ text : Red
910
+ - value : green
911
+ text : Green
912
+ checked : false
913
+ - value : blue
914
+ text : Blue
915
+ values : [red, green]
Original file line number Diff line number Diff line change 63
63
{%- if item .divider %}
64
64
<div class =" govuk-checkboxes__divider" >{{ item .divider }} </div >
65
65
{%- else %}
66
- {% set isChecked = item .checked or (params .values and item .value in params .values ) %}
66
+ {% set isChecked = item .checked | default (params .values and item .value in params .values ) %}
67
67
{% set hasHint = true if item .hint .text or item .hint .html %}
68
68
{% set itemHintId = id + " -item-hint" if hasHint else " " %}
69
69
{% set itemDescribedBy = describedBy if not hasFieldset else " " %}
Original file line number Diff line number Diff line change @@ -194,6 +194,13 @@ describe('Checkboxes', () => {
194
194
expect ( $other . attr ( 'checked' ) ) . toEqual ( 'checked' )
195
195
} )
196
196
197
+ it ( 'allows item.checked to override values' , ( ) => {
198
+ const $ = render ( 'checkboxes' , examples [ 'item checked overrides values' ] )
199
+
200
+ const $green = $ ( '.govuk-checkboxes' ) . find ( 'input[value="green"]' )
201
+ expect ( $green . attr ( 'checked' ) ) . toBeUndefined ( )
202
+ } )
203
+
197
204
describe ( 'when they include attributes' , ( ) => {
198
205
it ( 'it renders the attributes' , ( ) => {
199
206
const $ = render ( 'checkboxes' , examples [ 'items with attributes' ] )
Original file line number Diff line number Diff line change @@ -69,7 +69,7 @@ params:
69
69
- name : checked
70
70
type : boolean
71
71
required : false
72
- description : If `true`, the radio will be checked when the page loads.
72
+ description : Whether the radio should be checked when the page loads. Takes precedence over the top-level `value` option .
73
73
- name : conditional
74
74
type : string
75
75
required : false
@@ -889,3 +889,17 @@ examples:
889
889
- value : no
890
890
text : No
891
891
checked : true
892
+
893
+ - name : item checked overrides value
894
+ hidden : true
895
+ data :
896
+ name : colors
897
+ items :
898
+ - value : red
899
+ text : Red
900
+ - value : green
901
+ text : Green
902
+ checked : false
903
+ - value : blue
904
+ text : Blue
905
+ value : green
Original file line number Diff line number Diff line change 56
56
{%- if item .divider %}
57
57
<div class =" govuk-radios__divider" >{{ item .divider }} </div >
58
58
{%- else %}
59
- {% set isChecked = item .checked or (params .value and item .value == params .value ) %}
59
+ {% set isChecked = item .checked | default (params .value and item .value == params .value ) %}
60
60
{% set hasHint = true if item .hint .text or item .hint .html %}
61
61
{% set itemHintId = id + ' -item-hint' %}
62
62
<div class =" govuk-radios__item" >
Original file line number Diff line number Diff line change @@ -137,6 +137,13 @@ describe('Radios', () => {
137
137
expect ( $lastInput . attr ( 'checked' ) ) . toEqual ( 'checked' )
138
138
} )
139
139
140
+ it ( 'allows item.checked to override value' , ( ) => {
141
+ const $ = render ( 'radios' , examples [ 'item checked overrides value' ] )
142
+
143
+ const $green = $ ( '.govuk-radios' ) . find ( 'input[value="green"]' )
144
+ expect ( $green . attr ( 'checked' ) ) . toBeUndefined ( )
145
+ } )
146
+
140
147
describe ( 'when they include attributes' , ( ) => {
141
148
it ( 'it renders the attributes' , ( ) => {
142
149
const $ = render ( 'radios' , examples [ 'items with attributes' ] )
Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ params:
23
23
- name : selected
24
24
type : boolean
25
25
required : false
26
- description : Sets the option as selected when the page loads.
26
+ description : Whether the option should be selected when the page loads. Takes precedence over the top-level `value` option .
27
27
- name : disabled
28
28
type : boolean
29
29
required : false
@@ -304,3 +304,17 @@ examples:
304
304
-
305
305
value : 2
306
306
text : GOV.UK frontend option 2
307
+
308
+ - name : item selected overrides value
309
+ hidden : true
310
+ data :
311
+ name : colors
312
+ items :
313
+ - value : red
314
+ text : Red
315
+ - value : green
316
+ text : Green
317
+ selected : false
318
+ - value : blue
319
+ text : Blue
320
+ value : green
Original file line number Diff line number Diff line change 42
42
{% for item in params .items %}
43
43
{% if item %}
44
44
<option value =" {{ item.value }}"
45
- {{- " selected" if item.selected or (params.value and item.value == params.value) }}
45
+ {{- " selected" if item.selected | default (params.value and item.value == params.value) }}
46
46
{{- " disabled" if item.disabled }}
47
47
{%- for attribute, value in item.attributes %} {{ attribute }} =" {{ value }}" {% endfor -%} >{{ item .text }} </option >
48
48
{% endif %}
Original file line number Diff line number Diff line change @@ -69,6 +69,13 @@ describe('Select', () => {
69
69
expect ( $selectedItem . attr ( 'selected' ) ) . toBeTruthy ( )
70
70
} )
71
71
72
+ it ( 'allows item.selected to override value' , ( ) => {
73
+ const $ = render ( 'select' , examples [ 'item selected overrides value' ] )
74
+
75
+ const $selectedItem = $ ( 'option[value="green"]' )
76
+ expect ( $selectedItem . attr ( 'selected' ) ) . toBeUndefined ( )
77
+ } )
78
+
72
79
it ( 'renders item with disabled' , ( ) => {
73
80
const $ = render ( 'select' , examples . default )
74
81
You can’t perform that action at this time.
0 commit comments