Key-value editor

Slant / Forms

Keys and values in rows, with unique keys and an optional type for each value.

Demo

Usage

@tuttn/slant
import { Field, KeyValueEditor } from '@tuttn/slant'

// Uncontrolled, in a Field, submitted as a JSON object: {"NODE_ENV":"production"}.
<Field>
  <Field.Label>Environment</Field.Label>
  <KeyValueEditor defaultValue={[{ key: 'NODE_ENV', value: 'production' }]} name="env" />
</Field>

// Controlled, with typed values: each row gets a type Select, and the
// submitted object holds numbers, booleans and parsed JSON.
const [settings, setSettings] = useState<KeyValuePair[]>([{ key: 'retries', value: '3', type: 'number' }])
<KeyValueEditor label="Settings" value={settings} onValueChange={setSettings} types={['text', 'number', 'boolean', 'json']} />

// Headers: keys compared without case, and a rule of your own.
<KeyValueEditor label="Headers" caseSensitive={false} keyLabel="Header" keyPlaceholder="Accept"
  validateKey={key => /^[\w-]+$/.test(key) ? null : 'Letters, digits and hyphens only'} max={20} />

Props

PropTypeDefaultDescription
valueKeyValuePair[]—The pairs, one row each. Pass it with onValueChange to control the editor; leave it out to let it keep its own.
defaultValueKeyValuePair[][]The pairs an uncontrolled editor starts with.
onValueChange(value: KeyValuePair[]) => void—Called with the new pairs after every edit, add, remove and move, errors and all.
typesreadonly KeyValueType[]['text']The value types a row can take. With more than one, each row gets a type Select, and a pair without a type takes the first. Changing a row's type converts its value where it can (to boolean, "true" stays and anything else is "false"; to number or JSON, a value that parses stays) and clears it otherwise.
keyLabelstring'Key'The key column's name: its head, and the start of each key's accessible name ("Key 2"). From the root's `messages.key`.
valueLabelstring'Value'The value column's name: its head, and the start of each value's accessible name ("Value 2"). From the root's `messages.value`.
keyPlaceholderstring—Placeholder text in every empty key.
valuePlaceholderstring—Placeholder text in every empty text, number or JSON value.
caseSensitivebooleantrueWhether `PATH` and `path` are different keys, in the duplicate check and the submitted object.
validateKey(key: string) => string | null | undefined—An extra rule for keys, such as an environment variable's pattern: return the error to show, or nothing when the key is fine. It gets non-empty keys only, without the spaces around them, and its error shows once the key has lost focus.
minnumber—The fewest rows: at this count Remove is unavailable, and says why.
maxnumber—The most rows: at this count Add is unavailable, and says why.
addLabelReactNode'Add'The Add button's text. From the root's `messages.addItem`.
labelstring—The group's accessible name, unless a Field's label or `aria-labelledby` names it.
namestring—Submits the pairs with a form: a hidden input with this name carries them as a JSON object (`{"PORT":8080}`): non-empty keys, values parsed by type (one that doesn't parse goes as its text), the last of duplicate keys winning. With no pairs, `{}`.
formstring—The id of the form the hidden input belongs to, when the editor sits outside it.
disabledboolean—Disables every cell and button. A disabled editor is not submitted. Inside a Field, the field's `disabled` does this.
readOnlyboolean—Keeps the cells focusable and readable, but nothing changes them, and leaves out Add, the move and remove buttons and the grip. Inside a Field, the field's `readOnly` does this.
invalidboolean—Marks the group invalid (`data-invalid`, a danger border round the rows). Inside a Field, the field's `invalid` does this.
size'sm' | 'md''md'The cells' and buttons' height: 28 or 34px at regular density.
KeyValuePair{ key: string; value: string; type?: KeyValueType }—One row. `value` is the text as typed, whatever the type ("8080", "true", '{"a":1}'). A pair without a type, or with one the editor doesn't offer, takes the first of `types`.
KeyValueType'text' | 'number' | 'boolean' | 'json'—A value's type: an Input; an Input with a decimal keyboard, checked as a finite number; a Switch ("true" or "false"); a monospace Textarea that grows with its content, checked with JSON.parse.
…restHTMLAttributes<HTMLDivElement>—className, style, other attributes and the ref reach the root, Repeater's role="group" element.

Keyboard

Tab / Shift+Tabmoves through each row's key, type and value, then its Move up, Move down and Remove buttons, row by row, then Add
Space on a boolean valueswitches it between true and false
Alt+ArrowUp / Alt+ArrowDown on a move buttonmoves the row one place up or down; Enter or Space on the button does the same
Alt+Home / Alt+End on a move buttonmoves the row to the top or the bottom
Enter / Space on Removeremoves the row; focus moves into the next row, or the previous one, or to Add
Enter / Space on Addappends an empty row and focuses its key

Accessibility

  • The root is Repeater's role="group", named by a Field's label through aria-labelledby (or by label), and described by the field's description and error. Each row is a group named by its key, or "Item 2" while the key is empty.
  • Each cell is named for its column and row through messages.keyValueCell: "Key 2", "Type 2", "Value 2", with keyLabel and valueLabel as the column names. The column heads over the rows show the same names and are hidden from assistive technology.
  • An error shows under its cell, whose aria-describedby names it, and the cell takes aria-invalid. A duplicate key shows at once, in every row that uses it; an empty key beside a value, a value that doesn't parse, and validateKey's refusals show once the cell (for an empty key, either cell of the row) has lost focus.
  • A boolean value is a Switch; read-only, it keeps its state and says so with aria-readonly.

Types

With one type (['text'] by default) there is no type column. With several, each row gets a Select, a new row takes the first type, and changing a row's type converts its value where it can: to boolean, "true" stays and anything else becomes "false"; to number or JSON, a value that parses stays; otherwise it is cleared. Text keeps any value.

Checks

Keys must be unique: a key used more than once marks every row using it, compared without the spaces around it, and without case when caseSensitive is false. A key that's empty while its row has a value needs one, a number must be finite and JSON must parse, and validateKey adds a rule of yours. Rows with neither key nor value are never errors. The errors don't block anything themselves: check the pairs before you save them.

Forms

With a name, a hidden input carries the pairs as one JSON object: rows with a key, values parsed by type (a value that doesn't parse goes as its text), and the last of duplicate keys winning. With no pairs it is {}, so the server can always parse it. The cells themselves submit nothing, and a disabled editor submits nothing at all.