RowActions
Lo que se puede hacer con una fila, detrás de un solo botón, para que la tabla no se llene de botones.
import { RowActions } from '@sypnia/ui/react'
En esta página
Acciones de cada clave
Abre el … de una fila. «Pausar» está apagado en una clave que nunca se usó; «Borrar» pregunta primero.
| Name | Key | Last used | Actions |
|---|---|---|---|
| Back office | key_live_01 | Today | |
| Mobile app | key_live_02 | Yesterday | |
| Tests | key_test_03 | Never |
Código
import { useState } from 'react';
import { ConfirmDialog, RowActions, Table, type Column } from '@sypnia/ui/react';
interface Key { id: string; name: string; used: string }
const start: Key[] = [
{ id: 'key_live_01', name: 'Back office', used: 'Today' },
{ id: 'key_live_02', name: 'Mobile app', used: 'Yesterday' },
{ id: 'key_test_03', name: 'Tests', used: 'Never' },
];
// Everything that can be done to one row, behind one button, so the table does not fill up with buttons.
// The button is named after its row, and deleting asks first.
export default function Basic() {
const [keys, setKeys] = useState(start);
const [doomed, setDoomed] = useState<Key | null>(null);
const [said, setSaid] = useState('');
const columns: Column<Key>[] = [
{ id: 'name', header: 'Name', cell: (k) => k.name },
{ id: 'id', header: 'Key', cell: (k) => <span className="sy-machine">{k.id}</span> },
{ id: 'used', header: 'Last used', cell: (k) => k.used },
{ id: 'actions', header: 'Actions', cell: (k) => (
<RowActions label={`Actions for ${k.name}`} actions={[
{ id: 'copy', label: 'Copy the key', icon: 'copy', onAction: () => setSaid(`Copied ${k.id}`) },
{ id: 'rename', label: 'Rename', icon: 'edit', onAction: () => setSaid(`Rename ${k.name}`) },
{ id: 'pause', label: 'Pause', icon: 'pause', disabled: k.used === 'Never', onAction: () => setSaid(`Paused ${k.name}`) },
{ id: 'delete', label: 'Delete', icon: 'trash', danger: true, onAction: () => setDoomed(k) },
]} />
) },
];
return (
<div className="sy-stack">
<Table label="API keys" columns={columns} rows={keys} rowKey={(k) => k.id} />
<p role="status">{said}</p>
<ConfirmDialog open={!!doomed} onClose={() => setDoomed(null)} title={`Delete the key «${doomed?.name}»?`}
action="Delete key" onConfirm={() => { setKeys((all) => all.filter((k) => k.id !== doomed?.id)); setDoomed(null); }}>
Apps that use it stop working at once. This cannot be undone.
</ConfirmDialog>
</div>
);
} Cuándo usarlo
Úsalo cuando
- Una fila tiene dos acciones o más: copiar, renombrar, pausar, borrar.
- Nombra el botón según su fila con
label: «Acciones de Back office». - La acción destructiva va de última, con
danger, y pregunta primero con un ConfirmDialog.
No lo uses cuando
- Hay una acción que la gente usa todo el tiempo: muéstrala como su propio botón.
- La acción es para varias filas: BulkActionBar.
Para todos
- El botón abre un menú y lleva el nombre de su fila; las flechas recorren las acciones, Escape cierra y el foco vuelve.
- Una acción que ahora no se puede usar queda apagada en el menú, y el menú la sigue mostrando para que nadie se pregunte dónde quedó.
- En el celular el botón mide al menos 44 px.
Props
| RowActions | Tipo | Por defecto |
|---|---|---|
actionsobligatorio | RowAction[] | |
labelThe name of the button, saying which row it belongs to: «Actions for order 1042». | string |