|
| 1 | +--- |
| 2 | +layout: component |
| 3 | +--- |
| 4 | + |
| 5 | +# Select |
| 6 | + |
| 7 | +The select component allows users to choose an option from a list. |
| 8 | + |
| 9 | +See the [GOV.UK Design System documentation on selects](https://design-system.service.gov.uk/components/select/) |
| 10 | +for more information on when to use this component. |
| 11 | + |
| 12 | +```vue-html |
| 13 | +<gv-select label="Sort by"> |
| 14 | + <gv-select-option value="published"> |
| 15 | + Recently published |
| 16 | + </gv-select-option> |
| 17 | + <gv-select-option value="updated"> |
| 18 | + Recently updated |
| 19 | + </gv-select-option> |
| 20 | + <gv-select-option value="views"> |
| 21 | + Most views |
| 22 | + </gv-select-option> |
| 23 | + <gv-select-option value="comments"> |
| 24 | + Most comments |
| 25 | + </gv-select-option> |
| 26 | +</gv-select> |
| 27 | +``` |
| 28 | + |
| 29 | +## Binding with `v-model` and using events |
| 30 | + |
| 31 | +The select component supports the same `v-model` binding behaviour as `<select>`. |
| 32 | + |
| 33 | +Each option needs a `value`. The value of `v-model` will be set to the `value` of the selected option. |
| 34 | + |
| 35 | +`value`s can be simple types, like strings or numbers, or more complex objects. |
| 36 | + |
| 37 | +You can also attach event listeners and they'll be bound to the underlying `<select>`. |
| 38 | + |
| 39 | +```vue |
| 40 | +<script setup lang="ts"> |
| 41 | +import { ref } from 'vue' |
| 42 | +
|
| 43 | +const sortOptions = [ |
| 44 | + { label: 'Price, high to low', property: 'price', direction: 'descending' }, |
| 45 | + { label: 'Price, low to high', property: 'price', direction: 'ascending' }, |
| 46 | +] |
| 47 | +
|
| 48 | +const selectedSort = ref(null) |
| 49 | +
|
| 50 | +function handleChange() { |
| 51 | + alert('You changed the sort value') |
| 52 | +} |
| 53 | +</script> |
| 54 | +
|
| 55 | +<template> |
| 56 | + <gv-select |
| 57 | + v-model="selectedSort" |
| 58 | + label="Sort by" |
| 59 | + @change="handleChange" |
| 60 | + > |
| 61 | + <gv-select-option |
| 62 | + v-for="sortOption in sortOptions" |
| 63 | + :key="sortOptions.label" |
| 64 | + :value="sortOption" |
| 65 | + > |
| 66 | + {{ sortOption.label }} |
| 67 | + </gv-select-option> |
| 68 | + </gv-select> |
| 69 | + |
| 70 | + <gv-inset-text v-if="selectedSort" aria-live="polite"> |
| 71 | + Sorting by {{ selectedSort.label.toLowerCase() }} |
| 72 | + </gv-inset-text> |
| 73 | +</template> |
| 74 | +``` |
| 75 | + |
| 76 | +## Select with hint |
| 77 | + |
| 78 | +You can add a hint using the `hint` prop or slot. |
| 79 | + |
| 80 | +```vue-html |
| 81 | +<gv-select |
| 82 | + label="Choose location" |
| 83 | + hint="This can be different to where you went before" |
| 84 | + > |
| 85 | + <gv-select-option value="eastmidlands">East Midlands</gv-select-option> |
| 86 | + <gv-select-option value="eastofengland">East of England</gv-select-option> |
| 87 | + <gv-select-option value="london">London</gv-select-option> |
| 88 | + <gv-select-option value="northeast">North East</gv-select-option> |
| 89 | + <gv-select-option value="northwest">North West</gv-select-option> |
| 90 | + <gv-select-option value="southeast">South East</gv-select-option> |
| 91 | + <gv-select-option value="southwest">South West</gv-select-option> |
| 92 | + <gv-select-option value="westmidlands">West Midlands</gv-select-option> |
| 93 | + <gv-select-option value="yorkshire">Yorkshire and the Humber</gv-select-option> |
| 94 | +</gv-select> |
| 95 | +``` |
| 96 | + |
| 97 | +## Error messages |
| 98 | + |
| 99 | +Use the `error-message` prop or slot to show an error message if the user has not selected an option for a mandatory select. |
| 100 | + |
| 101 | +```vue-html |
| 102 | +<gv-select |
| 103 | + label="Choose location" |
| 104 | + hint="This can be different to where you went before" |
| 105 | + error-message="Select a location" |
| 106 | + > |
| 107 | + <gv-select-option value="eastmidlands">East Midlands</gv-select-option> |
| 108 | + <gv-select-option value="eastofengland">East of England</gv-select-option> |
| 109 | + <gv-select-option value="london">London</gv-select-option> |
| 110 | + <gv-select-option value="northeast">North East</gv-select-option> |
| 111 | + <gv-select-option value="northwest">North West</gv-select-option> |
| 112 | + <gv-select-option value="southeast">South East</gv-select-option> |
| 113 | + <gv-select-option value="southwest">South West</gv-select-option> |
| 114 | + <gv-select-option value="westmidlands">West Midlands</gv-select-option> |
| 115 | + <gv-select-option value="yorkshire">Yorkshire and the Humber</gv-select-option> |
| 116 | +</gv-select> |
| 117 | +``` |
| 118 | + |
| 119 | +::gvd-options{component="Select" :showName=true} |
| 120 | +:: |
| 121 | + |
| 122 | +::gvd-options{component="SelectOption" :showName=true} |
| 123 | +:: |
0 commit comments