InlineEdit
A value that is changed where it is shown. Press the pencil, change it, Enter saves and Escape cancels; if it cannot be saved, it says why under the field.
import { InlineEdit, KeyValue } from '@sypnia/ui/react'
Details of a report
Try a key with a space, then the key «admin».
- Title
- Orders by country
- Key
- orders_by_country
- Owner
- Nobody yet
Code
import { useState } from 'react';
import { InlineEdit, KeyValue } from '@sypnia/ui/react';
// Press the pencil. Enter saves, Escape cancels. A name with spaces cannot be saved; «admin» is «taken» by the server.
export default function Values() {
const [title, setTitle] = useState('Orders by country');
const [key, setKey] = useState('orders_by_country');
return (
<KeyValue items={[
['Title', <InlineEdit label="Title" value={title} onSave={async (v) => { await new Promise((r) => setTimeout(r, 700)); setTitle(v); }} />],
['Key', <InlineEdit label="Key" machine value={key}
validate={(v) => (/\s/.test(v) ? 'Use no spaces: orders_by_country.' : v ? undefined : 'Write a key.')}
onSave={async (v) => { await new Promise((r) => setTimeout(r, 700)); if (v === 'admin') throw new Error('admin is reserved. Choose another key.'); setKey(v); }} />],
['Owner', <InlineEdit label="Owner" value="" placeholder="Nobody yet" onSave={() => {}} />],
]} />
);
} Skeleton
The real piece and its skeleton: the same box, so nothing moves when the data arrives.
- Title
- Orders by country
- Title
- Owner
Code
import { InlineEdit, InlineEditSkeleton, KeyValue } from '@sypnia/ui/react';
// While the value loads there is nothing to edit yet: a bar, and the room of the edit button.
export default function Skeleton() {
return (
<KeyValue items={[
['Title', <InlineEdit label="Title" value="Orders by country" onSave={() => {}} />],
['Title', <InlineEdit label="Title" value="" onSave={() => {}} loading />],
['Owner', <InlineEditSkeleton />],
]} />
);
} When to use it
Use it when
- A detail page where one or two values change now and then: a title, a key, an owner.
- Throw an Error with a readable message from
onSavewhen the server refuses: it is shown under the field.
Do not use it when
- Several values that must change together: use a Form or a Dialog.
- Values in a table: the DataTable edits its own cells.
For everyone
- The pencil is a button named «Edit Title», so each one is told apart.
- After saving or cancelling the focus goes back to the pencil.
- The field is named by the value's label and the error is tied to it.
Props
| InlineEdit | Type | Default |
|---|---|---|
labelrequiredWhat the value is called: it names the edit button and the field. | string | |
onSaverequiredSaves the new value. Throw an Error with a readable message when it cannot be saved: it is shown under the field. | (value: string) => void | Promise<void> | |
valuerequired | string | |
loadingThe value is on its way: a bar the height of the text, with no edit button yet. | boolean | |
machine | boolean | |
placeholderShown when the value is empty. | string | |
renderHow the value reads when it is not being edited. | ((value: string) => ReactNode) | |
validateReturns what is wrong with a value, or nothing when it is right. | ((value: string) => string) |
| KeyValue | Type | Default |
|---|---|---|
itemsrequired | [ReactNode, ReactNode][] | |
loading | boolean |