Search documentation v0.9.0

Select

Choose an option from a predefined list, displayed in a dropdown

Source ↗️ Report Issue ↗️

The Select component is an extended List component. Any prop that the List component can accept, can also be used by the Select component. To better understand how this component works, we recommend reading through the documentation of the List component first.

Preview
  • Suggestions
  • Create issue
    (GitHub)
  • Knowledge base
    Learn more
  • Switch theme
  • Settings
  • Profile
  • Preferences
  • Sign out
How to use Selects in Astro
---
import { Select } from 'webcoreui/astro'
---
<Select
name="select-id"
itemGroups={[...]}
showSearchBar={true}
showSearchBarIcon={true}
searchBarPlaceholder="Filter options"
/>
<script>
import { listen } from 'webcoreui'
listen('selectOnChange', event => console.log(event))
listen('selectOnClose', event => console.log(event))
</script>
How to use Selects in Svelte
<script>
import { Select } from 'webcoreui/svelte'
</script>
<Select
name="select-id"
itemGroups={[...]}
showSearchBar={true}
showSearchBarIcon={true}
searchBarPlaceholder="Filter options"
onChange={event => console.log(event)}
onClose={event => console.log(event)}
/>
How to use Selects in React
import React from 'react'
import { Select } from 'webcoreui/react'
const Component = () => (
<Select
name="select-id"
itemGroups={[...]}
showSearchBar={true}
showSearchBarIcon={true}
searchBarPlaceholder="Filter options"
onChange={event => console.log(event)}
onClose={event => console.log(event)}
/>
)
export default Component
You can see the exact value of the itemGroups prop used in the above example in this section.

To handle select events, listen to the selectOnChange event in Astro, or use the onChange prop for Svelte and React components. The event object returns the following properties:

Event object
{
select: 'select-id' // The name prop of the Select component
value: 'new', // Value of the clicked item
name: 'Create issue', // Name of the clicked item
list: ul._list_1rduk_1 // HTMLUListElement - the ul which the item belongs to
}

You can also listen to the selectOnClose event in Astro, or use the onClose prop for svelte and React components to determine when the select is closed without a selection, for example when it loses focus or closed with the Esc key.

States

The select event is only triggered for active Select components. To disable a Select component, set the disabled prop to true:

Disabled Select
  • Suggestions
  • Create issue
    (GitHub)
  • Knowledge base
    Learn more
  • Switch theme
  • Settings
  • Profile
  • Preferences
  • Sign out
<Select
name="select-id"
itemGroups={[...]}
disabled={true}
showSearchBar={true}
showSearchBarIcon={true}
searchBarPlaceholder="Filter options"
/>

For default values, you can use the value prop. You can either set an arbitrary value, or use one of the values from your itemsGroups prop:

Arbitrary value
  • Suggestions
  • Create issue
    (GitHub)
  • Knowledge base
    Learn more
  • Switch theme
  • Settings
  • Profile
  • Preferences
  • Sign out
Value from itemGroups
  • Suggestions
  • Create issue
    (GitHub)
  • Knowledge base
    Learn more
  • Switch theme
  • Settings
  • Profile
  • Preferences
  • Sign out
<Select
name="select-id"
value="Arbitrary value"
itemGroups={[...]}
updateValue={false}
showSearchBar={true}
showSearchBarIcon={true}
searchBarPlaceholder="Filter options"
/>
<Select
name="select-id"
value="new"
itemGroups={[{
title: 'Suggestions',
items: [
{
name: 'Create issue',
value: 'new',
...
},
{ ... },
{ ... }
]
}, { ... }]}
updateValue={false}
showSearchBar={true}
showSearchBarIcon={true}
searchBarPlaceholder="Filter options"
/>

If you’d like to use the Select component as a command list or as a combobox, you can disable automatic value update to keep its placeholder/value intact using the updateValue prop:

  • Suggestions
  • Create issue
    (GitHub)
  • Knowledge base
    Learn more
  • Switch theme
  • Settings
  • Profile
  • Preferences
  • Sign out
