Components Select The select component (sometimes called a dropdown list) allows users to choose a single item from a collapsible list of options.
Example Interact with this example to see how it works.
Code Mustache / Handlebars // Developer note: ensure your ".mustache" files are imported as plain text. In Webpack you might use https://github.com/webpack-contrib/raw-loader
import Mustache from "mustache";
import SelectBlock from "@govtnz/ds/build/mustache/SelectBlock.mustache";
// The variables SelectBlock are all strings that are mustache templates.
export default `${Mustache.render(SelectBlock, {
id: `select1`,
label: `Order by`,
children: `<option value="published">Recently published</option><option value="updated">Recently updated</option><option value="views">Most views</option><option value="comments">Most comments</option>`
})}`;
React (JavaScript) import React from "react";
import SelectBlock from "@govtnz/ds/build/react-js/SelectBlock";
import "@govtnz/ds/build/css/SelectBlock.css"; // or @govtnz/ds/build/scss/SelectBlock.scss
export default () => (
<SelectBlock id="select1" label="Order by">
<option value="published">Recently published</option>
<option value="updated">Recently updated</option>
<option value="views">Most views</option>
<option value="comments">Most comments</option>
</SelectBlock>
);
React (TypeScript) import React from "react";
import SelectBlock from "@govtnz/ds/build/react-ts/SelectBlock";
import "@govtnz/ds/build/css/SelectBlock.css"; // or @govtnz/ds/build/scss/SelectBlock.scss
export default () => (
<SelectBlock id="select1" label="Order by">
<option value="published">Recently published</option>
<option value="updated">Recently updated</option>
<option value="views">Most views</option>
<option value="comments">Most comments</option>
</SelectBlock>
);
React with Styled Components (JavaScript) import React from "react";
import SelectBlock from "@govtnz/ds/build/react-js-styled-components/SelectBlock";
export default () => (
<SelectBlock id="select1" label="Order by">
<option value="published">Recently published</option>
<option value="updated">Recently updated</option>
<option value="views">Most views</option>
<option value="comments">Most comments</option>
</SelectBlock>
);
React with Styled Components (TypeScript) import React from "react";
import SelectBlock from "@govtnz/ds/build/react-ts-styled-components/SelectBlock";
export default () => (
<SelectBlock id="select1" label="Order by">
<option value="published">Recently published</option>
<option value="updated">Recently updated</option>
<option value="views">Most views</option>
<option value="comments">Most comments</option>
</SelectBlock>
);
Vue (JavaScript) <template>
<select-block id="select1" label="Order by">
<option value="published">
Recently published
</option>
<option value="updated">
Recently updated
</option>
<option value="views">
Most views
</option>
<option value="comments">
Most comments
</option>
</select-block>
</template>
<script>
import Vue from "vue";
import SelectBlock from "@govtnz/ds/build/vue-js/SelectBlock.vue";
export default { components: { "select-block": SelectBlock } };
</script>
Vue (TypeScript) <template>
<select-block id="select1" label="Order by">
<option value="published">
Recently published
</option>
<option value="updated">
Recently updated
</option>
<option value="views">
Most views
</option>
<option value="comments">
Most comments
</option>
</select-block>
</template>
<script lang="ts">
import Vue from "vue";
import SelectBlock from "@govtnz/ds/build/vue-ts/SelectBlock.vue";
export default { components: { "select-block": SelectBlock } };
</script>
HTML <!--
Remember to add these styles:
in CSS: SelectBlock.css
OR in Sass (SCSS): SelectBlock.scss
-->
<div>
<label class="g-selectBlock-label"> Order by </label>
<select class="g-selectBlock-select">
<option value="published"> Recently published </option>
<option value="updated"> Recently updated </option>
<option value="views"> Most views </option>
<option value="comments"> Most comments </option>
</select>
</div>
twig-embed {% embed "SelectBlock.html.twig" with {'id':'select1', 'label':'Order by'} only %}{% block children %}<option value="published">Recently published</option>
<option value="updated">Recently updated</option>
<option value="views">Most views</option>
<option value="comments">Most comments</option>{% endblock %}{% endembed %}
When to use it Only use a select component if you have no other option, because research shows many users have difficulty choosing items from dropdown lists. Also, select components are implemented differently on various devices, and can be especially problematic on mobiles. Watch a video about how some users struggle with select components .
Consider using radio buttons or checkboxes instead of a select dropdown.
How it works The select component allows users to choose an option from a list, usually more than 6 and fewer than 15 options.
Before deciding to use a select component, do some research with users to find out if fewer options are feasible. If you can reduce the list to 6 items or fewer, use radio buttons instead.
Credit Guidance, original HTML and CSS derived from GOV.UK Design System .
Get in touch If you’ve got a question, idea, or suggestion, email the Design System (DS) team at info@digital.govt.nz , use our NZGDS Slack app , or discuss Select on 'GitHub issues'.