Ir al contenido

Field

Pone un nombre encima de un control, ayuda debajo y el error al lado cuando algo está mal. Todo control de un formulario va dentro de uno, así nadie tiene que adivinar qué escribir.

import { Field, FieldSkeleton } from '@sypnia/ui/react'

En esta página
  1. Etiqueta y pista
  2. Error
  3. Alrededor de cualquier control
  4. Skeleton
  5. Cuándo usarlo
  6. Para todos
  7. Props

Etiqueta y pista

Un nombre, y una ayuda que queda visible. El campo opcional lo dice junto a su etiqueta.

Shown in the sidebar and in invitations.

Invoices go here. Without it, they go to the owner.

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

export default function LabelAndHint() {
  return (
    <FormGrid>
      <Field label="Project name" hint="Shown in the sidebar and in invitations.">
        <Input defaultValue="Storefront" />
      </Field>
      <Field label="Billing email" optional hint="Invoices go here. Without it, they go to the owner.">
        <Input type="email" placeholder="billing@northwind.io" />
      </Field>
    </FormGrid>
  );
}

Error

Escribe en el campo: el error aparece mientras el nombre no es válido y se va cuando lo es.

Use only lowercase letters, numbers and underscores, starting with a letter: order_items.

It becomes part of the API address.

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

// Type a capital letter or a space: the error says what is wrong and how to fix it.
export default function WithError() {
  const [name, setName] = useState('Order Items');
  const error = name === '' ? 'Write a name for the table.'
    : /^[a-z][a-z0-9_]*$/.test(name) ? undefined
    : 'Use only lowercase letters, numbers and underscores, starting with a letter: order_items.';
  return (
    <FormGrid>
      <Field label="Table name" hint="It becomes part of the API address." error={error}>
        <Input machine value={name} onChange={(e) => setName(e.target.value)} />
      </Field>
    </FormGrid>
  );
}

Alrededor de cualquier control

Un select, un campo con unidad y un área de texto, con la misma etiqueta, pista y error.

Where the data is stored. It cannot be changed later.

per minute

Write a number between 60 and 6,000.

Código
import { Field, FormGrid, InputGroup, Input, NativeSelect, Textarea } from '@sypnia/ui/react';

// The same label, hint and error work around any control.
export default function AnyControl() {
  return (
    <FormGrid>
      <Field label="Region" hint="Where the data is stored. It cannot be changed later.">
        <NativeSelect defaultValue="eu-central">
          <option value="eu-central">Frankfurt</option>
          <option value="us-east">Virginia</option>
          <option value="sa-east">São Paulo</option>
        </NativeSelect>
      </Field>
      <Field label="Request limit" error="Write a number between 60 and 6,000.">
        <InputGroup end="per minute"><Input type="number" defaultValue={12000} /></InputGroup>
      </Field>
      <Field label="Notes" optional wide>
        <Textarea placeholder="What is this project for?" />
      </Field>
    </FormGrid>
  );
}

Skeleton

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

Shown to everyone in the team.

Código
import type { CSSProperties } from 'react';
import { Field, FieldSkeleton, Input } from '@sypnia/ui/react';

// A field whose value is loading: the same box, the size of its control. 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}>
      <Field label="Project name" hint="Shown to everyone in the team."><Input defaultValue="storefront" /></Field>
      <FieldSkeleton hint />
    </div>
  );
}

Cuándo usarlo

Úsalo cuando

  • Cualquier control de un formulario: Input, Textarea, NativeSelect, InputGroup, PasswordInput, CopyField.
  • La persona necesita saber el formato antes de escribir: escríbelo en hint, que siempre se ve.
  • Algo está mal: pasa error con lo que pasó y cómo arreglarlo. Su presencia marca el control como inválido.

No lo uses cuando

  • Un solo Checkbox, Radio o Switch: llevan su propia etiqueta y descripción.
  • Un grupo de opciones con una pregunta: eso es un Fieldset.
  • La etiqueta quedaría oculta y un texto de ejemplo la reemplazaría: el texto de ejemplo desaparece apenas la persona escribe.

Para todos

  • La etiqueta está unida al control: presionarla lleva el foco allí, y los lectores de pantalla la dicen.
  • La pista y el error se unen con aria-describedby; el error se lee primero. El control recibe aria-invalid.
  • Un error nunca es solo color: tiene icono y palabras, y dice cómo arreglar el problema.
  • Obligatorio es lo normal y no se marca. Marca los pocos campos que son optional.

Props

FieldTipoPor defecto
labelobligatorio ReactNode
className string
error

What is wrong and how to fix it. Its presence marks the control as invalid.

ReactNode
hint

Help that is always visible: what to write, in what format.

ReactNode
optional

Adds «optional» beside the label. Required is the default and is not marked.

boolean
wide

Spans both columns of a FormGrid.

boolean
FieldSkeletonTipoPor defecto
hint boolean
rows number
size "sm" | "md" | "lg" md
  1. Etiqueta y pista
  2. Error
  3. Alrededor de cualquier control
  4. Skeleton
  5. Cuándo usarlo
  6. Para todos
  7. Props

    Busca páginas, secciones y términos