Select
The select and search controls allow the user to select an option or options.
Select menus in Base hover atop of a selection menu while providing a simple list of options. Proper spacing, color, and a check-mark are clear indicators of a selection. Each menu is scrollable, if applicable.
Note: by default, id
and label
are default properties that Select components look for in options array objects. To adapt to custom properties, use the labelKey
and valueKey
to override the defaults.
Examples
Things to note in the example source code:
- the
value
is always anArray
to provide a consistent interface - no matter if you use multi or single selects, - you have to call
setState
with the entire object, not just theid
value.
Things to note:
- keys should be unique b/w group options
You can use isLoading
, width
, closeMenu
props when defining dropdownFooter component
When type is 'search' a magnifying glass icon will be rendered to the input's left and a dropdown arrow will not be rendered to the right.
The creatable select enables users to create new options along with choosing existing options.
When type is 'overlay', select renders in overlay. Child element works as target element for overlay select.
Virtualization is enabled in Select by default. Select uses react-virtual.
To override the Popover
that's being used by the Dropdown subcomponent, you have to use a nested override.
Setting clearAsOption
will render an additional option in the popover whenever a value is selected. This additional option can be used to clear the selected value. Whenever clearAsOption
is set, ClearIcon will not be rendered irrespective of the value of clearable
prop. By default the label for this clear option will be Unassign, a custom label can be set using clearAsOptionLabel
prop.
API
Select props
Name | Type | Default | Description |
---|---|---|---|
aria-describedby | string | Sets the aria-describedby attribute. | |
aria-label | string | Sets the aria-label attribute. | |
aria-labelledby | string | Sets the aria-describedby attribute. | |
autoFocus | boolean | Defines if select element is focused on the first mount. | |
backspaceClearsInputValue | boolean | false | By default, backspace will only remove the last character of the input value. If true, the input value will be cleared. |
backspaceRemoves | boolean | true | Defines if options can be removed by pressing backspace. |
captureScroll | boolean | false | When the user reaches top/bottom of the menu, prevents scroll to the body/scroll-parent |
children | react node | When type is 'overlay', Select is opened in overlay and children works as target element for the popover. For all other types, children is simply ignored. | |
className | string | Enriched style to be provided to Select Root | |
clearAsOption | boolean | false | Add option to clear selected value in the popover instead of clearable icon. |
clearAsOptionLabel | string | Unassign | Defines the label for the option which clears the selected values. |
clearable | boolean | true | Defines if the select value can be cleared. If true a clear icon is rendered when a value is set. |
closeOnSelect | boolean | true | Defines if the menu closes after a selection if made. |
controlRef | ref | Exposes methods to control select's dropdown and other internal state. Check types and examples for more information. | |
creatable | boolean | Defines if new options can be created along with choosing existing options. | |
createOption | function | A custom handler that returns options to be created from the input value (label) entered by the user. It should return Options or Promise<Options>. | |
deleteRemoves | boolean | true | Defines if options can be removed by pressing backspace. |
delimiter | string | If defined, select will create option and call onChange whenever delimeter is encountered when input changes. | |
disabled | boolean | Defines if the control is disabled. | |
dropdownFooter | function | Add a footer for dropdown menu | |
escapeClearsValue | boolean | true | Defines if the value is cleared when escape is pressed and the dropdown is closed. |
filterOptions | function | A custom method to filter options to be displayed in the dropdown. | |
filterOutSelected | boolean | true | Defines if currently selected options are filtered out in the dropdown options. Only for `multi` select. |
getOptionLabel | function | A custom method to get a display value for a dropdown option. | |
getValueLabel | function | A custom method to get a display value for a selected option. | |
hideSelectAll | boolean | false | Hides select all option for multi select box. |
id | string | Sets the id attribute of the internal input element. Allows for usage with labels. | |
ignoreBoundary | boolean | If true, popover element will overflow outside anchor's scrollParent. | |
inputRef | ref | A ref to access an input element. | |
intent | 'default' | 'success' | 'warning' | 'error' | Defines the intent of the Select. | |
isLoading | boolean | Defines if the select is in a loading (async) state. | |
label | react node | Sets label of Select when type is 'mobile' | |
labelKey | string | Defines an option key name for a default label value. | |
limitTags | number | Number of tags which should be rendered rest of the tag will be shown as a popover | |
maxDropdownHeight | string | Sets max height of the dropdown list. | |
mountNode | ref | Where to mount the popover. | |
multi | boolean | Defines if multiple options can be selected. | |
noResultsMsg | react node | Message to be displayed if no options is found for a search query. | |
noTooltipOnTruncation | boolean | false | By default, tooltip will be visible when content gets truncated |
onBlur | function | Called the onBlur event triggers. | |
onBlurResetsInput | boolean | true | Defines if the input value is reset to an empty string when a blur event happens on the select. |
onChange | function | Change handler of the select to be called when a value is changed. | |
onClose | function | A function that is called when the dropdown closes. | |
onCloseResetsInput | boolean | true | Defines if the input value is reset to an empty string when dropdown is closed. |
onFocus | function | Called the onFocus event triggers. | |
onInputChange | function | Change handler of the search input. | |
onOpen | function | A function that is called when the dropdown opens. | |
onPaste | function | A function that is called when a paste event occurs on the select's input. It receives a single argument of the clipboard event and expects options to be returned.return values from onPaste won't trigger onChange if creatable is false | |
onSelectResetsInput | boolean | true | Defines if the input value is reset to an empty string when a selection is made. |
openOnClick | boolean | true | Defines if the dropdown opens on a click event on the select. |
options | array | Options to be displayed in the dropdown. If an option has a disabled prop value set to true it will be rendered as a disabled option in the dropdown. | |
overlayWidth | string | Sets width of select when type is 'overlay'. Default is '300px'. | |
overrides | custom | Lets you customize all aspects of the component. | |
placeholder | string | Sets the placeholder. | |
placement | 'auto' | 'topLeft' | 'top' | 'topRight' | 'rightTop' | 'right' | 'rightBottom' | 'bottomRight' | 'bottom' | 'bottomLeft' | 'leftTop' | 'left' | 'leftBottom' | Controls how to position the dropdown relative to the select. Defaults to `bottom` | |
popoverMinWidth | string | Sets the minWidth of the dropdown container. | |
required | boolean | Defines if the select field is required to have a selection. | |
searchable | boolean | true | Defines if the search functionality is enabled. |
selectOnTab | boolean | false | When enabled allows to click on/select highlighted item on Tab key press. |
size | 'xs' | 'sm' | 'md' | Input's size. SPACIOUS | COMFORTABLE -> md, COMPACT -> xxxs, DEFAULT -> md | |
startOpen | boolean | If true, opens the dropdown when the select mounts. | |
style | string | Enriched style to be provided to Select Root | |
type | 'search' | 'select' | 'overlay' | 'mobile' | Defines type of the component to be in a select or search mode. When set to 'search' the search icon is rendered on the left and the select arrow icon is not rendered. When set to 'overlay' the select is rendered in overlay. | |
value | array | A current selected value(s). If a selected value has a clearableValue prop set to false it will be rendered as a disabled selected option that can't be cleared. | |
valueKey | string | Defines a key name for an option's unique identifier value. The value of the 'valueKey' prop is used to identify what options are selected or removed from the selection, it also used for default filtering out the selected options from the dropdown list. | |
virtual | boolean | true | Add virtualization to select options |
You can check the implementation of the component for more details.
Select exports
You can import this module like so:
import {CLEARABLE_KEY} from 'spaceweb/select'
It exports the following components or utility functions:
- CLEARABLE_KEY
- CLEAR_OPTION_ID
- CollapsibleSelectMenu
- GroupedSelect
- LOADING_KEY
- MobileSheet
- MultiSelect
- MultiValue
- NoResults
- SELECT_ALL_ID
- STATE_CHANGE_TYPE
- Select
- SelectMenuAdapter
- SingleSelect
- SingleValue
- StatefulSelect
- StyledClearIcon
- StyledControlContainer
- StyledDropdownContainer
- StyledIconsContainer
- StyledInput
- StyledInputContainer
- StyledInputSizer
- StyledOverlayContentContainer
- StyledPlaceholder
- StyledRoot
- StyledSearchIconContainer
- StyledSelectArrow
- StyledSingleValue
- StyledValueContainer
- UNGROUPED_ID
- defaultFilterOptions