Key-value editor
Keys and values in rows, with unique keys and an optional type for each value.
Demo
Usage
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
| Prop | Type | Default | Description |
|---|---|---|---|
value | KeyValuePair[] | — | The pairs, one row each. Pass it with onValueChange to control the editor; leave it out to let it keep its own. |
defaultValue | KeyValuePair[] | [] | 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. |
types | readonly 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. |
keyLabel | string | '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`. |
valueLabel | string | '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`. |
keyPlaceholder | string | — | Placeholder text in every empty key. |
valuePlaceholder | string | — | Placeholder text in every empty text, number or JSON value. |
caseSensitive | boolean | true | Whether `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. |
min | number | — | The fewest rows: at this count Remove is unavailable, and says why. |
max | number | — | The most rows: at this count Add is unavailable, and says why. |
addLabel | ReactNode | 'Add' | The Add button's text. From the root's `messages.addItem`. |
label | string | — | The group's accessible name, unless a Field's label or `aria-labelledby` names it. |
name | string | — | 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, `{}`. |
form | string | — | The id of the form the hidden input belongs to, when the editor sits outside it. |
disabled | boolean | — | Disables every cell and button. A disabled editor is not submitted. Inside a Field, the field's `disabled` does this. |
readOnly | boolean | — | 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. |
invalid | boolean | — | 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. |
…rest | HTMLAttributes<HTMLDivElement> | — | className, style, other attributes and the ref reach the root, Repeater's role="group" element. |
Keyboard
| Tab / Shift+Tab | moves 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 value | switches it between true and false |
| Alt+ArrowUp / Alt+ArrowDown on a move button | moves the row one place up or down; Enter or Space on the button does the same |
| Alt+Home / Alt+End on a move button | moves the row to the top or the bottom |
| Enter / Space on Remove | removes the row; focus moves into the next row, or the previous one, or to Add |
| Enter / Space on Add | appends an empty row and focuses its key |
Accessibility
- The root is Repeater's
role="group", named by a Field's label througharia-labelledby(or bylabel), 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", withkeyLabelandvalueLabelas 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-describedbynames it, and the cell takesaria-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, andvalidateKey'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.