# Input Group

> A text input that shares one surface with a prefix, suffix or action.

```tsx title="hero.tsx"
import { Field, FieldDescription, FieldLabel } from '@langgenius/dify-ui/field'
import { InputGroup, InputGroupAddon, InputGroupInput } from '@langgenius/dify-ui/input-group'

export function InputGroupHeroDemo() {
  return (
    <Field name="repositoryUrl" className="w-full max-w-80">
      <FieldLabel>Repository URL</FieldLabel>
      <InputGroup>
        <InputGroupInput
          placeholder="github.com/langgenius/dify"
          inputMode="url"
          autoCapitalize="none"
          autoComplete="off"
          autoCorrect="off"
          spellCheck={false}
        />
        <InputGroupAddon className="pe-0">https://</InputGroupAddon>
      </InputGroup>
      <FieldDescription>The https:// prefix is not part of the value.</FieldDescription>
    </Field>
  )
}
```

## Usage guidelines

- Use `InputGroup` when one text input shares its visual surface with a prefix, suffix or action. Use the standalone [`Input`](/components/input.md) when nothing shares the surface, and do not absolutely position content over an `Input` to recreate one.
- Place the input before every addon in the DOM. The input is the primary control and addons are read or reached after it; only decorative `aria-hidden` content may come first. Set `align="inline-start"` (the default) or `align="inline-end"` on the addon for its visual position; it does not change reading or focus order.
- Give `InputGroupInput` an accessible name from a visible label, `aria-label` or `aria-labelledby`. Wrap the group in [`Field`](/components/field.md) with a `FieldLabel` for a form row; `Field` also supplies the name, validation, description and error.
- Field state reaches `InputGroupInput`, and the group derives its invalid, disabled, read-only and focus visuals from that direct input. Do not repeat those states on addons or restyle them on the group.
- Treat addon text and icons as supporting content, and mark decorative icons `aria-hidden`. Put interactive content in a `Button`, `IconButton` or link instead of adding click or keyboard handlers to the addon. Those controls keep their own focus and events.
- Pressing a non-interactive part of the group focuses the input. Pressing an interactive addon targets that control and leaves focus alone, and portalled addon content does not refocus the input. Call `preventDefault()` in the group's `onMouseDown` to opt out.
- The group supports the default input size. A new size belongs on `InputGroup`, which owns the shared surface and addon layout, rather than on the input and addons separately.

## Anatomy

```tsx
import { InputGroup, InputGroupAddon, InputGroupInput } from '@langgenius/dify-ui/input-group'
```

```tsx
<InputGroup>
  <InputGroupInput />
  <InputGroupAddon />
</InputGroup>
```

`InputGroup` owns the border, background, focus ring and the non-interactive pointer surface. `InputGroupInput` owns the native input and its value. `InputGroupAddon` owns the layout of its supporting content. The direct input can also be another native input that owns its value and states, as in [`PasswordField`](/components/password-field.md).

## Examples

### Decorative icon

The search icon is decorative and renders visually first through the default `inline-start` alignment while the input stays first in the DOM. The search box filters the list it sits above and has no room for a visible label, so the input takes `aria-label`; the `<search>` element marks it as a search landmark.

```tsx title="icon.tsx"
import { InputGroup, InputGroupAddon, InputGroupInput } from '@langgenius/dify-ui/input-group'
import * as React from 'react'

const knowledgeBases = [
  { name: 'Product FAQ', docs: '24 docs' },
  { name: 'Refund policy', docs: '12 docs' },
  { name: 'API reference', docs: '48 docs' },
  { name: 'Release notes', docs: '19 docs' },
] as const

export function InputGroupIconDemo() {
  const [query, setQuery] = React.useState('')
  const normalized = query.trim().toLowerCase()
  const filtered = normalized
    ? knowledgeBases.filter((item) => item.name.toLowerCase().includes(normalized))
    : knowledgeBases

  return (
    <div className="flex w-full max-w-80 flex-col gap-3">
      <search>
        <InputGroup>
          <InputGroupInput
            type="search"
            aria-label="Search knowledge bases"
            placeholder="e.g. Product FAQ"
            value={query}
            onValueChange={setQuery}
            autoCapitalize="none"
            autoComplete="off"
            autoCorrect="off"
            spellCheck={false}
            enterKeyHint="search"
          />
          <InputGroupAddon>
            <span
              aria-hidden="true"
              className="i-ri-search-line size-4 text-components-input-text-placeholder"
            />
          </InputGroupAddon>
        </InputGroup>
      </search>
      {filtered.length > 0 ? (
        <ul className="divide-y divide-divider-subtle">
          {filtered.map((item) => (
            <li key={item.name} className="flex items-center justify-between gap-3 py-2">
              <span className="min-w-0 truncate system-sm-medium text-text-primary">
                {item.name}
              </span>
              <span className="shrink-0 system-xs-regular text-text-tertiary tabular-nums">
                {item.docs}
              </span>
            </li>
          ))}
        </ul>
      ) : (
        <p className="py-3 text-center system-xs-regular text-text-tertiary">
          No matching knowledge bases.
        </p>
      )}
    </div>
  )
}
```

