Field
Junta rótulo, controle, descrição e erro. O rótulo fica 8px acima do controle e a descrição 8px abaixo, tudo com texto de 16px.
Instalação
npx shadcn@latest add @sevenone/fieldUso
import {
Field,
FieldContent,
FieldDescription,
FieldError,
FieldGroup,
FieldLabel,
FieldLegend,
FieldSeparator,
FieldSet,
} from "@/components/ui/field"Exemplos
Padrão
We will never share your email.
<Field>
<FieldLabel htmlFor="email" required>Email</FieldLabel>
<Input id="email" type="email" placeholder="m@example.com" aria-describedby="email-description" />
<FieldDescription id="email-description">We will never share your email.</FieldDescription>
</Field>Inválido
Coloque data-invalid no Field e aria-invalid no controle. O FieldError é anunciado na hora.
Enter a valid email address.
<Field data-invalid>
<FieldLabel htmlFor="email">Email</FieldLabel>
<Input id="email" type="email" aria-invalid aria-describedby="email-error" />
<FieldError id="email-error">Enter a valid email address.</FieldError>
</Field>Checkbox
orientation="horizontal" coloca o controle ao lado do rótulo.
One short email a month. Unsubscribe any time.
<Field orientation="horizontal">
<Checkbox id="news" defaultChecked aria-describedby="news-description" />
<FieldContent>
<FieldLabel htmlFor="news">Email me about new components</FieldLabel>
<FieldDescription id="news-description">One short email a month. Unsubscribe any time.</FieldDescription>
</FieldContent>
</Field>Conjunto de campos
FieldSet e FieldLegend agrupam campos relacionados, e os leitores de tela leem a legenda junto com cada um.
<FieldSet>
<FieldLegend>Shipping address</FieldLegend>
<FieldDescription>Where we send your order.</FieldDescription>
<FieldGroup>
<Field>
<FieldLabel htmlFor="street">Street</FieldLabel>
<Input id="street" autoComplete="street-address" />
</Field>
<div className="grid grid-cols-2 gap-4">
<Field>
<FieldLabel htmlFor="city">City</FieldLabel>
<Input id="city" autoComplete="address-level2" />
</Field>
<Field>
<FieldLabel htmlFor="zip">Postal code</FieldLabel>
<Input id="zip" autoComplete="postal-code" />
</Field>
</div>
<FieldSeparator />
<Field orientation="horizontal">
<Checkbox id="billing" defaultChecked />
<FieldLabel htmlFor="billing">Same as billing address</FieldLabel>
</Field>
</FieldGroup>
</FieldSet>Acessibilidade
- O FieldLabel é um rótulo de verdade: ligue ao controle com htmlFor e id.
- Ligue o FieldDescription e o FieldError ao controle com aria-describedby.
- O FieldError tem role=alert e a cor de texto destrutiva (7:1); a mensagem é texto, nunca só cor.
- Use FieldSet e FieldLegend em grupos de checkboxes, rádios ou campos relacionados.