Skip to content

Select

The Select component provides a customizable dropdown selection interface that enhances the standard HTML select element with advanced features like filtering, multiple selection, icons, option groups, and remote data loading.

  • Input Common component
  • Input component
  • Dropdown component
  • Tag component
  • Button component
  • Button Group component
<select data-role="select">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
<select data-role="select">
<option data-icon="<span class='mif-rocket'>" value="rocket">Rocket</option>
<option data-icon="<span class='mif-star-empty'>" value="star">Star</option>
<option data-icon="<img src='images/flag.svg' alt=''/>" value="flag" selected>Flag</option>
</select>
<select data-role="select" multiple data-clear-button="true">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
<select data-role="select" data-show-group-name="true">
<optgroup label="Group 1">
<option value="g1-option1">Group 1 Option 1</option>
<option value="g1-option2">Group 1 Option 2</option>
</optgroup>
<optgroup label="Group 2">
<option value="g2-option1">Group 2 Option 1</option>
<option value="g2-option2">Group 2 Option 2</option>
</optgroup>
</select>
<select data-role="select" data-filter="true" data-filter-placeholder="Search options...">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
<select data-role="select"
data-source="data/options.json"
data-source-method="GET">
</select>

The remote data should be in JSON format, structured as follows:

[
{"value": "1", "text": "Option 1"},
{"value": "2", "text": "Option 2", "icon": "<span class='mif-star'></span>"},
{"value": "3", "text": "Option 3", "icon": "<img src='images/icon.svg' alt=''/>"},
{"value": "3", "text": "Option 3"},
{"value": "4", "text": "Option 4", "selected": true}
]

You can transform the remote data using a callback function specified in the data-on-data attribute. This function should return an array of options in the format expected by the Select component.

<select data-role="select" id="select"
data-filter="true"
data-source="https://dummyjson.com/products"
data-filter-source="https://dummyjson.com/products/search?q="
data-on-data="updateData"></select>
<script>
function updateData(data){
const products = [...data.products]
const result = []
for (const p of products) {
result.push({
value: p.id,
text: p.title
})
}
return result
}
</script>
ParameterTypeDefaultDescription
idstring""Custom ID for the select element
labelstring""Label text for the select
sizestring”normal”Size of the select (small, medium, normal, large, largest)
selectDeferrednumber0Delay in milliseconds before processing selection
clearButtonbooleanfalseShow clear button to reset selection
clearButtonIconstring”❌“Icon for the clear button
usePlaceholderbooleanfalseUse placeholder text when no option is selected
placeholderstring""Placeholder text
addEmptyValuebooleanfalseAdd an empty option at the beginning
emptyValuestring""Value for the empty option
durationnumber0Animation duration for dropdown
prependstring""Text to prepend before the select
appendstring""Text to append after the select
filterPlaceholderstring""Placeholder text for filter input
filterbooleanfalseEnable filtering of options
dropHeightnumber200Maximum height of dropdown in pixels
dropWidthnumbernullWidth of dropdown (null for auto)
dropFullSizebooleanfalseMake dropdown full width
openModestring”auto”How dropdown opens (auto, up, down)
showGroupNamebooleanfalseShow group name in selected items
shortTagbooleantrueUse short tags (max width 120px) for multiple selection
sourcestringnullURL for remote data source
sourceMethodstring”GET”HTTP method for remote data
sourceTypestring”json”Data type for remote source (json, text)
filterSourcestringnullURL for filtered data source
filterThresholdnumber500Minimum characters before filtering
ParameterTypeDefaultDescription
clsSelectstring""Additional CSS class for select container
clsSelectInputstring""Additional CSS class for select input
clsPrependstring""Additional CSS class for prepend element
clsAppendstring""Additional CSS class for append element
clsOptionstring""Additional CSS class for options
clsOptionActivestring""Additional CSS class for active option
clsOptionGroupstring""Additional CSS class for option groups
clsDropListstring""Additional CSS class for dropdown list
clsDropContainerstring""Additional CSS class for dropdown container
clsSelectedItemstring""Additional CSS class for selected item tags
clsSelectedItemActionstring""Additional CSS class for selected item action button
clsLabelstring""Additional CSS class for label
clsGroupNamestring""Additional CSS class for group names
clsFilterInputstring""Additional CSS class for filter input
<select data-role="select"
data-clear-button="true"
data-filter="true"
data-filter-placeholder="Search..."
data-show-group-name="true"
data-cls-select="custom-select"
data-cls-option="custom-option">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
EventDescription
onClearFired when the select is cleared
onChangeFired when the selection changes
onUpFired when the dropdown closes
onDropFired when the dropdown opens
onOptionsFired when options are processed
onItemSelectFired when an item is selected
onItemDeselectFired when an item is deselected
onSelectCreateFired when the select component is created
onDataFunction to process data before rendering
onResetEvent calling when select was reseted
onOptionsEvent calling when options where created
<select data-role="select"
data-on-change="onSelectChange"
data-on-clear="onSelectClear">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
<script>
function onSelectChange(event) {
console.log('Selection changed:', event.detail);
}
function onSelectClear(event) {
console.log('Select cleared');
}
</script>
  • getSelected() - Returns an array of selected values.
  • val(val) - Gets or sets the selected value(s). If no parameter is provided, returns current selection.
  • data(options, selected, delimiter) - Sets the select options from data object or HTML string.
  • options(options, selected, delimiter) - Alias for the data method.
  • addOption(val, title, selected) - Adds a single option to the select.
  • addOptions(values) - Adds multiple options from array or object.
  • removeOption(val) - Removes an option by value.
  • removeOptions(values) - Removes multiple options by values array.
  • fetch(source, options, clearOptions) - Fetches data from remote source.
  • clear() - Clears the selected value(s).
  • reset() - Resets the select to its initial state.
  • destroy() - Destroys the component and removes all event listeners.
