Skip to content

PromptDialog

Asks for one value before doing something: the name of a copy, a new name. The field has the focus, Enter sends, and an answer from the server is shown under the field.

import { PromptDialog } from '@sypnia/ui/react'

On this page
  1. Duplicate a table
  2. When to use it
  3. For everyone
  4. Props

Duplicate a table

Empty the field, then try «orders», then any other name.

Code
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 />
    </>
  );
}

When to use it

Use it when

  • An action that needs exactly one value, such as Duplicate or Rename.
  • Suggest a value with defaultValue so most people only press Enter.

Do not use it when

  • Two or more values: use a Dialog with a Form.
  • Values edited where they are shown: use InlineEdit.

For everyone

  • The field has the focus when it opens and is named by label.
  • Errors are tied to the field and read with it.
  • While it sends, the button works and Escape does not close it.
  • A press outside does nothing, by design: it closes with Cancel, the cross or Escape, so a value being typed is not lost.

Props

PromptDialogTypeDefault
actionrequired

The button says what happens: «Duplicate», «Rename».

string
labelrequired

The field's label: «Name of the copy».

string
onCloserequired () => void
onSubmitrequired

Receives 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>
titlerequired ReactNode
defaultValue string
hint ReactNode
machine boolean
open boolean true
validate

Returns what is wrong with the value, or nothing.

((value: string) => string)
  1. Duplicate a table
  2. When to use it
  3. For everyone
  4. Props

    Search pages, sections and terms