Select
- How
- A native
<select class=“sdc-select”>.Select.astrowrites the element, its options and, whenerroris set, the message under it. The label and any hint come from the.sdc-fieldwrapper markup that Field uses, written around it by hand, becauseField.astrorenders its own input and cannot hold a Select. - When
- For one answer out of many, past the point where Choice chips stop being scannable, roughly a dozen.
- Where
- In a form, inside a
.sdc-fieldwrapper with a label.
Why the native element
Section titled “Why the native element”The platform picker is better than anything we would build: it is the control the visitor already knows, it works with a keyboard, it works with a screen reader, and on a phone it opens the system wheel rather than a list that scrolls inside a scrolling page. A custom dropdown has to re-earn all of that and usually does not.
So only the chrome is ours: the same height, border, radius and background as a text input, because the two sit next to each other and a mismatch reads as a mistake. The extra inline-end padding leaves room for the platform’s own arrow.
In code
Section titled “In code”src/components/ui/Select.astro.
---import Select from '../../components/ui/Select.astro';const options = [ { value: '', label: 'Pick one' }, { value: 'referral', label: 'Someone referred me' }, { value: 'search', label: 'I searched for it' },];---<div class="sdc-field"> <label class="sdc-field__label" for="heard">How did you hear about me?</label> <Select id="heard" name="heard" options={options} /></div>The label’s for matches the Select’s id, which is what gives the control its
name. To show an error, pass error, the same as on Field:
<Select id="heard" name="heard" options={options} error="Pick how you heard about me." />That draws the error border, writes the message under the control, and sets
aria-invalid and aria-describedby, so the failure is announced and not carried
by color alone.
| Prop | Type | Default | What it does |
|---|---|---|---|
id |
string | required | Ties the label’s for to this control |
name |
string | none | The form field name |
options |
{ value, label }[] |
required | In display order. A value of '' becomes a disabled placeholder row, and with no value given the control starts on it |
value |
string | none | Which option starts selected |
required |
boolean | false |
Marks it required |
disabled |
boolean | false |
Greys it out |
error |
string | none | The validation message. Present means invalid, as on Field |
invalid |
boolean | false |
The 02.03 way to mark it invalid, with no message. Still works; use error. Goes away in 03.00.00 |
describedBy |
string | none | The id of a hint describing it. An error is added after it |
Checks before you ship
Section titled “Checks before you ship”- It has a visible label whose
formatches the Select’sid. - The placeholder row is disabled, so it cannot be submitted as an answer.
- It is not being used for fewer than about four options.