TagInput
Una caja donde cada palabra escrita se vuelve un chip. Enter o una coma la agregan, Retroceso quita la última, y una lista pegada se divide en chips.
import { TagInput } from '@sypnia/ui/react'
Palabras en chips
Escribe una dirección y presiona Enter, o pega «a.com, b.com». Retroceso quita la última.
2 sites can call this project from a browser. Separate them with Enter or a comma.
Código
import { useState } from 'react';
import { Field, TagInput } from '@sypnia/ui/react';
export default function Basic() {
const [origins, setOrigins] = useState(['sypnia.com', 'app.sypnia.com']);
return (
<Field label="Allowed origins" hint={`${origins.length} sites can call this project from a browser. Separate them with Enter or a comma.`}>
<TagInput value={origins} onChange={setOrigins} placeholder="example.com" />
</Field>
);
} Ordenado y limitado
Cada etiqueta se pasa a minúscula con guiones antes de agregarse, y puede haber cinco como máximo.
Up to five. «Needs Review» becomes needs-review.
Código
import { Field, slugify, TagInput } from '@sypnia/ui/react';
export default function Tidy() {
return (
<Field label="Labels" hint="Up to five. «Needs Review» becomes needs-review.">
<TagInput defaultValue={['production', 'billing']} max={5} normalize={(tag) => slugify(tag).replace(/-+$/, '')} placeholder="Add a label" />
</Field>
);
} Estados
Vacío con un error que dice cómo arreglarlo, y desactivado.
Add at least one address, then press Enter.
Only the owner can change them.
Código
import { Field, FormGrid, TagInput } from '@sypnia/ui/react';
export default function States() {
return (
<FormGrid>
<Field label="Invite by mail" error="Add at least one address, then press Enter.">
<TagInput placeholder="name@company.com" />
</Field>
<Field label="Allowed origins" hint="Only the owner can change them.">
<TagInput disabled defaultValue={['sypnia.com', 'localhost:4410']} />
</Field>
</FormGrid>
);
} Cuándo usarlo
Úsalo cuando
- La persona escribe sus propios valores cortos, varios: direcciones permitidas, etiquetas, correos para invitar.
- La gente va a pegar listas copiadas de otro lado: comas, punto y coma y saltos de línea dividen.
No lo uses cuando
- Los valores vienen de una lista conocida: MultiSelect, así nadie tiene que recordar cómo se escriben.
- Es un solo texto largo: Textarea.
- El orden importa y los elementos se editan seguido: una lista con filas.
Para todos
- Es una caja de texto simple: Enter o una coma vuelven chip lo escrito; salir de la caja también, así no se pierde nada de lo escrito.
- Retroceso en una caja vacía quita el último chip. Cada chip también tiene su botón, llamado «Quitar» más la etiqueta.
- Después de cada cambio un mensaje discreto dice cuántas hay.
- Una etiqueta escrita dos veces se agrega una vez.
- Dentro de un Field toma la etiqueta, la pista y el error por sí solo; suelto, dale un
aria-label.
Props
| TagInput | Tipo | Por defecto |
|---|---|---|
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 | |
maxThe most tags it takes. | number | |
normalizeTidies each tag before it is added (lowercase, no spaces…). Return an empty text to refuse it. | ((tag: string) => string) | |
onChange | ((tags: string[]) => void) | |
placeholder | string | |
value | string[] |