Select
Una lista que se abre para elegir una opción. A diferencia de la lista propia del dispositivo, cada opción puede llevar un icono y una segunda línea que ayuda a elegir.
import { Select } from '@sypnia/ui/react'
Una opción
Cada región tiene una segunda línea con su código. La pista cambia con la elección.
Choose the one closest to your customers. It cannot be changed later.
Código
import { useState } from 'react';
import { Field, FormGrid, Select, type Option } from '@sypnia/ui/react';
const regions: Option[] = [
{ value: 'us-east-1', label: 'Virginia', description: 'us-east-1 · North America' },
{ value: 'sa-east-1', label: 'São Paulo', description: 'sa-east-1 · South America' },
{ value: 'eu-central-1', label: 'Frankfurt', description: 'eu-central-1 · Europe' },
{ value: 'eu-west-1', label: 'Dublin', description: 'eu-west-1 · Europe' },
{ value: 'ap-southeast-1', label: 'Singapore', description: 'ap-southeast-1 · Asia', disabled: true },
];
export default function Basic() {
const [region, setRegion] = useState<string | null>(null);
return (
<FormGrid>
<Field label="Region" hint={region ? `The database will live in ${region}.` : 'Choose the one closest to your customers. It cannot be changed later.'}>
<Select options={regions} value={region} onChange={setRegion} placeholder="Choose a region" />
</Field>
</FormGrid>
);
} Opciones con icono
Un rol para un nuevo miembro del proyecto. El icono de la opción elegida queda en el control.
It can be changed later from the team page.
Código
import { Field, FormGrid, Select, type Option } from '@sypnia/ui/react';
const roles: Option[] = [
{ value: 'owner', label: 'Owner', description: 'Everything, including billing and deleting the project.', icon: 'key' },
{ value: 'editor', label: 'Editor', description: 'Changes data and tables, not the team.', icon: 'edit' },
{ value: 'viewer', label: 'Viewer', description: 'Reads data, and nothing else.', icon: 'watch' },
];
export default function WithIcons() {
return (
<FormGrid>
<Field label="Role" hint="It can be changed later from the team page.">
<Select options={roles} defaultValue="editor" />
</Field>
</FormGrid>
);
} Tamaños y estados
Pequeño para barras de herramientas, con un error que dice cómo arreglarlo, y desactivado.
Choose who owns this key: they get the notice when it expires.
The plan decides this value.
Código
import { Field, FormGrid, Select, type Option } from '@sypnia/ui/react';
const periods: Option[] = [
{ value: '1', label: 'Last 24 hours' },
{ value: '7', label: 'Last 7 days' },
{ value: '30', label: 'Last 30 days' },
];
const members: Option[] = [
{ value: 'laura', label: 'Laura Méndez', description: 'laura@northwind.dev' },
{ value: 'diego', label: 'Diego Ruiz', description: 'diego@northwind.dev' },
];
export default function States() {
return (
<FormGrid>
<Field label="Small, for toolbars">
<Select size="sm" options={periods} defaultValue="7" />
</Field>
<Field label="Owner of the key" error="Choose who owns this key: they get the notice when it expires.">
<Select options={members} placeholder="Choose a member" />
</Field>
<Field label="Backups" hint="The plan decides this value.">
<Select disabled options={[{ value: '7', label: 'Kept for 7 days' }]} defaultValue="7" />
</Field>
</FormGrid>
);
} Cuándo usarlo
Úsalo cuando
- Una opción entre cinco y quince que necesitan un icono o una línea de explicación: una región, un rol, un plan.
- La elección se guarda con el resto de un formulario.
smen barras de herramientas y filtros.
No lo uses cuando
- Hay de dos a cuatro opciones y espacio para mostrarlas: RadioGroup o RadioCards las dejan a la vista.
- La lista es larga y la persona sabe lo que busca: Combobox la deja escribir.
- Las opciones son palabras simples y el formulario se usa sobre todo en celulares: NativeSelect abre el selector que la gente ya conoce.
- Se pueden elegir varias: MultiSelect o CheckboxGroup.
Para todos
- Enter, Espacio o la flecha abajo abren la lista; las flechas se mueven; escribir salta a la opción que empieza con esas letras; Enter elige; Escape cierra y devuelve el foco.
- Se anuncia como un botón que abre una lista (
listboxconoption), con el valor elegido como parte de su nombre. - Dentro de un Field toma la etiqueta, la pista y el error por sí solo; suelto, dale un
aria-label. - La lista flota en la capa superior, es tan ancha como el control y nunca más ancha que la ventana.
Props
| Select | 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 | |
idInside a Field it comes from the Field. | string | |
invalidMarks the value as wrong. Inside a `<Field error>` it is set for you. | boolean | |
nameThe name of the value when it travels in a form. | string | |
onChange | ((value: string) => void) | |
placeholderShown while nothing is chosen. | string | |
size | "sm" | "md" | "lg" | |
valueThe chosen value. Leave it out (and use `defaultValue`) to let the component keep it. | string | null |