SegmentedControl
Unas pocas opciones lado a lado en una barra, con exactamente una elegida: una vista, un período, un modo.
import { SegmentedControl } from '@sypnia/ui/react'
Vistas
Tres formas de ver las mismas filas.
Showing the rows as table.
Código
import { useState } from 'react';
import { SegmentedControl } from '@sypnia/ui/react';
// One is always chosen, and all the options are in sight.
export default function Views() {
const [view, setView] = useState<'table' | 'cards' | 'json'>('table');
return (
<div className="sy-stack">
<SegmentedControl label="View of the rows" value={view} onChange={setView} options={[
{ value: 'table', label: 'Table', icon: 'table' },
{ value: 'cards', label: 'Cards' },
{ value: 'json', label: 'JSON' },
]} />
<p className="sy-text--sm sy-text--soft">Showing the rows as {view === 'json' ? 'JSON' : view}.</p>
</div>
);
} Un período
La cifra sigue al período.
Requests in the period318,904
Código
import { useState } from 'react';
import { SegmentedControl, Stat } from '@sypnia/ui/react';
const requests = { '24h': '48,210', '7d': '318,904', '30d': '1,290,442', '90d': '3,804,117' };
// Four short options. On a phone they sit two by two.
export default function Period() {
const [period, setPeriod] = useState<keyof typeof requests>('7d');
return (
<div className="sy-stack">
<SegmentedControl label="Period" value={period} onChange={setPeriod} options={[
{ value: '24h', label: '24 hours' }, { value: '7d', label: '7 days' }, { value: '30d', label: '30 days' }, { value: '90d', label: '90 days' },
]} />
<Stat label="Requests in the period" value={requests[period]} />
</div>
);
} Dos modos
El campo de abajo cambia con el modo.
Código
import { useState } from 'react';
import { Field, Input, SegmentedControl, Textarea } from '@sypnia/ui/react';
// Two modes of the same task: what is under it changes at once.
export default function TwoModes() {
const [mode, setMode] = useState<'simple' | 'sql'>('simple');
return (
<div className="sy-stack">
<SegmentedControl label="How to write the rule" value={mode} onChange={setMode} options={[{ value: 'simple', label: 'Simple' }, { value: 'sql', label: 'SQL' }]} />
{mode === 'simple'
? <Field label="A person sees a row when the column"><Input machine defaultValue="customer_id" /></Field>
: <Field label="Condition"><Textarea machine defaultValue="customer_id = auth.user_id()" /></Field>}
</div>
);
} Todavía no disponible
Una opción desactivada sigue a la vista; su etiqueta dice por qué no se puede elegir.
Código
import { useState } from 'react';
import { SegmentedControl } from '@sypnia/ui/react';
// A mode that is coming stays in sight, but it cannot be chosen yet: its label says why.
export default function NotYet() {
const [provider, setProvider] = useState<'smtp' | 'resend' | 'sypnia_mail'>('smtp');
return (
<SegmentedControl label="Who sends the e-mail" value={provider} onChange={setProvider}
options={[{ value: 'smtp', label: 'SMTP' }, { value: 'resend', label: 'Resend' }, { value: 'sypnia_mail', label: 'Sypnia Mail · Soon', disabled: true }]} />
);
} Cuándo usarlo
Úsalo cuando
- De dos a cuatro opciones cortas, todas dignas de verse a la vez.
- La elección se aplica de inmediato y cambia cómo se muestra el contenido de al lado.
No lo uses cuando
- Cada opción muestra un contenido distinto con su propio panel: Tabs.
- Hay más de cuatro opciones o sus nombres son largos: NativeSelect.
- Es encendido o apagado: Switch.
- La elección se guarda con un formulario y necesita explicación: Radio, con descripciones.
Para todos
- Es un grupo de radios con nombre (
label): una parada de Tab, las flechas se mueven y eligen. - Cada opción se lee con su posición: «Tabla, 1 de 3».
- La opción elegida está en relieve y su texto es más fuerte; el foco muestra un anillo.
- En el celular las opciones van de a dos y miden al menos 44 px de alto.
Props
| SegmentedControl | Tipo | Por defecto |
|---|---|---|
labelobligatorio | string | |
onChangeobligatorio | (v: T) => void | |
optionsobligatorio`disabled` keeps a mode in sight that cannot be chosen yet («Sypnia Mail · Soon»): say why in its label or next to it. | { value: T; label: ReactNode; icon?: "function" | "error" | "replay" | "performance" | "uptime" | "traces" | "rage-click" | "dead-click" | "alert" | "summary" | "watch" | ... 353 more ... | undefined; disabled?: boolean | undefined; }[] | |
valueobligatorio | string |