HexagonJS
Edit Page
Single Select
Provides a <select> like element with more features and options.
Examples

Standard

HTML

<p>Standard</p>
<div id="single-select-standard"></div>

JavaScript

const items = [
  'Item #1',
  'Item #2',
  'Item #3',
  'Item #4',
];

new hx.SingleSelect('#single-select-standard', items);

Standard with search

HTML

<p>Standard with search</p>
<div id="single-select-search"></div>

JavaScript

new hx.SingleSelect('#single-select-search', items, {
  showSearch: true,
});

Nested Items

HTML

<p>Nested Items</p>
<div id="single-select-nested-items"></div>

JavaScript

const nestedItems = [
  { text: 'Name of group', children: ['Item #1', 'Item #2'] },
  { text: 'Name of second group', children: ['Item #3', 'Item #4'] },
  'Item #5 (not in group)',
  'Item #6',
];

new hx.SingleSelect('#single-select-nested-items', nestedItems);

Disabled Item

HTML

<p>Disabled Item</p>
<div id="single-select-disabled-item"></div>

JavaScript

const disabledItem = [
  { value: 'Item #1' },
  { value: 'Item #2' },
  { value: 'Item #3' },
  { value: 'Item #4' },
  { value: 'Item #5 (not available)', disabled: true },
  { value: 'Item #6' },
];

new hx.SingleSelect('#single-select-disabled-item', disabledItem);

Disabled / Required

HTML

<p>Disabled / Required</p>
<form id="single-select-form">
  <div id="single-select-disabled"></div>
  <div id="single-select-required"></div>
  <button id="submit" class="hx-btn hx-flag-button" type="submit">Submit</button>
</form>

JavaScript

new hx.SingleSelect('#single-select-disabled', items, { disabled: true });
new hx.SingleSelect('#single-select-required', items, { required: true });

hx.select('#single-select-form').on('submit', (e) => {
  e.preventDefault();
});

Fluid

HTML

<p>Fluid</p>
<div id="single-select-container"></div>

JavaScript

hx.select('#single-select-container')
  .add(hx.singleSelect(items));
Api
Prototypes
hx.SingleSelectextendsEventEmitter
Creates a <select> like element that uses an <input> for consistency and to allow for better form validation.
Constructors
hx.SingleSelectselectorString / HTMLElementitemsArray / FunctionoptionsObject
Arguments
selectorString / HTMLElement
The selector to create the single select inside.
The items to use. This can be an array of strings or objects as well as a function.
When using the default valueLookup and renderer the items array or the items returned by the items function can be a string or an object.
const stringItems = [
  'Item #1',
  'Item #2',
  'Item #3',
];

