Edit page

SelectField

SelectField allows users to select one option from a set.

Basic Usage

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.

General Guidelines

  • 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.

Design Variants

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.

Sizes

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).

Invisible Label

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.

Horizontal Layout

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.

Help Text

You may provide an additional help text to clarify how the input should be filled.

States

Validation States

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.

Disabled State

It's possible to disable just some options or the whole input.

Forwarding HTML Attributes

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.

Forwarding ref

If you provide ref, it is forwarded to the native HTML <select> element.

API

Prop nameTypeDefaultRequiredDescription
disabledboolfalsefalse

If true, the input will be disabled.

fullWidthboolfalsefalse

If true, the field will span the full width of its parent.

helpTextnodenullfalse

Optional help text.

idstringundefinedfalse

ID of the input HTML element.

Also serves as a prefix for important inner elements:

  • <ID>__label
  • <ID>__labelText,
  • <ID>__helpText
  • <ID>__validationText

and of individual options:

  • <ID>__item__<VALUE>

If key in the option definition object is set, then option.key is used instead of option.value in place of <VALUE>.

isLabelVisiblebooltruefalse

If false, the label will be visually hidden (but remains accessible by assistive technologies).

labelstringtrue

Select field label.

layout'horizontal' │ 'vertical''vertical'false

Layout of the field.

Ignored if the component is rendered within FormLayout component as the value is inherited in such case.

optionsArray<{ "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 option.value is normally used. For cases when this is not practical or the option.value values are not unique the option.key attribute can be set manually. The same applies for the label value of grouped options which is supposed to be unique. To ensure uniqueness key attribute can be set manually.

requiredboolfalsefalse

If true, the input will be required.

size'small' │ 'medium' │ 'large''medium'false

Size of the field.

validationState'invalid' │ 'valid' │ 'warning'nullfalse

Alter the field to provide feedback based on validation result.

validationTextnodenullfalse

Validation message to be displayed.

variant'filled' │ 'outline''outline'false

Design variant of the field, further customizable with CSS custom properties.

Theming

Head to Forms Theming to see shared form theming options. On top of that, the following options are available for SelectField.

Custom PropertyDescription
--rui-FormField--box--select__caret__border-styleSelectField arrow border style (e.g. solid)
--rui-FormField--box--select__caret__backgroundSelectField arrow background (including url() or gradient)
--rui-FormField--box--select__option--disabled__colorText color of disabled SelectField options