PromptDialog
Pide un valor antes de hacer algo: el nombre de una copia, un nombre nuevo. El campo tiene el foco, Enter envía, y la respuesta del servidor se muestra debajo del campo.
import { PromptDialog } from '@sypnia/ui/react'
En esta página
Duplicar una tabla
Vacía el campo, luego prueba «orders» y después cualquier otro nombre.
Código
import { useState } from 'react';
import { Button, PromptDialog, toast, Toaster } from '@sypnia/ui/react';
// One value before acting. Empty it to see the check; the name «orders» is «taken» by the server.
export default function Duplicate() {
const [open, setOpen] = useState(false);
return (
<>
<>
<Button icon="duplicate" onClick={() => setOpen(true)}>Duplicate orders</Button>
<PromptDialog open={open} onClose={() => setOpen(false)} title="Duplicate the table orders" label="Name of the copy" defaultValue="orders_copy" machine
hint="Lowercase letters, numbers and underscores." action="Duplicate"
validate={(v) => (/^[a-z][a-z0-9_]*$/.test(v) ? undefined : 'Use lowercase letters, numbers and underscores, starting with a letter.')}
onSubmit={async (v) => {
await new Promise((r) => setTimeout(r, 900));
if (v === 'orders') throw new Error('There is already a table called orders. Choose another name.');
toast(`The copy ${v} is ready`, { tone: 'ok' });
}} />
</>
<Toaster />
</>
);
} Cuándo usarlo
Úsalo cuando
- Una acción que necesita exactamente un valor, como Duplicar o Renombrar.
- Sugiere un valor con
defaultValuepara que la mayoría solo pulse Enter.
No lo uses cuando
- Dos o más valores: usa un Dialog con un Form.
- Valores que se editan donde se muestran: usa InlineEdit.
Para todos
- El campo tiene el foco al abrirse y se nombra con
label. - Los errores quedan unidos al campo y se leen con él.
- Mientras envía, el botón trabaja y Escape no lo cierra.
- Un toque afuera no hace nada, a propósito: se cierra con Cancelar, la cruz o Escape, así no se pierde un valor a medio escribir.
Props
| PromptDialog | Tipo | Por defecto |
|---|---|---|
actionobligatorioThe button says what happens: «Duplicate», «Rename». | string | |
labelobligatorioThe field's label: «Name of the copy». | string | |
onCloseobligatorio | () => void | |
onSubmitobligatorioReceives the value. It may be async: the button works meanwhile. Throw an Error with a readable message to show it under the field. | (value: string) => void | Promise<void> | |
titleobligatorio | ReactNode | |
defaultValue | string | |
hint | ReactNode | |
machine | boolean | |
open | boolean | true |
validateReturns what is wrong with the value, or nothing. | ((value: string) => string) |