Skip to content

ListComponent

A component that edits a reorderable list of strings: each item has move-up, move-down and remove buttons, and a row below the list adds an item, on its button or on Enter.

ListComponent.newItemInputEl is a plain <input>, so a suggest can be attached to it; FolderListComponent and TagListComponent do exactly that. A subclass shapes what is added by overriding ListComponent.normalizeItem and how an item reads by overriding ListComponent.formatItem. An item already in the list is not added twice.

You can add this component using SettingEx.addList.

In order to add the styles for the component, use initPluginContext in your plugin’s onload() function.

Alternatively, you can copy styles from https://unpkg.com/@mnaoumov/obsidian-lib/dist/styles.css.

Import:

import { ListComponent } from '@mnaoumov/obsidian-lib/setting-components/list-component';

Signature:

export class ListComponent extends ValueComponent<readonly string[]> implements ValueComponentWithChangeTracking<readonly string[]>

Extends: ValueComponent<readonly string[]>

Implements: ValueComponentWithChangeTracking<readonly string[]>

Constructor

new ListComponent(containerEl: HTMLElement, cssClass?: string | undefined)

Creates a new list component.

Properties

PropertyTypeDescription
listElHTMLElementThe element holding one row per item.
newItemButtonExtraButtonComponentThe button that adds the typed item.
newItemInputElHTMLInputElementThe input the next item is typed into.
onChange(callback: (newValue: readonly string[]) => void) => thisSets a callback function to be called when the value of the component changes.
(Inherited from ValueComponentWithChangeTracking)

Methods

MethodReturnsDescription
addTypedItem()booleanAdds the text typed into ListComponent.newItemInputEl, when it normalizes to an item not yet in the list, and clears the input.
formatItem(item)stringFormats an item for display.
getValue()readonly string[]Gets the items.
normalizeItem(text)null | stringTurns the typed text into an item. By default, trims it and refuses an empty one.
onChange(callback)thisSets the callback to be called when the user adds, moves or removes an item.
setDisabled(isDisabled)thisSets the disabled state of the component.
setPlaceholder(placeholder)thisSets the placeholder of ListComponent.newItemInputEl.
setValue(value)thisSets the items.

Links to this page: