MultiSelect
Varias opciones de una lista. Cada opción elegida se vuelve un chip dentro del control, con una cruz para quitarla, y escribir reduce la lista.
import { MultiSelect } from '@sypnia/ui/react'
En esta página
Elegir varias
Las columnas que van en una exportación. Escribe para reducir la lista; Retroceso quita el último chip.
3 of 7 columns go into the file, in this order.
Código
import { useState } from 'react';
import { Field, MultiSelect, type Option } from '@sypnia/ui/react';
const columns: Option[] = [
{ value: 'id', label: 'id', description: 'uuid' },
{ value: 'email', label: 'email', description: 'text' },
{ value: 'full_name', label: 'full_name', description: 'text' },
{ value: 'plan', label: 'plan', description: 'text' },
{ value: 'country', label: 'country', description: 'text' },
{ value: 'created_at', label: 'created_at', description: 'timestamp' },
{ value: 'last_seen_at', label: 'last_seen_at', description: 'timestamp' },
];
export default function Basic() {
const [chosen, setChosen] = useState(['id', 'email', 'created_at']);
return (
<Field label="Columns to export" hint={`${chosen.length} of ${columns.length} columns go into the file, in this order.`}>
<MultiSelect options={columns} value={chosen} onChange={setChosen} placeholder="Choose columns" />
</Field>
);
} Estados
Vacío con un error que dice cómo arreglarlo, y desactivado.
Choose at least one event, or the webhook will never fire.
Only the owner can change a webhook.
Código
import { Field, FormGrid, MultiSelect, type Option } from '@sypnia/ui/react';
const events: Option[] = [
{ value: 'row.created', label: 'row.created' },
{ value: 'row.updated', label: 'row.updated' },
{ value: 'row.deleted', label: 'row.deleted' },
{ value: 'backup.finished', label: 'backup.finished' },
];
export default function States() {
return (
<FormGrid>
<Field label="Events to send" error="Choose at least one event, or the webhook will never fire.">
<MultiSelect options={events} placeholder="Choose events" />
</Field>
<Field label="Events to send" hint="Only the owner can change a webhook.">
<MultiSelect disabled options={events} defaultValue={['row.created', 'row.deleted']} />
</Field>
</FormGrid>
);
} Cuándo usarlo
Úsalo cuando
- Varias opciones entre muchas: las columnas de una exportación, los miembros de un grupo, los eventos de un webhook.
- Lo elegido debe quedar a la vista mientras la persona sigue eligiendo.
No lo uses cuando
- Hay seis opciones o menos: un CheckboxGroup las muestra todas sin abrir nada.
- La persona escribe sus propias palabras en vez de elegir de una lista: TagInput.
- Solo se puede elegir una: Select o Combobox.
Para todos
- Escribir filtra; las flechas recorren la lista; Enter agrega o quita la opción enfocada y la lista sigue abierta; Escape la cierra.
- Retroceso en una caja vacía quita el último chip. Cada chip tiene su propio botón, llamado «Quitar» más la opción.
- Las opciones elegidas se marcan como seleccionadas en la lista, así la lista sola dice todo el estado.
- Dentro de un Field toma la etiqueta, la pista y el error por sí solo; suelto, dale un
aria-label.
Props
| MultiSelect | Tipo | Por defecto |
|---|---|---|
optionsobligatorio | Option[] | |
aria-describedby | string | |
aria-labelThe name for screen readers when there is no Field around it. | string | |
aria-labelledby | string | |
className | string | |
defaultValue | string[] | |
disabled | boolean | |
emptyText | ReactNode | |
idInside a Field it comes from the Field. | string | |
invalidMarks the value as wrong. Inside a `<Field error>` it is set for you. | boolean | |
name | string | |
onChange | ((values: string[]) => void) | |
placeholder | string | |
value | string[] |