<Select
name="select-id"
itemGroups={[...]}
value="Take an action"
updateValue={false}
showSearchBar={true}
showSearchBarIcon={true}
searchBarPlaceholder="Filter options"
/>

Labels

The Select component can be labelled in three different ways using the following props:

  • Suggestions
  • Create issue
    (GitHub)
  • Knowledge base
    Learn more
  • Switch theme
  • Settings
  • Profile
  • Preferences
  • Sign out
  • Suggestions
  • Create issue
    (GitHub)
  • Knowledge base
    Learn more
  • Switch theme
  • Settings
  • Profile
  • Preferences
  • Sign out
  • Suggestions
  • Create issue
    (GitHub)
  • Knowledge base
    Learn more
  • Switch theme
  • Settings
  • Profile
  • Preferences
  • Sign out
<Select
name="select-id"
label="Commonly used actions"
itemGroups={[...]}
showSearchBar={true}
showSearchBarIcon={true}
searchBarPlaceholder="Filter options"
/>
<Select
name="select-id"
placeholder="Commonly used actions"
itemGroups={[...]}
showSearchBar={true}
showSearchBarIcon={true}
searchBarPlaceholder="Filter options"
/>
<Select
name="select-id"
subText="Take an action or jump to a page"
itemGroups={[...]}
showSearchBar={true}
showSearchBarIcon={true}
searchBarPlaceholder="Filter options"
/>

Positioning

By default, options are displayed under the Select component. These options can be repositioned using the position prop. Please see the API documentation below for all possible position values. The following three Select components demonstrate the left, right, and modal positions.

  • Suggestions
  • Create issue
    (GitHub)
  • Knowledge base
    Learn more
  • Switch theme
  • Settings
  • Profile
  • Preferences
  • Sign out
  • Suggestions
  • Create issue
    (GitHub)
  • Knowledge base
    Learn more
  • Switch theme
  • Settings
  • Profile
  • Preferences
  • Sign out
  • Suggestions
  • Create issue
    (GitHub)
  • Knowledge base
    Learn more
  • Switch theme
  • Settings
  • Profile
  • Preferences
  • Sign out
Automatic repositioning during filtering with custom positions is currently not supported.

API

type SelectProps = {
name: string
value?: string
placeholder?: string
label?: string
subText?: string
disabled?: boolean
updateValue?: boolean
position?: 'top'
| 'bottom'
| 'left'
| 'left-start'
| 'left-end'
| 'right'
| 'right-start'
| 'right-end'
| 'modal'
} & ListProps
type SvelteSelectProps = {
onChange?: (event: ListEventType & { select: string }) => void
onClose?: (event: ModalCallback | PopoverCallback) => void
} & SelectProps
type ReactSelectProps = {
onChange?: (event: ListEventType & { select: string }) => void
onClose?: (event: ModalCallback | PopoverCallback) => void
} & SelectProps
For the ListProps and ListEventType types, please see the API documentation of the List component. For ModalCallback and PopoverCallback, please see the Modal/Popover sections in the utilities documentation.
PropPurpose
name Sets a unique name for the select that.
value Sets a default value for the select.
placeholder Sets a placeholder for the select.
label Sets a label for the select that appears above the input.
label Sets a label for the select that appears below the input.
disabled Disables the select.
updateValue Sets whether the value of the select should be updated on interaction. Defaults to true.
position Sets the position of the options, relative to the select. Defaults to bottom.
Astro EventsPurpose
selectOnChange Triggered when an option is selected.
selectOnClose Triggered when the select is closed without a selection.
Svelte & React PropPurpose
onChange Attach an on-change event listener that is triggered when an option is selected.
onClose Attach a close event listener that is triggered when the select is closed without a selection.

Request improvement for this page