// Get the select component
const select = Metro.getPlugin('#mySelect', 'select');
// Set single value
select.val('option1');
// Set multiple values
select.val(['option1', 'option2']);
// Get selected values
const selected = select.getSelected();
// Add options from object
select.data({
'value1': 'Option 1',
'value2': 'Option 2'
});
// Add single option
select.addOption('new-value', 'New Option', true);
// Clear selection
select.clear();
// Reset to initial state
select.reset();
VariableDefault (Light)Dark ModeDescription
--select-focus-colorrgba(204,204,204,0.45)rgba(204,204,204,0.2)Focus outline color
--select-disabled-color#ccc#cccDisabled state color
--select-options-background#ffffff#2b2d30Dropdown background
--select-options-color#191919#dbdfe7Dropdown text color
--select-option-backgroundtransparenttransparentOption background
--select-option-colorinheritinheritOption text color
--select-option-background-hoverrgba(29,29,29,0.1)#43454aOption hover background
--select-option-color-hoverinherit#ffffffOption hover text color
--select-option-background-selected#e8e8e8#43454aSelected option background
--select-option-color-selected#191919#ffffffSelected option text color
--select-button-backgroundtransparenttransparentButton background
--select-button-color#191919#dbdfe7Button text color
--select-button-background-hovertransparenttransparentButton hover background
--select-button-color-hover#191919#ffffffButton hover text color
--select-dropdown-toggle-color#191919#ffffffDropdown arrow color
--select-group-title-background#f6f7f8#1e1f22Group title background
--select-group-title-color#646464#646464Group title text color
--select-item-group-title-backgroundtransparenttransparentSelected item group background
--select-item-group-title-color#ccc#646464Selected item group text color
--select-tag-background#f6f7f8#2b2d30Tag background for multiple select
--select-tag-color#191919#dbdfe7Tag text color
--select-tag-button-background#f6f7f8#2b2d30Tag button background
--select-tag-button-color#191919#dbdfe7Tag button text color
--select-border-radius4px4pxBorder radius
/* Custom select styling */
#mySelect {
--select-options-background: #f0f0f0;
--select-option-background-hover: #e0e0e0;
--select-border-radius: 8px;
}
/* Dark theme customization */
.dark-side #mySelect {
--select-options-background: #1a1a1a;
--select-option-background-hover: #333333;
}
  • .select - Main select container class
  • .select-input - Input display area
  • .option-list - Dropdown options list
  • .drop-container - Dropdown container
  • .small - Small size select
  • .medium - Medium size select
  • .large - Large size select
  • .largest - Largest size select
  • .focused - Applied when select is focused
  • .active-toggle - Applied when dropdown is open
  • .multiple - Applied to multiple select containers
  • .disabled - Applied when select is disabled
  • .drop-full-size - Makes dropdown full width
  • .no-icons - Hides option icons
  • .pill-input - Applies pill-shaped styling
  • .dropdown-caret - Dropdown arrow element
  • .group-title - Option group title
  • .tag - Selected item tag in multiple select
  • .unselect-option - Remove button for selected items
  • .input-clear-button - Clear button
  • .selected-item__group-name - Group name in selected items

When using remote data sources, the expected JSON format is:

[
{
"value": "option1",
"text": "Option 1",
"icon": "<span class='mif-star'>",
"selected": false
},
{
"value": "option2",
"text": "Option 2",
"selected": true
}
]

Options support several data attributes:

  • data-icon - HTML for option icon
  • data-template - Custom template with $1 placeholder for text
  • data-display - Custom display text different from option text

For multiple selection:

  • Use the multiple attribute on the select element
  • Selected items appear as removable tags
  • Use data-short-tag="true" to limit tag width to 120px
  • Always provide meaningful option values and text
  • Use option groups to organize large lists of options
  • Enable filtering for lists with more than 10-15 options
  • Provide clear labels and placeholders for better UX
  • Use icons consistently across all options or not at all
  • Test with both light and dark themes
  • Ensure proper keyboard navigation support
  • Use the clear button for optional selections
  • Handle loading states when using remote data sources
  • Validate selections on form submission