const objectItems: [
  { value: 'Item #1' },
  { value: 'Item #2' },
  { value: 'Item #3' },
];
When using item objects, there are additional properties that can be passed to an item.
Item
Properties
disabledString
Whether this item should be selectable or disabled.
value
The value for this item and the text to show when selecting the item.
Items can also be nested to create a grouped menu:
const nestedItems = [
  {
    text: 'Group',
    children: [
      'Item #1',
      'Item #2',
    ]
  }
]
ItemWithChildren
Properties
childrenArray[Item]
The array of items to display in this group.
text
The text to display in the dropdown menu as a group heading
A function can also be used to return the items. This can be syncronous or asyncronous and uses a callback style:
ItemstermStringcallbackFunction
Arguments
termString
When the single select is open and the user enters a value, this is the term they have entered.
This allows some pre-filtering to reduce the amount of data that needs to be processed in the front-end.
callbackresultsArray[Item|ItemWithChildren]
The function to call when the items have been fetched with the results.
The single select will display Loading... until this has been called.
Arguments
resultsArray[Item|ItemWithChildren]
The array of items to show.
It is worth noting that when using a function, the items will be cached by search term to reduce the amount of calls to the server that are made. If the data is dynamic, pass useCache: false in when creating the SingleSelect
options
Properties
chooseValueTextString
The text to display as a placeholder when no value is selected.
Default: userFacingText('singleSelect', 'chooseValue')
disabledBoolean
Whether the single select should be disabled
filteritemsArraytermStringArray
The function to use when filtering internally. Should only be used when one of the hx.filter methods isn't suitable for filtering the data.
Arguments
itemsArray
The items from the single select cache or data source
termString
The search term to use when filtering
Returns
The filtered data
filterOptions
The options to use when filtering internally. The available options can be found on the Filter page.
loadingTextString
The text to display when loading items. This is shown both in the input as well as in the dropdown when items are being searched.
Default: userFacingText('singleSelect', 'loading')
matchTypeString
The type of filtering the single select should use when filtering internally
The internal filter uses the hx.filter functions. All the filter types can be specified as the match type (e.g. 'startsWith', 'exact' or 'fuzzy') and the default value is 'contains'
In addition to the internal filter, external matching can be used (where the data returned from the callback is used directly and not sorted/filtered internally) by setting the match type to 'external'
Default: 'contains'
noResultsTextString
The text to display when no results are found when searching
Default: userFacingText('singleSelect', 'noResults')
pleaseSelectAValueTextString
The text passed to setCustomValidity when no value has been selected and this single select is marked as required
Default: userFacingText('singleSelect', 'pleaseSelectAValueText')
rendererelemHTMLElementitemObject
A function used to render the items in the dropdown
The default renderer sets the html to the item value using the valueLookup function.
When overriding the renderer, the item passed to the render function is the same as the item passed in to the dataset
Default:
(element, item) => {
  const sel = select(element);
  if (item && item.children) {
    sel.text(item.text);
  } else {
    sel.text(options.valueLookup(item));
  }
}
Arguments
elemHTMLElement
The element to populate
item
The data item to populate the element with
requiredBoolean
Whether the single select should be required.
This is mainly applicable when used inside a Form. When this is true, the pleaseSelectAValueText option is used as the error message.
searchTextString
The text to display as a placeholder in the search input when no value is selected.
Default: userFacingText('singleSelect', 'search')
showSearchBoolean
Whether to show the search box in the dropdown to allow the user to search the available items.
trimTrailingSpacesBoolean
Whether trailing spaces should be trimmed from the search term when filtering.
Default: false
useCacheBoolean
Whether the underlying data feed should attempt to cache results based on search term.
This should be set to false when using a function to return items that can change over time (i.e. are not static).
valueAny
The value to set.
By default, this can be a string or an object with value property and should correspond to one of the items.
valueLookupitemAnyString
Set a custom value lookup.
Default:
item => (item ? (item.value || item) : undefined);
Arguments
itemAny
The item to retrieve the value from.
Returns
The string value that is used to filter items and display the selected value in the input field.
Events
change
The event emit when the value of the single select is changed.
Properties
causeString
The cause of the event. Either user when caused by a user interaction or api when caused by calling the value method
valueAny
The currently set value for the single select. Can be any of the items passed in, or undefined
dropdown.changeBoolean
Emitted when the dropdown is shown or hidden. The data sent with this event is a boolean indicating whether or not the dropdown is visible. True means that the dropdown has just been shown, false means it has just been hidden.
dropdown.hideend
Emitted when the dropdown animation ends. No data is sent with this event.
dropdown.hidestart
Emitted when the dropdown animation starts. No data is sent with this event.
dropdown.showend
Emitted when the dropdown animation finishes. No data is sent with this event.
dropdown.showstart
Emitted when the dropdown animation starts. No data is sent with this event.
highlight
The event called when a menu item is set as the active item. This can only be done by the keyboard or when the user clicks on an item
Properties
causeString
The cause of the event. Currently it is only possible for this event to be caused by user interaction.
value
Properties
eventTypeString
The type of event that caused the selection:
  • 'click' - User clicked an item
  • 'arrow' - User used the arrow keys
itemAny
The item as it was passed into the single select.
Methods
clearCache
Clears the cached search results manually
disabledBoolean
Get the current disabled state of the single select
Returns
The current disabled state
disableddisabledBooleanSingleSelect
Sets the current disabled state of the single select
Arguments
disabledBoolean
Whether the single select should be disabled
Returns
SingleSelect
This single select
hideSingleSelect
Hides the open single select dropdown.
Returns
SingleSelect
This single select
Get the current items of the single select
Returns
The current items
itemsitemsArray / FunctionSingleSelect
Sets the current items of the single select
Arguments
The items to set
Returns
SingleSelect
This single select
rendererFunction
Get the current renderer of the single select
Returns
function
The current renderer
rendererrendererFunctionSingleSelect
Sets the current renderer of the single select
Arguments
renderer
The renderer to set
Returns
SingleSelect
This single select
valueAny
Get the current value of the single select
Returns
Any
The current value
valuevalueAnySingleSelect
Sets the current value of the single select
Arguments
valueAny
Whether the single select should be value
Returns
SingleSelect
This single select
Functions
hx.singleSelectitemsArray / FunctionoptionsObjectSelection
Fluid version of the single select. Returns a selection containing a pre-created single select component.
Arguments
The items to use. This can be an array of strings or objects as well as a function.
When using the default valueLookup and renderer the items array or the items returned by the items function can be a string or an object.
const stringItems = [
  'Item #1',
  'Item #2',
  'Item #3',
];

