Key Value Editor¶
Rows of a key input and a value input with Add and Remove, for headers, tags, and mappings. It is a Repeater of two text fields, so its rows, Add, Remove, limits, and validation come from there. Each row validates its key and value.
Decision¶
| When | Use |
|---|---|
| Webhook headers, variables, and path mappings | Key Value Editor |
| Entries with no value | String List |
| Rows with more than two fields, or a field that is not text | Repeater |
| Keys from a fixed list, with each value from a select | Mapping Table |
Usage¶
import { createKeyValueEditor } from './configurationpage?name=myplugin_stolon.js';
var headers = createKeyValueEditor({ values: config.Headers, onChange: function (values) { config.Headers = values; } });
container.appendChild(headers.element);
if (!headers.isValid()) { return; }
Classes: stolon-kv-editor, stolon-kv-head, stolon-kv-row. The handle is { element, getValue, setValue, setItems, isValid, addRow, setHeight, destroy }.
Parts: stolon-kv-key, stolon-kv-value, stolon-kv-remove, stolon-kv-rows. The editor also carries the Repeater's classes, stolon-repeater stolon-repeater-layout-inline, and each field sits in a stolon-field-cell.
States and Modifiers¶
valuesis an array of{ key, value }, or an object whose properties become rows. An object passed tosetValuebecomes rows too.getValue()returns{ key, value }rows and leaves out a row where both are empty.- A repeated key is an error unless
duplicateKeysis on. A value with no key shows "Enter a key." and a repeated key shows "Key is already used.". validate(key, value, index)returns an error sentence, or an empty value when valid.maxRowscaps the rows.- Each row validates on input and shows its message under the key.
isValid()checks every row and shows every message, and leaves focus where it is. emptyTextis a sentence shown under the rows while there is none. It is the List's empty sentence,stolon-list-empty outside, and nothing shows without it.- The labels default to "Key", "Value", and "Add".
showLabels: falsehides the column labels.removeLabelis "Remove {value}", where the value is the key or "Row 2". height,maxHeight, andminHeightset the height of the rows as a List takes them,'auto'by default so the rows grow with the page.setHeight(height, maxHeight)changes them later.- When the editor itself is narrow it turns
compact: the column labels hide, each field shows its own label, and a row stacks to one column with a rule between rows. 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.
createKeyValueEditor¶
function createKeyValueEditor(options?: KeyValueEditorOptions): KeyValueEditorHandle
Rows of a key input and a value input with Add and Remove, for headers, tags, and mappings. It is a Repeater of two text fields. Each row validates its key and value as it is typed, and a repeated key is an error unless duplicateKeys is on.
| Option | Type | What it does |
|---|---|---|
values |
KeyValueSource | null |
array of { key, value }, or an object whose properties become rows [optional] |
keyLabel |
string |
column label and aria-label of the key inputs (default 'Key') |
valueLabel |
string |
column label and aria-label of the value inputs (default 'Value') |
showLabels |
boolean |
true shows the column labels above the rows (default true) |
keyPlaceholder |
string |
a valid example key [optional] |
valuePlaceholder |
string |
a valid example value [optional] |
addText |
string |
text of the add button (default 'Add') |
removeLabel |
string |
aria-label of a remove button, where {value} is the key or "Row 2" (default 'Remove {value}') |
rowName |
string |
prefix of that fallback row name (default 'Row') |
requiredText |
string |
message for a value with no key (default 'Enter a key.') |
duplicateText |
string |
message for a repeated key (default 'Key is already used.') |
duplicateKeys |
boolean |
true allows a repeated key (default false) |
maxRows |
number |
most rows allowed, after which Add is disabled and the limit message shows [optional] |
limitText |
string |
message when maxRows is reached (default 'No more rows can be added.') |
emptyText |
string |
sentence shown while there is no row [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 |
(key: string, value: string, index: number): string | null | undefined |
function(key, value, index) returning an error sentence, or an empty value when valid [optional] |
onChange |
(values: KeyValuePair[]): void |
function(values) [optional] |
| Handle member | Type |
|---|---|
getValue |
(): KeyValuePair[] |
setValue |
(next: KeyValueSource | null): void |
setItems |
(next: KeyValueSource | null): void |
isValid |
(): boolean |
addRow |
(data?: { key?: string; value?: string; } | null): boolean |
setHeight |
(height: ListHeight, maxHeight?: number | string | null): void |
destroy |
(): void |
| Type | What it is |
|---|---|
KeyValuePair |
One row of the editor, as getValue and onChange give it. |
KeyValueSource |
Rows to load, as an array of key and value records or an object whose properties become rows. |
| Class | What it looks like |
|---|---|
stolon-kv-editor |
Editor of key and value rows, a Repeater of two text fields, with column labels and an add button |
stolon-kv-head |
Column labels over the key and value columns, the repeater's head, hidden while the editor is narrow |
stolon-kv-rows |
Rows of a key cell, a value cell, and a remove button, the repeater's rows, which stack while the editor is narrow |
Example¶
Example Code¶
<!-- title: Key Value Editor -->
<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.createKeyValueEditor({
values: [{ key: 'Accept', value: 'application/json' }, { key: 'X-Mode', value: 'fast' }],
keyPlaceholder: 'Accept',
valuePlaceholder: 'application/json'
}).element);
</script>