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.
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.
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.
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.
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.
To render custom options, use the renderOption property:
importtype{SelectProps}from'@gravity-ui/uikit';import{Select}from'@gravity-ui/uikit';const renderOption:SelectProps['renderOption']=(option)=>{return<divstyle={{color: option.data.color}}>{option.children}</div>;};exportdefaultfunction(){return(<Selectplaceholder="Custom options"renderOption={renderOption}><Select.Optionvalue="val_1"data={{color:'#8FE1A1'}}> Value 1</Select.Option><Select.Optionvalue="val_2"data={{color:'#38C0A8'}}> Value 2</Select.Option><Select.Optionvalue="val_3"data={{color:'#3A7AC3'}}> Value 3</Select.Option><Select.Optionvalue="val_4"data={{color:'#534581'}}> Value 4</Select.Option></Select>);}
To render custom selected options, use the renderSelectedOption property:
importtype{SelectProps}from'@gravity-ui/uikit';import{Select}from'@gravity-ui/uikit';const renderSelectedOption:SelectProps['renderSelectedOption']=(option)=>{return<divstyle={{color: option.data.color}}>{option.children}</div>;};exportdefaultfunction(){return(<Selectplaceholder="Custom selected options"renderSelectedOption={renderSelectedOption}><Select.Optionvalue="val_1"data={{color:'#8FE1A1'}}> Value 1</Select.Option><Select.Optionvalue="val_2"data={{color:'#38C0A8'}}> Value 2</Select.Option><Select.Optionvalue="val_3"data={{color:'#3A7AC3'}}> Value 3</Select.Option><Select.Optionvalue="val_4"data={{color:'#534581'}}> Value 4</Select.Option></Select>);}
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.
importtype{SelectProps}from'@gravity-ui/uikit';import{Select}from'@gravity-ui/uikit';const getOptionHeight:SelectProps['getOptionHeight']=(option)=> option.data.height;exportdefaultfunction(){return(<Selectplaceholder="Different heights"getOptionHeight={getOptionHeight}><Select.Optionvalue="val_1"data={{height:20}}> Value 1</Select.Option><Select.Optionvalue="val_2"data={{height:40}}> Value 2</Select.Option><Select.Optionvalue="val_3"data={{height:60}}> Value 3</Select.Option><Select.Optionvalue="val_4"data={{height:80}}> Value 4</Select.Option></Select>);}
To render a custom counter, use the renderCounter property. The counter is only displayed when multiple selection is enabled (multiple={true}) and hasCounter={true}.
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.
importtype{SelectProps}from'@gravity-ui/uikit';import{Select}from'@gravity-ui/uikit';const renderPopup:SelectProps['renderPopup']=({renderList, renderFilter})=>{return(<>{renderFilter()}<divstyle={{width:'100%', height:20, backgroundColor:'tomato'}}/>{renderList()}</>);};exportdefaultfunction(){return(<Selectfilterableplaceholder="Custom popup"renderPopup={renderPopup}><Select.Optionvalue="val_1"data={{color:'#8FE1A1'}}> Value 1</Select.Option><Select.Optionvalue="val_2"data={{color:'#38C0A8'}}> Value 2</Select.Option><Select.Optionvalue="val_3"data={{color:'#3A7AC3'}}> Value 3</Select.Option><Select.Optionvalue="val_4"data={{color:'#534581'}}> Value 4</Select.Option></Select>);}
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.