InlineEdit
Un valor que se cambia donde se muestra. Pulsa el lápiz, cámbialo, Enter guarda y Escape cancela; si no se puede guardar, dice por qué debajo del campo.
import { InlineEdit, KeyValue } from '@sypnia/ui/react'
Detalles de un informe
Prueba una clave con un espacio y luego la clave «admin».
- Title
- Orders by country
- Key
- orders_by_country
- Owner
- Nobody yet
Código
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
La pieza real y su skeleton: la misma caja, así nada se mueve cuando llegan los datos.
- Title
- Orders by country
- Title
- Owner
Código
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 />],
]} />
);
} Cuándo usarlo
Úsalo cuando
- Una página de detalle donde uno o dos valores cambian de vez en cuando: un título, una clave, un responsable.
- Lanza un Error con un mensaje legible desde
onSavecuando el servidor lo rechaza: se muestra debajo del campo.
No lo uses cuando
- Varios valores que deben cambiar juntos: usa un Form o un Dialog.
- Valores en una tabla: el DataTable edita sus propias celdas.
Para todos
- El lápiz es un botón llamado «Editar Title», así cada uno se distingue.
- Al guardar o cancelar, el foco vuelve al lápiz.
- El campo se nombra con la etiqueta del valor y el error queda unido a él.
Props
| InlineEdit | Tipo | Por defecto |
|---|---|---|
labelobligatorioWhat the value is called: it names the edit button and the field. | string | |
onSaveobligatorioSaves 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> | |
valueobligatorio | 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 | Tipo | Por defecto |
|---|---|---|
itemsobligatorio | [ReactNode, ReactNode][] | |
loading | boolean |