Skip to content

FormGrid

Lays the fields of a form in two columns on wide screens and in one on phones, so short fields pair up and long ones take the whole row.

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

On this page
  1. Two columns and a wide field
  2. A whole form
  3. When to use it
  4. For everyone
  5. Props

Two columns and a wide field

Narrow the window: at phone width everything is one column.

Code
import { Field, FormGrid, Input, NativeSelect, Textarea } from '@sypnia/ui/react';

// Short fields pair up; a long one is marked `wide` and takes the whole row. On a phone there is one column.
export default function TwoColumns() {
  return (
    <FormGrid>
      <Field label="First name"><Input autoComplete="given-name" defaultValue="Laura" /></Field>
      <Field label="Last name"><Input autoComplete="family-name" defaultValue="Méndez" /></Field>
      <Field label="Work email"><Input type="email" autoComplete="email" defaultValue="laura@northwind.io" /></Field>
      <Field label="Role">
        <NativeSelect defaultValue="editor"><option value="owner">Owner</option><option value="editor">Editor</option><option value="viewer">Viewer</option></NativeSelect>
      </Field>
      <Field label="Note for the invitation" optional wide><Textarea placeholder="Welcome to the Storefront project." /></Field>
    </FormGrid>
  );
}

A whole form

Press «Create project» with the name empty, then with a name: error, working, done.

It cannot be changed later.

Code
import { useState, type FormEvent } from 'react';
import { Actions, Alert, Button, Field, FormGrid, Input, NativeSelect } from '@sypnia/ui/react';

// Press «Create project» with the name empty.
export default function WholeForm() {
  const [name, setName] = useState('');
  const [state, setState] = useState<'editing' | 'missing' | 'saving' | 'saved'>('editing');
  const submit = (e: FormEvent) => {
    e.preventDefault();
    if (!name.trim()) return setState('missing');
    setState('saving');
    setTimeout(() => setState('saved'), 1500);
  };
  return (
    <form className="sy-stack" onSubmit={submit} noValidate>
      {state === 'saved' && <Alert tone="ok" title={`The project ${name} is ready`}>Create its first table to start storing data.</Alert>}
      <FormGrid>
        <Field label="Project name" error={state === 'missing' ? 'Write a name for the project.' : undefined}>
          <Input value={name} onChange={(e) => { setName(e.target.value); setState('editing'); }} />
        </Field>
        <Field label="Region" hint="It cannot be changed later.">
          <NativeSelect defaultValue="eu-central"><option value="eu-central">Frankfurt</option><option value="us-east">Virginia</option></NativeSelect>
        </Field>
      </FormGrid>
      <Actions>
        <Button type="reset" onClick={() => { setName(''); setState('editing'); }}>Clear</Button>
        <Button variant="primary" type="submit" busy={state === 'saving'}>{state === 'saving' ? 'Creating…' : 'Create project'}</Button>
      </Actions>
    </form>
  );
}

When to use it

Use it when

  • A form has several short fields that go well in pairs: first and last name, host and port.
  • Mark a Field wide when its content needs the full row.

Do not use it when

  • The form has two or three fields: one column reads faster.
  • The fields are unrelated and a pair would suggest they belong together.
  • You want three or more columns: forms are read top to bottom, and wide rows are easy to skip.

For everyone

  • The reading order and the Tab order are the same: left to right, then down.
  • On phones there is one column, so nothing scrolls sideways and every field is full width.
  • It adds no role: group related fields with a Fieldset when the group has a name.

Props

FormGridTypeDefault
className string
FieldTypeDefault
labelrequired 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
  1. Two columns and a wide field
  2. A whole form
  3. When to use it
  4. For everyone
  5. Props

    Search pages, sections and terms