### Suffix

`align="inline-end"` places a fixed suffix after the value. Clicking the suffix focuses the input. For numeric values with units and steppers, use [`NumberField`](/components/number-field.md) instead.

```tsx title="suffix.tsx"
import { Field, FieldDescription, FieldLabel } from '@langgenius/dify-ui/field'
import { InputGroup, InputGroupAddon, InputGroupInput } from '@langgenius/dify-ui/input-group'

export function InputGroupSuffixDemo() {
  return (
    <Field name="documentFileName" className="w-full max-w-80">
      <FieldLabel>Document file name</FieldLabel>
      <InputGroup>
        <InputGroupInput
          defaultValue="refund-policy"
          placeholder="product-faq"
          autoCapitalize="none"
          autoComplete="off"
          autoCorrect="off"
          spellCheck={false}
        />
        <InputGroupAddon align="inline-end">.md</InputGroupAddon>
      </InputGroup>
      <FieldDescription>Saved as a Markdown file in the knowledge base.</FieldDescription>
    </Field>
  )
}
```

### Actions

Interactive addons are real controls: a text `Button` copies a read-only value, and an `IconButton` opens a `Menu` through its trigger's `render`. Neither moves focus to the input when pressed.

```tsx title="actions.tsx"
import { Button } from '@langgenius/dify-ui/button'
import { Field, FieldLabel } from '@langgenius/dify-ui/field'
import { IconButton } from '@langgenius/dify-ui/icon-button'
import { InputGroup, InputGroupAddon, InputGroupInput } from '@langgenius/dify-ui/input-group'
import { Menu, MenuContent, MenuItem, MenuTrigger } from '@langgenius/dify-ui/menu'
import * as React from 'react'

const endpoint = 'https://api.dify.ai/v1'
const defaultVariableName = 'summary'

export function InputGroupActionsDemo() {
  const [copyCount, setCopyCount] = React.useState(0)
  const [variableName, setVariableName] = React.useState(defaultVariableName)
  const copied = copyCount > 0

  React.useEffect(() => {
    if (copyCount === 0) return
    const timer = window.setTimeout(() => setCopyCount(0), 2000)
    return () => window.clearTimeout(timer)
  }, [copyCount])

  async function handleCopy() {
    try {
      await navigator.clipboard.writeText(endpoint)
      setCopyCount((count) => count + 1)
    } catch {
      // Clipboard access can be denied in insecure contexts.
    }
  }

  return (
    <div className="flex w-full max-w-80 flex-col gap-4">
      <Field name="apiEndpoint">
        <FieldLabel>API endpoint</FieldLabel>
        <InputGroup>
          <InputGroupInput
            defaultValue={endpoint}
            placeholder="https://api.example.com/v1"
            readOnly
          />
          <InputGroupAddon align="inline-end">
            <Button size="small" variant="secondary" onClick={() => void handleCopy()}>
              <span className="inline-grid place-items-center">
                <span
                  aria-hidden={copied || undefined}
                  className={
                    copied ? 'invisible col-start-1 row-start-1' : 'col-start-1 row-start-1'
                  }
                >
                  Copy
                </span>
                <span
                  aria-hidden={!copied || undefined}
                  className={
                    copied
                      ? 'col-start-1 row-start-1 inline-flex items-center gap-1'
                      : 'invisible col-start-1 row-start-1 inline-flex items-center gap-1'
                  }
                >
                  <span aria-hidden="true" className="i-ri-check-line size-3.5 text-text-success" />
                  Copied
                </span>
              </span>
            </Button>
          </InputGroupAddon>
        </InputGroup>
        <span role="status" className="sr-only">
          {copied ? 'Copied API endpoint' : ''}
        </span>
      </Field>
      <Field name="variableName">
        <FieldLabel>Output variable</FieldLabel>
        <InputGroup>
          <InputGroupInput
            placeholder="answer_text"
            value={variableName}
            onValueChange={setVariableName}
            autoCapitalize="none"
            autoComplete="off"
            autoCorrect="off"
            spellCheck={false}
          />
          <InputGroupAddon align="inline-end">
            <Menu>
              <MenuTrigger
                render={
                  <IconButton
                    aria-label="Output variable actions"
                    className="active:bg-state-base-active data-popup-open:bg-state-base-hover data-popup-open:text-text-secondary"
                  >
                    <span aria-hidden="true" className="i-ri-more-fill size-4" />
                  </IconButton>
                }
              />
              <MenuContent align="end">
                <MenuItem onClick={() => setVariableName('context_summary')}>
                  Use suggested name
                </MenuItem>
                <MenuItem
                  disabled={variableName === defaultVariableName}
                  onClick={() => setVariableName(defaultVariableName)}
                >
                  Reset to default
                </MenuItem>
              </MenuContent>
            </Menu>
          </InputGroupAddon>
        </InputGroup>
      </Field>
    </div>
  )
}
```

