Skip to content

Input

The Input component is a versatile and feature-rich text input field that extends the standard HTML input element with additional functionality including autocomplete, history, custom buttons, prepend/append options, and various styling options.

The Input component requires the following dependencies:

  • input-common - Common input functionality
  • button - Button component for action buttons
  • select - Select component for prepend/append options
<!-- Simple input -->
<input type="text" data-role="input" data-label="Enter text:">
<!-- Input with placeholder -->
<input type="text" data-role="input" data-label="Username:" placeholder="Enter username">
<!-- Input with clear button -->
<input type="text" data-role="input" data-clear-button="true" data-label="Search:">
<!-- Password input with reveal button -->
<input type="password" data-role="input" data-reveal-button="true" data-label="Password:">
<!-- Input with search button -->
<input type="text" data-role="input" data-search-button="true" data-label="Search:">
<!-- Input with random generator button -->
<input type="text" data-role="input" data-random-button="true" data-label="Generate:">
<!-- Static autocomplete list -->
<input type="text" data-role="input"
data-autocomplete="apple,banana,cherry,date,elderberry"
data-label="Fruit:">
<!-- Autocomplete from URL -->
<input type="text" data-role="input"
data-autocomplete-url="/api/suggestions"
data-autocomplete-url-method="GET"
data-label="Search:">
<!-- Input with history support -->
<input type="text" data-role="input"
data-history="true"
data-history-preset="previous search|another search|third search"
data-label="Search with history:">
<!-- Input with prepend text -->
<input type="text" data-role="input"
data-prepend="$"
data-label="Price:">
<!-- Input with append text -->
<input type="text" data-role="input"
data-append=".00"
data-label="Amount:">
<!-- Input with prepend/append options -->
<input type="text" data-role="input"
data-prepend-options="http://,https://,ftp://"
data-append-options=".com,.org,.net"
data-label="URL:">
<input type="text" data-role="input"
data-custom-buttons='[{"text":"📋","onclick":"copyToClipboard","cls":"copy-btn"}]'
data-label="Text to copy:">
ParameterTypeDefaultDescription
inputDeferrednumber0Delay in milliseconds before initializing the component
labelstring""Label text for the input
autocompletestring/arraynullStatic autocomplete data (comma-separated string or array)
autocompleteUrlstringnullURL to fetch autocomplete data
autocompleteUrlMethodstring”GET”HTTP method for autocomplete URL
autocompleteUrlKeystringnullKey to extract data from JSON response
autocompleteDividerstring”,“Divider for autocomplete string data
autocompleteListHeightnumber200Maximum height of autocomplete dropdown
historybooleanfalseEnable input history functionality
historyPresetstring""Preset history items (pipe-separated)
historyDividerstring”|“Divider for history preset string
preventSubmitbooleanfalsePrevent form submission on Enter key
defaultValuestring""Default value for the input
sizestring”default”Custom width for the input
prependstring""Text to prepend to the input
appendstring""Text to append to the input
prependOptionsstring""Dropdown options to prepend (comma-separated)
prependOptionsSepstring”,“Separator for prepend options
appendOptionsstring""Dropdown options to append (comma-separated)
appendOptionsSepstring”,“Separator for append options
searchButtonbooleanfalseShow search button
clearButtonbooleantrueShow clear button
revealButtonbooleantrueShow reveal button for password inputs
randomButtonbooleanfalseShow random generator button
clearButtonIconstring”❌“Icon for clear button
revealButtonIconstring”👀“Icon for reveal button
searchButtonIconstring”🔍“Icon for search button
randomButtonIconstring”🎲“Icon for random button
customButtonsarray[]Array of custom button configurations
searchButtonClickstring”submit”Action for search button (“submit” or custom)
randomSymbolsstring”…”Symbol groups for random generation
randomLengthnumber12Length of generated random string
badgestringnullBadge text to display
clsComponentstring""Additional CSS class for component container
clsInputstring""Additional CSS class for input element
clsPrependstring""Additional CSS class for prepend element
clsAppendstring""Additional CSS class for append element
clsClearButtonstring""Additional CSS class for clear button
clsRevealButtonstring""Additional CSS class for reveal button
clsCustomButtonstring""Additional CSS class for custom buttons
clsSearchButtonstring""Additional CSS class for search button
clsRandomButtonstring""Additional CSS class for random button
clsLabelstring""Additional CSS class for label

