Ir al contenido

CheckboxGroup

Varias opciones de sí o no bajo una pregunta. La persona marca las que quiera, o ninguna.

import { CheckboxGroup, CheckboxGroupSkeleton } from '@sypnia/ui/react'

En esta página
  1. Varias de unas pocas
  2. En una fila
  3. Estados
  4. Skeleton
  5. Cuándo usarlo
  6. Para todos
  7. Props

Varias de unas pocas

Lo que puede hacer una nueva clave de API. La pista cuenta lo marcado.

1 of 4 permissions granted. Give a key only what it needs.

Código
import { useState } from 'react';
import { CheckboxGroup, Field, type Option } from '@sypnia/ui/react';

const permissions: Option[] = [
  { value: 'read', label: 'Read rows', description: 'Queries and exports.' },
  { value: 'write', label: 'Write rows', description: 'Insert, update and delete.' },
  { value: 'schema', label: 'Change tables', description: 'Create, alter and drop tables and columns.' },
  { value: 'billing', label: 'See billing', description: 'Only the owner can grant this.', disabled: true },
];

export default function Basic() {
  const [granted, setGranted] = useState(['read']);
  return (
    <Field label="This key can" hint={`${granted.length} of 4 permissions granted. Give a key only what it needs.`}>
      <CheckboxGroup options={permissions} value={granted} onChange={setGranted} />
    </Field>
  );
}

En una fila

Las etiquetas cortas pueden ir lado a lado; pasan de línea cuando no hay espacio.

Código
import { CheckboxGroup, Field } from '@sypnia/ui/react';

const days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'].map((d) => ({ value: d.toLowerCase(), label: d }));

export default function Horizontal() {
  return (
    <Field label="Back up on">
      <CheckboxGroup orientation="horizontal" options={days} defaultValue={['mon', 'wed', 'fri']} />
    </Field>
  );
}

Estados

Con un error que dice cómo arreglarlo, y desactivado.

Tick at least one channel, or nobody will hear about failures.

Alerts are paused while the project sleeps.

Código
import { CheckboxGroup, Field, FormGrid } from '@sypnia/ui/react';

const channels = [{ value: 'mail', label: 'Mail' }, { value: 'slack', label: 'Slack' }, { value: 'webhook', label: 'Webhook' }];

export default function States() {
  return (
    <FormGrid>
      <Field label="Send alerts by" error="Tick at least one channel, or nobody will hear about failures.">
        <CheckboxGroup options={channels} />
      </Field>
      <Field label="Send alerts by" hint="Alerts are paused while the project sleeps.">
        <CheckboxGroup disabled options={channels} defaultValue={['mail']} />
      </Field>
    </FormGrid>
  );
}

Skeleton

La pieza real y su skeleton: la misma caja, así nada se mueve cuando llegan los datos.

Código
import type { CSSProperties } from 'react';
import { CheckboxGroup, CheckboxGroupSkeleton } from '@sypnia/ui/react';

// Options that come from the server. The skeleton has the same box as the real one, so nothing moves when the data arrives.
export default function Skeleton() {
  return (
    <div className="sy-grid" style={{ '--min': '16rem' } as CSSProperties}>
      <CheckboxGroup aria-label="Notify me about" options={[{ value: 'deploys', label: 'Deploys' }, { value: 'errors', label: 'Errors' }, { value: 'billing', label: 'Billing' }]} defaultValue={['errors']} />
      <CheckboxGroupSkeleton items={3} />
    </div>
  );
}

Cuándo usarlo

Úsalo cuando

  • Hasta unas seis opciones que no se excluyen, todas dignas de verse a la vez: de qué recibir avisos, qué puede hacer una clave.
  • Cada opción puede llevar una línea que la explica.

No lo uses cuando

  • Solo se puede elegir una: RadioGroup.
  • Hay muchas opciones: MultiSelect mantiene corto el formulario.
  • Es un solo sí o no: Checkbox, o Switch cuando se aplica de inmediato.

Para todos

  • Cada opción es una casilla nativa: Tab se mueve entre ellas y Espacio marca.
  • El grupo se anuncia con la pregunta como nombre (role="group"), así cada opción se oye en su contexto.
  • Toda la línea, texto incluido, marca la casilla. En pantallas táctiles cada línea mide 44 px de alto.
  • Dentro de un Field toma la etiqueta, la pista y el error por sí solo; suelto, dale un aria-label.

Props

CheckboxGroupTipoPor defecto
optionsobligatorio Option[]
aria-describedby string
aria-label

The name for screen readers when there is no Field around it.

string
aria-labelledby string
className string
defaultValue string[]
disabled boolean
id

Inside a Field it comes from the Field.

string
invalid

Marks the value as wrong. Inside a `<Field error>` it is set for you.

boolean
name

The name of the value when it travels in a form.

string
onChange ((values: string[]) => void)
orientation

vertical: one under another (the default, easier to scan). horizontal: in a row that wraps, for short labels.

"horizontal" | "vertical"
value string[]
CheckboxGroupSkeletonTipoPor defecto
items number 3
orientation "horizontal" | "vertical"
  1. Varias de unas pocas
  2. En una fila
  3. Estados
  4. Skeleton
  5. Cuándo usarlo
  6. Para todos
  7. Props

    Busca páginas, secciones y términos