Select

Maintainer:
korvin89
GitHub
size
pin
placeholder
label
multiple
filterable
disabled
hasClear
hasCounter
import {Select} from '@gravity-ui/uikit';

Select is a control that provides a list of options that a user can select.

Options

Options to select.

Defining options

You can define options as an array of objects or as the children of a component. The first approach is useful for cases where options require complex preparation and, possibly, memorization. The second one is convenient when there are few options, and their configuration does not require complex calculations.

Flat list

Array of objects
Child nodes

Grouped list

Array of objects
Child nodes

Storing data in options

You can define and store unique data in each option by using the option.data property. This can be useful when you need to enrich the data when using the onUpdate callback or, for example, when drawing your options with renderOption.

Selecting multiple options

To enable multiple selection, use the multiple property. Its default value is false.

Counter

You can add a counter of the selected items to the component using the hasCounter property.

0

Filtering options

To enable filter section, use the filterable property. Its default value is false.

Size

To manage the default control and option size, use the size property. Its default size is m.

Control width

By default, the control width stretches to match the width of the content of the selected options. You can manage it by using the width property:

'max': Stretches to the full width of the parent.

number: Applies width in pixels.

Default

Max

In pixels

You can manage the popup width with the popupWidth property. The available values are:

'fit': Apply control width.

number: Apply width in pixels.

Points to note about the default behavior:

  • The popup width is equal to the width of the widest option, but not wider than 90vw. This does not apply in case you use virtualization.

  • Narrow options are stretched to fit the width of the control.

Default

Fit

In pixels

Virtualized list

For optimal display of a large number of options, Select has a built-in list virtualization tool. Virtualization is enabled after overcoming the threshold of the number of options (50 by default). You can manage this value using the virtualizationThreshold property.

When using virtualization, some restrictions apply to the popup element:

  • The popup width no longer gets adjusted to the length of the longest option.

  • The minimum width of the popup is equal to the width of the control, or 100px if the control is shorter.

Default

In pixels

Advanced usage

There are many ways to customize your Select.

Rendering custom control

To render a custom control, use the renderControl property. Note: You should forward all arguments to your node in order to enable consistent behavior, just as when using the default control.

Rendering custom filter section

To render a custom filter section, use the renderFilter property and set the filterable property to true. Note: You need to forward all arguments to your node in order to enable a properly working filter, just as when using the default configuration.

Rendering custom options

To render custom options, use the renderOption property:

Rendering custom selected options

To render custom selected options, use the renderSelectedOption property:

Rendering options with different heights

Options have a fixed height according to the size property. If you need to render options with different heights, you can use the option.data property. It will store information about what height you need to set for the options, as well as the getOptionHeight property to set this value.

Rendering custom counter

To render a custom counter, use the renderCounter property. The counter is only displayed when multiple selection is enabled (multiple={true}) and hasCounter={true}.

Rendering options list

The renderPopup property allows you to control the content of the options list: change the order of standard elements (filter, list), hide them, or add custom elements between, before, or after them.

Error

This Select state is for incorrect user input. To change the Select appearance, use the validationState property with the "invalid" value. Optionally, you can provide an error message through the errorMessage property. By default, the message text is rendered outside the component. You can change this with the errorPlacement property.

Error message

Properties

NameDescriptionTypeDefault
classNameControl classNamestring
defaultValueDefault values that represent selected options in case of using an uncontrolled statestring[]
disabledShows that the user cannot work with the controlbooleanfalse
filterableShows that select popup has a filter sectionbooleanfalse
filterOptionUsed to compare option with filterfunction
filterPlaceholderDefault filter input placeholder textstring
getOptionHeightUsed to set height of customized user optionsfunction
getOptionGroupHeightUsed to set height of customized user option groupfunction
hasClearEnables displaying icon for clearing selected optionsbooleanfalse
idid HTML attributestring
labelControl labelstring
loadingAdds the loading item to the end of the option list. Works like a persistent loading indicator while the options list is empty.boolean
multipleShows whether multiple options can be selected in the listbooleanfalse
nameName of the controlstring
onBlurHandler that is called when the element loses focus.function
filterControlled filter valuestring''
onFilterChangeFires every time after changing the filterfunction
onFocusHandler that is called when the element gets focusfunction
onLoadMoreFires when the loading indicator gets visiblefunction
onOpenChangeFires every time after changing popup visibilityfunction
onUpdateFires when an alteration to the Select value is committed by the userfunction
optionsOptions to select(SelectOption | SelectOptionGroup)[]
pinControl border viewstring'round-round'
placeholderPlaceholder textstring
popupClassNamePopup with the option list classNamestring
popupPlacementPopup placementPopupPlacement Array<PopupPlacement>['bottom-start', 'bottom-end', 'top-start', 'top-end']
popupWidthPopup widthnumber | 'fit' | 'outfit''outfit'
qaTest id attribute (data-qa)string
renderControlUsed to render user controlfunction
renderCounterUsed to render user counter. Works only with hasCounter.function
renderEmptyOptionsUsed to render a node for an empty option listfunction
renderFilterUsed to render user filter sectionfunction
renderOptionUsed to render user optionsfunction
renderOptionGroupUsed to render user option groupsfunction
renderSelectedOptionUsed to render user selected optionsfunction
renderPopupUsed to render options list contentfunction
sizeControl / options sizestring'm'
valueValues that represent selected optionsstring[]
viewControl viewstring'normal'
virtualizationThresholdOption count threshold after which virtualization is enablednumber50
widthControl widthstring | numberundefined
errorMessageError textstring
errorPlacementError positionoutside insideoutside
validationStateValidation state"invalid"
hasCounterShows the selected option count. The counter appears only when the multiple selection is enabled.boolean

CSS API

NameDescription
--g-select-focus-outline-colorOutline color if focused (missing by default)