Skip to content

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'

On this page
  1. Details of a report
  2. Skeleton
  3. When to use it
  4. For everyone
  5. Props

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 onSave when 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

InlineEditTypeDefault
labelrequired

What the value is called: it names the edit button and the field.

string
onSaverequired

Saves 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
loading

The value is on its way: a bar the height of the text, with no edit button yet.

boolean
machine boolean
placeholder

Shown when the value is empty.

string
render

How the value reads when it is not being edited.

((value: string) => ReactNode)
validate

Returns what is wrong with a value, or nothing when it is right.

((value: string) => string)
KeyValueTypeDefault
itemsrequired [ReactNode, ReactNode][]
loading boolean
  1. Details of a report
  2. Skeleton
  3. When to use it
  4. For everyone
  5. Props

    Search pages, sections and terms