### Disabled

Setting `disabled` on `Field` disables `InputGroupInput`, and the group dims the surface and its non-interactive addons together.

```tsx title="disabled.tsx"
import { Field, FieldDescription, FieldLabel } from '@langgenius/dify-ui/field'
import { InputGroup, InputGroupAddon, InputGroupInput } from '@langgenius/dify-ui/input-group'

export function InputGroupDisabledDemo() {
  return (
    <Field name="workspaceSlug" disabled className="w-full max-w-80">
      <FieldLabel>Workspace domain</FieldLabel>
      <InputGroup>
        <InputGroupInput defaultValue="acme-corp" placeholder="my-workspace" />
        <InputGroupAddon align="inline-end">.dify.app</InputGroupAddon>
      </InputGroup>
      <FieldDescription>Contact a workspace owner to change the domain.</FieldDescription>
    </Field>
  )
}
```

### Validation

Inside a `Form`, the input's native constraints drive `FieldError`, and the whole group shows the invalid state. Errors appear when the form is submitted, then update as the value changes. Submit the form empty or with uppercase letters to see each message.

```tsx title="validation.tsx"
import { Button } from '@langgenius/dify-ui/button'
import { Field, FieldDescription, FieldError, FieldLabel } from '@langgenius/dify-ui/field'
import { Form } from '@langgenius/dify-ui/form'
import { InputGroup, InputGroupAddon, InputGroupInput } from '@langgenius/dify-ui/input-group'
import * as React from 'react'

type WebAppAddressValues = {
  subdomain: string
}

export function InputGroupValidationDemo() {
  const [published, setPublished] = React.useState<string | null>(null)

  return (
    <Form<WebAppAddressValues>
      aria-label="Web app address"
      className="grid w-full max-w-80 gap-4"
      onChange={() => {
        if (published) setPublished(null)
      }}
      onFormSubmit={(values) => setPublished(values.subdomain)}
    >
      <Field name="subdomain">
        <FieldLabel>Web app address</FieldLabel>
        <InputGroup>
          <InputGroupInput
            required
            placeholder="support-bot"
            pattern="[a-z0-9\-]+"
            autoCapitalize="none"
            autoComplete="off"
            autoCorrect="off"
            spellCheck={false}
          />
          <InputGroupAddon align="inline-end">.dify.app</InputGroupAddon>
        </InputGroup>
        <FieldDescription>Lowercase letters, numbers and hyphens.</FieldDescription>
        <FieldError match="valueMissing">Enter an address.</FieldError>
        <FieldError match="patternMismatch">
          Use lowercase letters, numbers and hyphens only.
        </FieldError>
      </Field>
      <div className="flex items-center justify-between gap-3">
        <span role="status" className="min-w-0 truncate system-xs-regular text-text-success">
          {published ? `Published at ${published}.dify.app` : ''}
        </span>
        <Button type="submit" variant="primary" className="shrink-0">
          Publish
        </Button>
      </div>
    </Form>
  )
}
```

## Keyboard

| Key             | Action                                                                                                |
| --------------- | ----------------------------------------------------------------------------------------------------- |
| `Tab`           | Moves focus to the input, then to each interactive addon in DOM order, whatever its visual alignment. |
| `Shift` + `Tab` | Moves focus back through the interactive addons and the input.                                        |

## API reference

### InputGroup

A compound text-input container for composing an input with prefix, suffix, or action add-ons. Renders a `<div>` element.

Place the direct `InputGroupInput` before direct `InputGroupAddon` children in the DOM and use `align` for visual placement.

Accepts the props of the element it renders.

### InputGroupAddon

**Props**

| Prop     | Type                             | Default          | Description                                                                   |
| -------- | -------------------------------- | ---------------- | ----------------------------------------------------------------------------- |
| `align?` | `'inline-end' \| 'inline-start'` | `'inline-start'` | Which side of the input the add-on is shown on, independent of its DOM order. |

**Data attributes**

| Attribute    | Description                                     |
| ------------ | ----------------------------------------------- |
| `data-align` | The visual side of the input the addon sits on. |

### InputGroupInput

A native input element that automatically works with [Field](https://base-ui.com/react/components/field). Renders an `<input>` element.

Inherits `className`, `style`, `onValueChange`, `defaultValue`, `value` from [Base UI Input](https://base-ui.com/react/components/input).

**Data attributes**

| Attribute       | Description                                                                 |
| --------------- | --------------------------------------------------------------------------- |
| `data-disabled` | Present when the input is disabled.                                         |
| `data-valid`    | Present when the input is in a valid state (when wrapped in Field.Root).    |
| `data-invalid`  | Present when the input is in an invalid state (when wrapped in Field.Root). |
| `data-touched`  | Present when the input has been touched (when wrapped in Field.Root).       |
| `data-dirty`    | Present when the input's value has changed (when wrapped in Field.Root).    |
| `data-filled`   | Present when the input is filled (when wrapped in Field.Root).              |
| `data-focused`  | Present when the input is focused (when wrapped in Field.Root).             |