Default Symbol Groups for Random Generation

Section titled “Default Symbol Groups for Random Generation”

0123456789;abcdefghijklmnopqrstuvwxyz;ABCDEFGHIJKLMNOPQRSTUVWXYZ;<>!?@#$%^&*()_+

  • getHistory() - Returns the input history array.
  • getHistoryIndex() - Returns the current history index.
  • setHistoryIndex(val) - Sets the current history index.
  • setHistory(history, append) - Sets the history array. If append is true, adds to existing history.
  • clear() - Clears the input value.
  • toDefault() - Resets input to default value.
  • disable() - Disables the input component.
  • enable() - Enables the input component.
  • toggleState() - Toggles the enabled/disabled state.
  • setAutocompleteList(list) - Sets the autocomplete data list.
  • val(value, splitter) - Gets or sets the input value. For inputs with prepend/append options, uses splitter to separate values.
  • prependOptionsVal(value) - Gets or sets the prepend options value.
  • appendOptionsVal(value) - Gets or sets the append options value.
  • destroy() - Destroys the component and removes all event listeners.
const input = Metro.getPlugin('#myInput', 'input');
// Get/set value
const currentValue = input.val();
input.val('new value');
// Work with history
input.setHistory(['item1', 'item2', 'item3']);
const history = input.getHistory();
// Control state
input.disable();
input.enable();
input.clear();
// Set autocomplete data
input.setAutocompleteList(['option1', 'option2', 'option3']);
EventDescription
onInputCreateFired when the input component is created
onAutocompleteSelectFired when an autocomplete item is selected
onHistoryChangeFired when history changes (new item added)
onHistoryUpFired when navigating up in history
onHistoryDownFired when navigating down in history
onClearClickFired when the clear button is clicked
onRevealClickFired when the reveal button is clicked
onSearchButtonClickFired when the search button is clicked
onEnterClickFired when Enter key is pressed
VariableDefault (Light)Dark ModeDescription
--input-autocomplete-background#ffffff#2b2d30Background color of autocomplete dropdown
--input-autocomplete-color#191919#dbdfe7Text color of autocomplete items
--input-autocomplete-selected-color#468cff#ffc351Color of selected text in autocomplete
--input-autocomplete-selected-backgroundtransparenttransparentBackground of selected text in autocomplete
--input-border-radius4px4pxBorder radius of the input
/* Custom input styling */
#my-input {
--input-border-radius: 8px;
--input-autocomplete-background: #f5f5f5;
--input-autocomplete-selected-color: #ff6b6b;
}
/* Pill-shaped input */
.my-pill-input {
--input-border-radius: calc(var(--input-height) * 0.44);
}
  • .input - Main container class for the input component
  • .input.focused - Applied when input is focused
  • .input.disabled - Applied when input is disabled
  • .pill-input - Creates a pill-shaped input with rounded borders
  • .rtl - Right-to-left text direction support
  • .button-group - Container for action buttons
  • .input-clear-button - Clear button
  • .input-reveal-button - Password reveal button
  • .input-search-button - Search button
  • .input-random-button - Random generator button
  • .input-custom-button - Custom buttons
  • .prepend - Prepend text container
  • .append - Append text container
  • .prepend-options - Prepend dropdown container
  • .append-options - Append dropdown container
  • .autocomplete-list - Autocomplete dropdown container
  • .autocomplete-list .item - Individual autocomplete items
  • .badge - Badge element
  • .label-for-input - Label styling
  • The input component automatically handles keyboard navigation for history (Up/Down arrows)
  • Autocomplete supports both static data and dynamic loading from URLs
  • The component supports RTL (right-to-left) text direction
  • Custom buttons can be configured with their own click handlers and styling
  • The random generator creates passwords using configurable symbol groups
  • History functionality stores previous input values for easy recall
  • Use appropriate input types (text, password, email, etc.) for better accessibility
  • Provide meaningful labels for screen readers
  • Consider using autocomplete for frequently entered data
  • Use the clear button for better user experience on mobile devices
  • Configure appropriate autocomplete list height for your use case
  • Use custom buttons sparingly to avoid cluttering the interface
  • Test with both light and dark themes when customizing colors