Ir al contenido

QueryEditor

El editor de SQL completo de una consola de base de datos: resaltado de PostgreSQL, las tablas y columnas del proyecto sugeridas mientras escribes, y Ctrl/⌘ + Enter para ejecutar lo seleccionado o todo el texto. Tiene su propia entrada, @sypnia/ui/react/code-editor, así que solo lo cargan las páginas que lo usan.

import { QueryEditor } from '@sypnia/ui/react/code-editor'; import { QueryEditorSkeleton } from '@sypnia/ui/react'

En esta página
  1. Una consulta con autocompletado
  2. Skeleton
  3. Cuándo usarlo
  4. Para todos
  5. Props

Una consulta con autocompletado

Código
import { lazy, Suspense, useState } from 'react';
import { Field, QueryEditorSkeleton, toast, Toaster } from '@sypnia/ui/react';

// CodeMirror loads only when the editor is shown: the skeleton holds its place meanwhile.
const QueryEditor = lazy(() => import('@sypnia/ui/react/code-editor').then((m) => ({ default: m.QueryEditor })));
const schema = { orders: ['id', 'customer_id', 'total', 'created_at'], customers: ['id', 'name', 'country'] };

// Type «from o» to see the tables, select a line and press Ctrl/⌘ + Enter to run only that.
export default function Query() {
  const [sql, setSql] = useState("select country, count(*)\nfrom orders\njoin customers on customers.id = orders.customer_id\nwhere created_at > now() - interval '7 days'\ngroup by 1\norder by 2 desc;");
  return (
    <>
      <Field label="Query">
        <Suspense fallback={<QueryEditorSkeleton rows={8} />}>
          <QueryEditor label="Query" rows={8} value={sql} onChange={setSql} schema={schema} placeholder="select …"
            onRun={(text) => toast(`Ran ${text.split('\n').length} lines`, { tone: 'ok' })} />
        </Suspense>
      </Field>
      <Toaster />
    </>
  );
}

Skeleton

Código
import { QueryEditorSkeleton } from '@sypnia/ui/react';

// While the editor's code arrives: the same box, the same height.
export default function Skeleton() {
  return <QueryEditorSkeleton rows={6} />;
}

Cuándo usarlo

Úsalo cuando

  • La página de SQL de una consola, donde se escriben y ejecutan consultas reales.
  • Cualquier lugar donde completar nombres de tablas y columnas ahorra escribir.

No lo uses cuando

  • Unas pocas líneas en un formulario: CodeEditor es más liviano y carga al instante.
  • Código que solo se lee: CodeBlock.

Para todos

  • Es un cuadro de texto con nombre y varias líneas; los lectores de pantalla lo leen como uno.
  • Ctrl/⌘ + Enter ejecuta; Escape cierra las sugerencias; Tab sale del editor.
  • Cárgalo de forma diferida y muestra QueryEditorSkeleton mientras tanto: la página no se mueve cuando llega.

Props

QueryEditorTipoPor defecto
labelobligatorio

Its name for screen readers.

string
onChangeobligatorio (value: string) => void
valueobligatorio string
className string
onRun

Ctrl/⌘ + Enter: runs the selection when there is one, otherwise the whole text.

((text: string) => void)
placeholder string
readOnly boolean
rows

Visible lines before it scrolls; it grows with its container when the container is taller.

number 10
schema

Tables and their columns, offered as the person types: `{ orders: ['id', 'total'] }`.

Record<string, string[]> {}
QueryEditorSkeletonTipoPor defecto
rows number 10
  1. Una consulta con autocompletado
  2. Skeleton
  3. Cuándo usarlo
  4. Para todos
  5. Props

    Busca páginas, secciones y términos