const objectItems: [
  { value: 'Item #1' },
  { value: 'Item #2' },
  { value: 'Item #3' },
];
When using item objects, there are additional properties that can be passed to an item.
Item
Properties
disabledString
Whether this item should be selectable or disabled.
value
The value for this item and the text to show when selecting the item.
Items can also be nested to create a grouped menu:
const nestedItems = [
  {
    text: 'Group',
    children: [
      'Item #1',
      'Item #2',
    ]
  }
]
ItemWithChildren
Properties
childrenArray[Item]
The array of items to display in this group.
text
The text to display in the dropdown menu as a group heading
A function can also be used to return the items. This can be syncronous or asyncronous and uses a callback style:
ItemstermStringcallbackFunction
Arguments
termString
When the single select is open and the user enters a value, this is the term they have entered.
This allows some pre-filtering to reduce the amount of data that needs to be processed in the front-end.
callbackresultsArray[Item|ItemWithChildren]
The function to call when the items have been fetched with the results.
The single select will display Loading... until this has been called.
Arguments
resultsArray[Item|ItemWithChildren]
The array of items to show.
It is worth noting that when using a function, the items will be cached by search term to reduce the amount of calls to the server that are made. If the data is dynamic, pass useCache: false in when creating the SingleSelect
options
Properties
chooseValueTextString
The text to display as a placeholder when no value is selected.
Default: userFacingText('singleSelect', 'chooseValue')
disabledBoolean
Whether the single select should be disabled
filteritemsArraytermStringArray
The function to use when filtering internally. Should only be used when one of the hx.filter methods isn't suitable for filtering the data.
Arguments
itemsArray
The items from the single select cache or data source
termString
The search term to use when filtering
Returns
The filtered data
filterOptions
The options to use when filtering internally. The available options can be found on the Filter page.
loadingTextString
The text to display when loading items. This is shown both in the input as well as in the dropdown when items are being searched.
Default: userFacingText('singleSelect', 'loading')
matchTypeString
The type of filtering the single select should use when filtering internally
The internal filter uses the hx.filter functions. All the filter types can be specified as the match type (e.g. 'startsWith', 'exact' or 'fuzzy') and the default value is 'contains'
In addition to the internal filter, external matching can be used (where the data returned from the callback is used directly and not sorted/filtered internally) by setting the match type to 'external'
Default: 'contains'
noResultsTextString
The text to display when no results are found when searching
Default: userFacingText('singleSelect', 'noResults')
pleaseSelectAValueTextString
The text passed to setCustomValidity when no value has been selected and this single select is marked as required
Default: userFacingText('singleSelect', 'pleaseSelectAValueText')
rendererelemHTMLElementitemObject
A function used to render the items in the dropdown
The default renderer sets the html to the item value using the valueLookup function.
When overriding the renderer, the item passed to the render function is the same as the item passed in to the dataset
Default:
(element, item) => {
  const sel = select(element);
  if (item && item.children) {
    sel.text(item.text);
  } else {
    sel.text(options.valueLookup(item));
  }
}
Arguments
elemHTMLElement
The element to populate
item
The data item to populate the element with
requiredBoolean
Whether the single select should be required.
This is mainly applicable when used inside a Form. When this is true, the pleaseSelectAValueText option is used as the error message.
searchTextString
The text to display as a placeholder in the search input when no value is selected.
Default: userFacingText('singleSelect', 'search')
showSearchBoolean
Whether to show the search box in the dropdown to allow the user to search the available items.
trimTrailingSpacesBoolean
Whether trailing spaces should be trimmed from the search term when filtering.
Default: false
useCacheBoolean
Whether the underlying data feed should attempt to cache results based on search term.
This should be set to false when using a function to return items that can change over time (i.e. are not static).
valueAny
The value to set.
By default, this can be a string or an object with value property and should correspond to one of the items.
valueLookupitemAnyString
Set a custom value lookup.
Default:
item => (item ? (item.value || item) : undefined);
Arguments
itemAny
The item to retrieve the value from.
Returns
The string value that is used to filter items and display the selected value in the input field.
Returns
Selection
A selection containing an element with an SingleSelect initialised on it