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
| Property | Type | Description |
|---|---|---|
| listEl | HTMLElement | The element holding one row per item. |
| newItemButton | ExtraButtonComponent | The button that adds the typed item. |
| newItemInputEl | HTMLInputElement | The input the next item is typed into. |
| onChange | (callback: (newValue: readonly string[]) => void) => this | Sets a callback function to be called when the value of the component changes. (Inherited from ValueComponentWithChangeTracking) |
Methods
| Method | Returns | Description |
|---|---|---|
| addTypedItem() | boolean | Adds the text typed into ListComponent.newItemInputEl, when it normalizes to an item not yet in the list, and clears the input. |
| formatItem(item) | string | Formats an item for display. |
| getValue() | readonly string[] | Gets the items. |
| normalizeItem(text) | null | string | Turns the typed text into an item. By default, trims it and refuses an empty one. |
| onChange(callback) | this | Sets the callback to be called when the user adds, moves or removes an item. |
| setDisabled(isDisabled) | this | Sets the disabled state of the component. |
| setPlaceholder(placeholder) | this | Sets the placeholder of ListComponent.newItemInputEl. |
| setValue(value) | this | Sets the items. |
Links to this page: