oakoss/ui
Components

Input Group

Text, icons and buttons inside an input's border.

SourceReact Aria

Installation

npx shadcn@latest add @oakoss/input-group

Usage

import { TextField } from 'react-aria-components';

import { FieldLabel } from '#/components/ui/inputs/field';
import { Input } from '#/components/ui/inputs/input';
import {
  InputGroup,
  InputGroupAddon,
  InputGroupText,
} from '#/components/ui/inputs/input-group';

<TextField>
  <FieldLabel>Price</FieldLabel>
  <InputGroup>
    <InputGroupAddon>
      <InputGroupText>$</InputGroupText>
    </InputGroupAddon>
    <Input />
  </InputGroup>
</TextField>;

Put the Input or Textarea directly inside the group: the group draws the border and focus outline, and the control drops its own. size on the group (sm, md, lg) sets the height. For text or an icon alone, Text Field's start and end do the same without composing. To attach a label outside the border, use Button Group with ButtonGroupText.

Examples

With a textarea

align="block-start" or "block-end" puts an addon above or below a Textarea, and the group grows with it.

Accessibility

React Aria renders a div for the group.

This component adds:

  • Role: a group with aria-label or aria-labelledby has the group role, and an unnamed one the presentation role, so it adds nothing for screen readers. Inside a React Aria field, leave it unnamed: the input keeps the field's name and description.
  • Addon text: an addon's text isn't part of the input's name, so a unit or prefix belongs in the label too ("Weight, in kilograms"), or use Text Field's end, which adds it to the description.
  • Focus: the outline wraps the whole group while the input has focus, and a focused addon button shows only its own.
  • Pointer: clicking an addon focuses the input, except on something interactive of its own. Buttons in an addon are small and ghost, with no 44px hit area, which would cover the input.
  • State: the border turns destructive when the input is invalid, and the group fades once when it's disabled.

The input keeps the browser's own keyboard behavior; React Aria adds no keys.

API

InputGroup

Extends React Aria's Group, and accepts all its props except role, which follows whether it's named.

Props

Prop

Type

Data attributes

AttributePresent when
data-slotAlways input-group
data-sizeAlways: sm, md or lg
data-disabledIts field is disabled
data-invalidIts field is invalid
data-hoveredA pointer hovers it
data-focus-withinSomething inside has focus
data-focus-visibleSomething inside has keyboard focus

InputGroupAddon

A div beside or around the control, and accepts all its props.

Props

Prop

Type

Data attributes

AttributePresent when
data-slotAlways input-group-addon
data-alignAlways: inline-start, inline-end, block-start, block-end

Other parts

PartExtendsdata-slot
InputGroupButtonOur Buttoninput-group-button
InputGroupText<span>input-group-text