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'
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
| QueryEditor | Tipo | Por defecto |
|---|---|---|
labelobligatorioIts name for screen readers. | string | |
onChangeobligatorio | (value: string) => void | |
valueobligatorio | string | |
className | string | |
onRunCtrl/⌘ + Enter: runs the selection when there is one, otherwise the whole text. | ((text: string) => void) | |
placeholder | string | |
readOnly | boolean | |
rowsVisible lines before it scrolls; it grows with its container when the container is taller. | number | 10 |
schemaTables and their columns, offered as the person types: `{ orders: ['id', 'total'] }`. | Record<string, string[]> | {} |
| QueryEditorSkeleton | Tipo | Por defecto |
|---|---|---|
rows | number | 10 |