Components
Select
The select component allows users to select a single value from a list of options.
Contents
Where possible, avoid overusing the select component. Alice Bartlett gave a talk at EpicFEL called "Burn your select tags" which explains when the <select>
tag is not the most appropriate tag to use.
HTML
<div class="tna-form-item">
<div class="tna-form-item__header">
<h4 class="tna-form-item__label tna-heading-m">
<label for="sort">
Sort by
</label>
</h4>
</div>
<div class="tna-form-item__body">
<select class="tna-select" name="sort" id="sort">
<option value="relevance">Relevance</option>
<option value="date">Date</option>
<option value="title">Title</option>
</select>
</div>
</div>
Nunjucks
Nunjucks options
Name | Type | Description |
---|---|---|
label | string |
Required. The label for the select field. |
headingLevel | number |
The heading level which represents an element from |
headingSize | string |
The physical size of the select title ( Default value: |
id | string |
Required. A unique ID for the select component. |
name | string |
Required. The name of the form field. |
hint | string |
An optional hint to display above the form field. |
error | object |
If set, the details of any errors. |
items | array |
Required. |
selected | string | |
size | string | |
formItemClasses | string |
Classes to add to the select form group. |
formItemAttributes | object |
HTML attributes (for example data attributes) to add to the select form group. |
classes | string |
Classes to add to the select. |
attributes | object |
HTML attributes (for example data attributes) to add to the select. |
Name | Type | Description |
---|---|---|
text | string |
Required. The error text to display. |
Name | Type | Description |
---|---|---|
text | string |
Required. |
value | string |
Required. The form field value. |
{% from "nationalarchives/components/select/macro.njk" import tnaSelect %}
{{ tnaSelect({
label: "Sort by",
headingLevel: 4,
headingSize: "m",
id: "sort",
name: "sort",
items: [
{
text: "Relevance",
value: "relevance"
},
{
text: "Date",
value: "date"
},
{
text: "Title",
value: "title"
}
]
}) }}
Component status
- Phase
- To be reviewed
- Tested without CSS
- Yes
- Passed DAC audit
- Not yet audied
- Analytics integrated
- Yes
- Documentation complete
- No
Preselected
HTML
<div class="tna-form-item">
<div class="tna-form-item__header">
<h4 class="tna-form-item__label tna-heading-m">
<label for="sort">
Sort by
</label>
</h4>
</div>
<div class="tna-form-item__body">
<select class="tna-select" name="sort" id="sort">
<option value="relevance">Relevance</option>
<option value="date" selected>Date</option>
<option value="title">Title</option>
</select>
</div>
</div>
Nunjucks
Nunjucks options
Name | Type | Description |
---|---|---|
label | string |
Required. The label for the select field. |
headingLevel | number |
The heading level which represents an element from |
headingSize | string |
The physical size of the select title ( Default value: |
id | string |
Required. A unique ID for the select component. |
name | string |
Required. The name of the form field. |
hint | string |
An optional hint to display above the form field. |
error | object |
If set, the details of any errors. |
items | array |
Required. |
selected | string | |
size | string | |
formItemClasses | string |
Classes to add to the select form group. |
formItemAttributes | object |
HTML attributes (for example data attributes) to add to the select form group. |
classes | string |
Classes to add to the select. |
attributes | object |
HTML attributes (for example data attributes) to add to the select. |
Name | Type | Description |
---|---|---|
text | string |
Required. The error text to display. |
Name | Type | Description |
---|---|---|
text | string |
Required. |
value | string |
Required. The form field value. |
{% from "nationalarchives/components/select/macro.njk" import tnaSelect %}
{{ tnaSelect({
label: "Sort by",
headingLevel: 4,
headingSize: "m",
id: "sort",
name: "sort",
items: [
{
text: "Relevance",
value: "relevance"
},
{
text: "Date",
value: "date"
},
{
text: "Title",
value: "title"
}
],
selected: "date"
}) }}
Hint
HTML
<div class="tna-form-item">
<div class="tna-form-item__header">
<h4 class="tna-form-item__label tna-heading-m">
<label for="sort">
Sort by
</label>
</h4>
</div>
<p id="sort-hint" class="tna-form-item__hint">
Select a property to sort by.
</p>
<div class="tna-form-item__body">
<select class="tna-select" name="sort" id="sort" aria-describedby="sort-hint">
<option value="relevance">Relevance</option>
<option value="date">Date</option>
<option value="title">Title</option>
</select>
</div>
</div>
Nunjucks
Nunjucks options
Name | Type | Description |
---|---|---|
label | string |
Required. The label for the select field. |
headingLevel | number |
The heading level which represents an element from |
headingSize | string |
The physical size of the select title ( Default value: |
id | string |
Required. A unique ID for the select component. |
name | string |
Required. The name of the form field. |
hint | string |
An optional hint to display above the form field. |
error | object |
If set, the details of any errors. |
items | array |
Required. |
selected | string | |
size | string | |
formItemClasses | string |
Classes to add to the select form group. |
formItemAttributes | object |
HTML attributes (for example data attributes) to add to the select form group. |
classes | string |
Classes to add to the select. |
attributes | object |
HTML attributes (for example data attributes) to add to the select. |
Name | Type | Description |
---|---|---|
text | string |
Required. The error text to display. |
Name | Type | Description |
---|---|---|
text | string |
Required. |
value | string |
Required. The form field value. |
{% from "nationalarchives/components/select/macro.njk" import tnaSelect %}
{{ tnaSelect({
label: "Sort by",
headingLevel: 4,
headingSize: "m",
id: "sort",
name: "sort",
hint: "Select a property to sort by.",
items: [
{
text: "Relevance",
value: "relevance"
},
{
text: "Date",
value: "date"
},
{
text: "Title",
value: "title"
}
]
}) }}
Error
HTML
<div class="tna-form-item tna-form-item--error">
<div class="tna-form-item__header">
<h4 class="tna-form-item__label tna-heading-m">
<label for="sort">
Sort by
</label>
</h4>
</div>
<p id="sort-error" class="tna-form-item__error">
<span class="tna-!--visually-hidden">Error:</span> You must select something to sort by
</p>
<div class="tna-form-item__body">
<select class="tna-select" name="sort" id="sort">
<option value="relevance">Relevance</option>
<option value="date">Date</option>
<option value="title">Title</option>
</select>
</div>
</div>
Nunjucks
Nunjucks options
Name | Type | Description |
---|---|---|
label | string |
Required. The label for the select field. |
headingLevel | number |
The heading level which represents an element from |
headingSize | string |
The physical size of the select title ( Default value: |
id | string |
Required. A unique ID for the select component. |
name | string |
Required. The name of the form field. |
hint | string |
An optional hint to display above the form field. |
error | object |
If set, the details of any errors. |
items | array |
Required. |
selected | string | |
size | string | |
formItemClasses | string |
Classes to add to the select form group. |
formItemAttributes | object |
HTML attributes (for example data attributes) to add to the select form group. |
classes | string |
Classes to add to the select. |
attributes | object |
HTML attributes (for example data attributes) to add to the select. |
Name | Type | Description |
---|---|---|
text | string |
Required. The error text to display. |
Name | Type | Description |
---|---|---|
text | string |
Required. |
value | string |
Required. The form field value. |
{% from "nationalarchives/components/select/macro.njk" import tnaSelect %}
{{ tnaSelect({
label: "Sort by",
headingLevel: 4,
headingSize: "m",
id: "sort",
name: "sort",
error: {
text: "You must select something to sort by"
},
items: [
{
text: "Relevance",
value: "relevance"
},
{
text: "Date",
value: "date"
},
{
text: "Title",
value: "title"
}
]
}) }}