Skip to content

Commit

Permalink
Allow preselected radio and checkboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
ahosgood committed Oct 11, 2023
1 parent 3b25c3e commit 01a3501
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/nationalarchives/components/checkboxes/checkboxes.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,28 @@ Small.args = {
small: true,
classes: "tna-checkboxes--demo",
};

export const Preselected = Template.bind({});
Preselected.args = {
label: "Categories",
headingLevel: 4,
headingSize: "m",
name: "categories3",
hint: "Select all that apply.",
items: [
{
text: "Alpha",
value: "alpha",
},
{
text: "Beta",
value: "beta",
checked: true
},
{
text: "Gamma",
value: "gamma",
},
],
classes: "tna-checkboxes--demo",
};
2 changes: 1 addition & 1 deletion src/nationalarchives/components/checkboxes/template.njk
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<div class="tna-checkboxes{% if params.small %} tna-checkboxes--small{% endif %}">
{%- for item in params.items %}
<div class="tna-checkboxes__item">
<input type="checkbox" id="tna-form__{{ params.name }}-{{ item.value }}" value="{{ item.value }}" name="{{ params.name }}">
<input type="checkbox" id="tna-form__{{ params.name }}-{{ item.value }}" value="{{ item.value }}" name="{{ params.name }}"{% if item.checked %} checked{% endif %}>
<label for="tna-form__{{ params.name }}-{{ item.value }}" class="tna-checkboxes__item-label">
{{ item.text }}
</label>
Expand Down
58 changes: 57 additions & 1 deletion src/nationalarchives/components/radios/radios.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const argTypes = {
name: { control: "text" },
hint: { control: "text" },
items: { control: "object" },
selected: { control: "text" },
small: { control: "boolean" },
classes: { control: "text" },
attributes: { control: "object" },
Expand All @@ -31,6 +32,7 @@ const Template = ({
name,
hint,
items,
selected,
small,
classes,
attributes,
Expand All @@ -43,6 +45,7 @@ const Template = ({
name,
hint,
items,
selected,
small,
classes,
attributes,
Expand Down Expand Up @@ -80,6 +83,59 @@ Small.args = {
headingSize: "m",
name: "type2",
hint: "You can only select one.",
items: [
{
text: "Admiralty, Navy, Royal Marines, and Coastguard",
value: "ADM",
},
{
text: "Air Ministry and Royal Air Force records",
value: "AIR",
},
{
text: "Board of Trade and successors",
value: "BT",
},
{
text: "Chancery, the Wardrobe, Royal Household, Exchequer and various commissions",
value: "C",
},
{
text: "Colonial Office, Commonwealth and Foreign and Commonwealth Offices",
value: "CO",
},
{
text: "Exchequer, Office of First Fruits and Tenths, and the Court of Augmentations",
value: "E",
},
{
text: "Foreign Office",
value: "FO",
},
{
text: "Home Office",
value: "HO",
},
{
text: "Prerogative Court of Canterbury",
value: "PROB",
},
{
text: "War Office, Armed Forces, Judge Advocate General, and related bodies",
value: "WO",
},
],
small: true,
classes: "tna-radios--demo",
};

export const Preselected = Template.bind({});
Preselected.args = {
label: "Type",
headingLevel: 4,
headingSize: "m",
name: "type3",
hint: "You can only select one.",
items: [
{
text: "Audio",
Expand All @@ -94,6 +150,6 @@ Small.args = {
value: "video",
},
],
small: true,
selected: "image",
classes: "tna-radios--demo",
};
2 changes: 1 addition & 1 deletion src/nationalarchives/components/radios/template.njk
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<div class="tna-radios{% if params.small %} tna-radios--small{% endif %}">
{%- for item in params.items %}
<div class="tna-radios__item">
<input type="radio" id="tna-form__{{ params.name }}-{{ item.value }}" value="{{ item.value }}" name="{{ params.name }}">
<input type="radio" id="tna-form__{{ params.name }}-{{ item.value }}" value="{{ item.value }}" name="{{ params.name }}"{% if params.selected and item.value === params.selected %} checked{% endif %}>
<label for="tna-form__{{ params.name }}-{{ item.value }}" class="tna-radios__item-label">
{{ item.text }}
</label>
Expand Down

0 comments on commit 01a3501

Please sign in to comment.