Input group

Slant / Forms

An input with leading and trailing addons: icons, prefixes, units and buttons.

Demo

Usage

@tuttn/slant
import { Button, EyeIcon, Field, Input, InputGroup, MailIcon } from '@tuttn/slant'

// Text and icon addons: the group draws one field around them and the input.
<InputGroup leading="https://" trailing=".tuttn.io">
  <Input aria-label="Workspace address" placeholder="aftur" />
</InputGroup>
<InputGroup leading={<MailIcon size={15} />}>
  <Input type="email" aria-label="Email" />
</InputGroup>

// A button in an addon: one size down from the group (sm in md), ghost.
<InputGroup trailing={<Button variant="ghost" size="sm" icon aria-label="Show password"
  aria-pressed={shown} onClick={() => setShown(!shown)}><EyeIcon size={15} /></Button>}>
  <Input type={shown ? 'text' : 'password'} aria-label="Password" />
</InputGroup>

// In a Field the input inside takes the label, description and error, and
// the group turns red with it. The Input takes the group's size.
<Field invalid={tooHigh}>
  <Field.Label>Monthly budget</Field.Label>
  <InputGroup size="sm" leading="$" trailing="USD">
    <Input value={budget} onChange={event => setBudget(event.target.value)} />
  </InputGroup>
  <Field.Error>Budgets top out at $10,000.</Field.Error>
</Field>

Props

PropTypeDefaultDescription
leadingReactNode—An addon before the control: a muted icon or prefix ("https://"), or a small Button.
trailingReactNode—An addon after the control: a unit, a key hint, or a small Button.
size'sm' | 'md' | 'lg''md'Height on the control scale: 28, 34 or 40px at regular density. The Input inside takes the group's size.
childrenReactNode—The control: an Input, usually. Inside the group it drops its own border and takes the group's size; NumberInput and SearchField are built this way.
…restHTMLAttributes<HTMLDivElement>—The ref, className, style and other attributes go on the group's div.

The group draws the field: the border, the background, the accent border while anything inside has focus (and the focus ring while the input does), the danger border when the input is aria-invalid, and the dimmed look when it is disabled. Addons are muted text or icons at the field's padding; an addon holding a control, such as a small Button, hugs the edge instead.

A pointer-down on the group's padding or on a text addon puts focus in the input, so the whole field is one click target. Buttons and links in an addon keep their own clicks, and stay in the tab order after the input.

The group itself has no role or name: label the input, with a Field or aria-label. An addon's text isn't part of the input's name, so put a unit that matters ("USD", "kg") in the label or the description too.