String List¶
An editor for a list of strings. The user types a value and presses Add or Enter, and each row has a Remove button and, when reorder is on, up and down buttons.
Decision¶
| When | Use |
|---|---|
| Hosts, paths, or names the user lists one at a time | String List |
| Short free form tags | Tag Input |
| Pairs | Key Value Editor |
| Entries with more than one field | Repeater |
| Reordering a set of items the reader does not type | Ordered List |
Usage¶
import { createStringList } from './configurationpage?name=myplugin_stolon.js';
var hosts = createStringList({
values: config.Hosts,
reorder: true,
onChange: function (values) { config.Hosts = values; tracker.check(); }
});
container.appendChild(hosts.element);
Classes: stolon-string-list, stolon-string-items, stolon-string-item, stolon-string-add, stolon-string-empty. The handle is { element, input, getValue, setValue, setItems, add, setHeight, destroy }.
Parts: stolon-string-text.
States and Modifiers¶
add(value)returns true when the value was added and shows the error otherwise.validate(value)returns an error sentence, or an empty value when valid.maxItemscaps the rows, andduplicates: trueallows a repeated value.trim(default true) trims each value.reorder: trueadds up and down buttons.- The defaults are "New item" (
inputLabel), "Add" (addText), "Remove {value}", "Move {value} up", "Move {value} down", the tooltips "Move up" and "Move down" (moveUpTitle,moveDownTitle), "Already in the list." (duplicateText), and "The list is full." (limitText).emptyTextis a sentence shown while the list is empty. It is the List's empty sentence,stolon-list-empty outside, under the rows, and nothing shows without it. - Enter adds the value and Escape clears the input. A refusal shows an inline message under the input through the validation core, with
aria-invalidandaria-describedby, and it updates as the reader types. - Remove returns focus to the input, and a move keeps focus on the pressed button.
stolon-string-itemsis hidden while empty.height,maxHeight, andminHeightset the height of the rows as a List takes them,'auto'by default so the list grows with the page.setHeight(height, maxHeight)changes them later.sizeissmall,medium(default), orlarge, and sizes its fields on the size scale.
Reference¶
Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.
createStringList¶
function createStringList(options?: StringListOptions): StringListHandle
An editor for a list of strings. The user types a value and presses Add or Enter, and each row has a Remove button and, when reorder is on, up and down buttons.
| Option | Type | What it does |
|---|---|---|
values |
unknown[] |
array of strings [optional] |
placeholder |
string |
a valid example for the input [optional] |
inputLabel |
string |
aria-label of the input (default 'New item') |
addText |
string |
text of the add button (default 'Add') |
removeLabel |
string |
aria-label of a remove button, where {value} is the row's text (default 'Remove {value}') |
moveUpLabel |
string |
aria-label of an up button (default 'Move {value} up') |
moveDownLabel |
string |
aria-label of a down button (default 'Move {value} down') |
moveUpTitle |
string |
tooltip of an up button (default 'Move up') |
moveDownTitle |
string |
tooltip of a down button (default 'Move down') |
emptyText |
string |
sentence shown while the list is empty [optional] |
duplicateText |
string |
message for a repeated value (default 'Already in the list.') |
limitText |
string |
message when maxItems is reached (default 'The list is full.') |
duplicates |
boolean |
true allows the same value twice (default false) |
reorder |
boolean |
true adds up and down buttons (default false) |
trim |
boolean |
true trims each value (default true) |
maxItems |
number |
most rows allowed [optional] |
height |
ListHeight |
height of the rows, as createList takes it (default 'auto') |
maxHeight |
number | string | null |
height past which the rows scroll [optional] |
minHeight |
number | string | null |
least height of a filling list [optional] |
size |
'small' | 'medium' | 'large' |
'small', 'medium', or 'large' for its fields (default 'medium') |
validate |
(value: string): string | null | undefined |
function(value) returning an error sentence, or an empty value when valid, checked through the validation core [optional] |
onChange |
(values: string[]): void |
function(values) [optional] |
| Handle member | Type |
|---|---|
input |
HTMLInputElement |
getValue |
(): string[] |
setValue |
(next: unknown[] | null): void |
setItems |
(next: unknown[] | null): void |
add |
(raw: unknown): boolean |
setHeight |
(height: ListHeight, maxHeight?: number | string | null): void |
destroy |
(): void |
| Class | What it looks like |
|---|---|
stolon-string-list |
Editor for a list of strings, with rows above an add row |
stolon-string-add |
Row holding the new value input and the add button |
Example¶
Example Code¶
<!-- title: String List -->
<div id="mount"></div>
<script type="module">
const base = await window.__baseReady;
const view = document.getElementById('view');
const mount = document.getElementById('mount');
mount.appendChild(base.createStringList({
values: ['one.example.test', 'two.example.test'],
reorder: true,
placeholder: 'host.example.test',
emptyText: 'Nothing added yet.',
validate: (value) => value.indexOf(' ') === -1 ? '' : 'No spaces allowed.'
}).element);
</script>