SelectField allows users to select one option from a set.
To implement the SelectField component, you need to import it first:
import { SelectField } from '@react-ui-org/react-ui';
And use it:
See API for all available options.
Use SelectField for many options. For sets of just a few options (say 3 at maximum) consider using the Radio component. This will help keep your UI clean and uncluttered and prevent your users from being overwhelmed by too many options.
Don't use for boolean (true/false) selection or to toggle things on and off. CheckboxField and Toggle are more suitable for such cases.
Use short and descriptive labels, ideally nouns rather than seemingly polite phrases like Please select your favourite fruit. Short labels will help your users accomplish their task faster.
Only make the SelectField's label invisible when there is another visual clue to guide users through filling the input. Using the first option as label is not recommended either — it disappears once user makes their choice.
When a short label is not enough, use help texts to guide users before they enter anything.
Use clear, calm error messages when there's a problem with what they entered.
In case of a large amount of options, consider grouping related items together by nesting them.
👉 We use the native select
HTML element to improve user experience on
mobile devices by using the native select of the platform.
To satisfy the design requirements of your project, all input fields in React UI come in two design variants to choose from: outline and filled. Both can be further customized with CSS custom properties.
Aside from the default (medium) size, two additional sizes are available: small and large.
Full-width fields span the full width of a parent:
For a large amount of options you can group related items together by nesting
them (implements the optgroup
HTML element).
While it may be acceptable for login screens with just a few fields or for other simple forms, it's dangerous to hide labels from users in most cases. Keep in mind you should provide another visual clue so users know what to fill into the input.
The default vertical layout is very easy to use and work with. However, there are situations where horizontal layout suits better — and that's why React UI supports this kind of layout as well.
You may provide an additional help text to clarify how the input should be filled.
Validation states visually present the result of validation of the input. You should always provide a validation message for states other than valid so users know what happened and what action they should take or what options they have.
It's possible to disable just some options or the whole input.
In addition to the options below in the component's API section, you
can specify React synthetic events or you can add whatever HTML attribute
you like. All attributes that don't interfere with the API are forwarded to
the native HTML <select>
. This enables making the component interactive and helps
to improve its accessibility.
👉 Refer to the MDN reference for the full list of supported attributes of the select element.
If you provide ref, it is forwarded to the native HTML <select>
element.
Prop name | Type | Default | Required | Description |
---|---|---|---|---|
disabled | bool | false | false | If |
fullWidth | bool | false | false | If |
helpText | node | null | false | Optional help text. |
id | string | undefined | false | ID of the input HTML element. Also serves as a prefix for important inner elements:
and of individual options:
If |
isLabelVisible | bool | true | false | If |
label | string | — | true | Select field label. |
layout | 'horizontal' │ 'vertical' | 'vertical' | false | Layout of the field. Ignored if the component is rendered within |
options | Array<{
"key": "string",
"label": "string",
"options": "Array<{\n \"disabled\": \"Boolean\",\n \"key\": \"string\",\n \"label\": \"string\",\n \"value\": \"string │ number\"\n}>"
}> │ Array<{
"disabled": "Boolean",
"key": "string",
"label": "string",
"value": "string │ number"
}> | — | true | Set of options to be chosen from. Either set of individual or grouped options is acceptable. For generating unique IDs the |
required | bool | false | false | If |
size | 'small' │ 'medium' │ 'large' | 'medium' | false | Size of the field. |
validationState | 'invalid' │ 'valid' │ 'warning' | null | false | Alter the field to provide feedback based on validation result. |
validationText | node | null | false | Validation message to be displayed. |
variant | 'filled' │ 'outline' | 'outline' | false | Design variant of the field, further customizable with CSS custom properties. |
Head to Forms Theming to see shared form theming options. On top of that, the following options are available for SelectField.
Custom Property | Description |
---|---|
--rui-FormField--box--select__caret__border-style | SelectField arrow border style (e.g. solid ) |
--rui-FormField--box--select__caret__background | SelectField arrow background (including url() or gradient) |
--rui-FormField--box--select__option--disabled__color | Text color of disabled SelectField options |