Skip to content

Custom rendering ​

For most needs, rich options (descriptions, avatars, meta text, badges) are enough. When you need full control, pass render functions.

js
const escape = (text) => text.replace(/[&<>"']/g, (char) => `&#${char.charCodeAt(0)};`);

new FlowSelect('#colour', {
  renderOption: (item, { selected }) =>
    `<span class="swatch" style="background:${escape(item.data.color)}"></span>
     <span class="fs-label">${escape(item.label)}</span>`,
  renderValue: (item) =>
    `<span class="swatch" style="background:${escape(item.data.color)}"></span>
     <span class="fs-value-text">${escape(item.label)}</span>`
});

The functions ​

OptionCalled withReplaces
renderOption(item, { selected })Each rowEverything in the row after the check mark
renderValue(item)The chosen itemThe content of the closed control (single selects)

Return a DOM Node or an HTML string. Return null or undefined to use the default rendering for that item.

The item ​

PropertyValue
value, labelStrings
description, meta, badge, avatar, keywords, groupStrings, empty when not set
disabledBoolean
dataFor native options, the option's dataset (so data-color is item.data.color). For remote items, the original server object

Escape your data

HTML strings you return are inserted as they are. Escape every value that comes from users or a server, as in the example above, or build DOM nodes with textContent. The default rendering always uses textContent.

Released under the MIT License.