# Input Group

> An input with icons, words or a button inside its frame, such as a search box or a price.

Page: https://uicontext.com/docs/components/input-group

Group: Primitives.

## When to use

An input with icons, words or a button inside its frame, such as a search box or a price. Use [Input](https://uicontext.com/docs/components/input.md) instead when the input needs nothing inside its frame.

## Usage

```ts
import { Input } from "./ui/uicontext/components/FormControls.ts"
import { inAddons, InputAddons } from "./ui/uicontext/components/InputAddons.ts"

export const Website = <M>(h: Html.H<M>) =>
	InputAddons(h, { start: [{ text: "https://" }] }, Input(h, { style: inAddons }).attrs({ "aria-label": "Website", placeholder: "example.com" }))
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### Search, a URL and a secret

The example the page opens with: Input Group in “Search, a URL and a secret”.

```ts title="src/demo.ts" source="www/src/examples/input-group/demo.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { FormField, Input } from "./ui/uicontext/components/FormControls.ts"
import { inAddons, InputAddons } from "./ui/uicontext/components/InputAddons.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({ form: { display: "grid", gap: space.xl, inlineSize: "min(100%, 26rem)" } })

const Message = UiSchema.TaggedEnum({
	ChangedQuery: { value: Schema.String },
	ClearedQuery: {},
	ChangedSlug: { value: Schema.String },
	ToggledKey: {},
})

const searchIcon = <M>(h: Html.H<M>) =>
	h.svg.attrs({ width: "16", height: "16", viewBox: "0 0 16 16", fill: "none", stroke: "currentColor", "stroke-width": "1.5", "stroke-linecap": "round" }).children(h.path.attrs({ d: "M7 12.5a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11zM14.5 14.5l-3.6-3.6" }))
const eye = <M>(h: Html.H<M>, open: boolean) =>
	h.svg.attrs({ width: "16", height: "16", viewBox: "0 0 16 16", fill: "none", stroke: "currentColor", "stroke-width": "1.5", "stroke-linecap": "round" }).children(
		h.path.attrs({ d: open ? "M1.5 8S4 3.5 8 3.5 14.5 8 14.5 8 12 12.5 8 12.5 1.5 8 1.5 8zM8 10a2 2 0 1 0 0-4 2 2 0 0 0 0 4z" : "M2 2l12 12M6.6 6.6a2 2 0 0 0 2.8 2.8M4.2 4.4C2.5 5.6 1.5 8 1.5 8S4 12.5 8 12.5c1.3 0 2.4-.4 3.4-1M7 3.6c.3 0 .7-.1 1-.1 4 0 6.5 4.5 6.5 4.5s-.5 1-1.5 2" }),
	)

// a workspace's settings: search with a key hint, a URL with its fixed start, and an API key you can show
export const WorkspaceSettings = Html.component({
	name: "WorkspaceSettings",
	model: Schema.Struct({ query: Schema.String, slug: Schema.String, shown: Schema.Boolean }),
	message: Message,
}).make({
	init: () => ({ query: "", slug: "northwind", shown: false }),
	update: {
		ChangedQuery: ($, { value }) => $.evolve({ query: value }),
		ClearedQuery: ($) => $.evolve({ query: "" }),
		ChangedSlug: ($, { value }) => $.evolve({ slug: value }),
		ToggledKey: ($) => $.evolve({ shown: (s) => !s }),
	},
	view: (h, model) =>
		h.div.attrs(sx(styles.form)).children(
			InputAddons(
				h,
				{ start: [{ icon: searchIcon(h) }], end: model.query === "" ? [{ kbd: "mod+k" }] : [{ button: { label: "Clear", onPress: Message.ClearedQuery() } }] },
				Input(h, { style: inAddons })
					.attrs({ type: "search", placeholder: "Search settings", "aria-label": "Search settings" })
					.value(model.query)
					.on("input", ({ value }) => Message.ChangedQuery({ value })),
			),
			FormField(h, { id: "slug", label: "Workspace URL", hint: "Letters, numbers and dashes." }, (field) =>
				InputAddons(
					h,
					{ dir: "ltr", start: [{ text: "app.acme.com/" }] },
					Input(h, { style: inAddons }).attrs({ ...field, autocomplete: "off", spellcheck: "false" }).value(model.slug).on("input", ({ value }) => Message.ChangedSlug({ value })),
				)),
			FormField(h, { id: "api-key", label: "API key" }, (field) =>
				InputAddons(
					h,
					{ dir: "ltr", end: [{ button: { label: model.shown ? "Hide key" : "Show key", icon: eye(h, !model.shown), iconOnly: true, onPress: Message.ToggledKey() } }] },
					Input(h, { style: inAddons }).attrs({ ...field, type: model.shown ? "text" : "password", readonly: "", autocomplete: "off" }).value("sk_live_51Hq8vT2eZvKYlo2C0f9w"),
				)),
		),
})
```

### A price and a unit

`content` takes any control, such as a currency picker. Fixed words are hidden from screen readers, so the label says “in kg”.

```ts title="src/price.ts" source="www/src/examples/input-group/price.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { FormField, Input, Select } from "./ui/uicontext/components/FormControls.ts"
import { inAddons, InputAddons } from "./ui/uicontext/components/InputAddons.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	form: { display: "grid", gap: space.xl, inlineSize: "min(100%, 22rem)" },
	unit: { blockSize: "100%", borderWidth: 0, borderRadius: 0, backgroundColor: "transparent", boxShadow: "none", paddingInlineStart: space.sm },
})

const Message = UiSchema.TaggedEnum({ ChangedPrice: { value: Schema.String }, ChangedCurrency: { value: Schema.String }, ChangedWeight: { value: Schema.String } })

// a product's price and shipping weight: the currency is a picker inside the field, the unit is fixed
export const ProductPrice = Html.component({
	name: "ProductPrice",
	model: Schema.Struct({ price: Schema.String, currency: Schema.String, weight: Schema.String }),
	message: Message,
}).make({
	init: () => ({ price: "48.00", currency: "USD", weight: "0.4" }),
	update: {
		ChangedPrice: ($, { value }) => $.evolve({ price: value }),
		ChangedCurrency: ($, { value }) => $.evolve({ currency: value }),
		ChangedWeight: ($, { value }) => $.evolve({ weight: value }),
	},
	view: (h, model) =>
		h.div.attrs(sx(styles.form)).children(
			FormField(h, { id: "price", label: "Price" }, (field) =>
				InputAddons(
					h,
					{
						start: [{ text: model.currency === "EUR" ? "€" : model.currency === "GBP" ? "£" : "$" }],
						end: [{
							content: Select(h, { style: styles.unit })
								.attrs({ "aria-label": "Currency" })
								.value(model.currency)
								.on("change", ({ value }) => Message.ChangedCurrency({ value }))
								.children(["USD", "EUR", "GBP"].map((c) => h.option.key(c).attrs({ value: c }).children(c))),
						}],
					},
					Input(h, { style: inAddons }).attrs({ ...field, inputmode: "decimal" }).value(model.price).on("input", ({ value }) => Message.ChangedPrice({ value })),
				)),
			FormField(h, { id: "weight", label: "Shipping weight in kg" }, (field) =>
				InputAddons(h, { end: [{ text: "kg" }] }, Input(h, { style: inAddons }).attrs({ ...field, inputmode: "decimal" }).value(model.weight).on("input", ({ value }) => Message.ChangedWeight({ value })))),
		),
})
```

## API reference

### InputAddons (view function)

**Options**

| Name | Type | Description |
| --- | --- | --- |
| `start` | `InputAddon<Message>[]` | — |
| `end` | `InputAddon<Message>[]` | — |
| `dir` | `"ltr"` | "ltr" for what always reads left to right, such as a URL, a key or code, in a right-to-left page too. |
| `style` | `Style` | — |

### Input (view function)

**Options**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `invalid` | `boolean` | — | Marks the value as wrong: a red border, and `aria-invalid` for assistive technology. |
| `size` | `"sm" \| "md"` | `md` | `sm` for toolbars, table rows and side panels, a control's height; `md` for forms; `md` by default. |
| `style` | `Style` | — | StyleX styles applied after the control's own. |

### Select (view function)

**Options**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `invalid` | `boolean` | — | Marks the value as wrong: a red border, and `aria-invalid` for assistive technology. |
| `size` | `"sm" \| "md"` | `md` | `sm` for toolbars, table rows and side panels, a control's height; `md` for forms; `md` by default. |
| `style` | `Style` | — | StyleX styles applied after the control's own. |

### Textarea (view function)

**Options**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `invalid` | `boolean` | — | Marks the value as wrong: a red border, and `aria-invalid` for assistive technology. |
| `size` | `"sm" \| "md"` | `md` | `sm` for toolbars, table rows and side panels, a control's height; `md` for forms; `md` by default. |
| `style` | `Style` | — | StyleX styles applied after the control's own. |

### InputGroup (view function)

**Options**

| Name | Type | Description |
| --- | --- | --- |
| `start` | `string` | — |
| `end` | `string` | — |
| `style` | `Style` | — |

### FormField (view function)

**Options**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `id` (required) | `string` | — | The control's id. The label, description and error take ids made from it. |
| `label` (required) | `string` | — | The label's text, which names the control. |
| `description` | `string` | — | A hint under the control, read out with it. |
| `error` | `string` | — | What's wrong with the value. Shown under the control, announced, and marks the control invalid. |
| `hint` | `string` | — | A short note at the end of the label's line: a count such as "12/48", or "Max 5 MB". |
| `hintOver` | `boolean` | — | The hint warns, as a count past its limit does. |
| `required` | `boolean` | — | An empty value is an error: a mark after the label, and `aria-required` on the control. |
| `optional` | `boolean` | — | May stay empty, said after the label: for a form where most fields are required. |
| `status` | `{ readonly tone: "pending" \| "success"; readonly text: string }` | — | Where an async check stands, in the error's place while there's no error: "Checking…", "acme is available". |
| `layout` | `"stacked" \| "inline" \| "inline-end"` | `stacked` | Where the label sits: above the control (text fields, selects, a radio group), or beside it, with the control first (a checkbox) or last (a switch in a settings row); `stacked` by default |
| `group` | `boolean` | — | The control is a group of controls (a radio group, a segmented control): a group can't be the target of a `<label for>`, so the label names it through `aria-labelledby` instead |
| `labelStyle` | `Style` | — | StyleX styles for the label: a row field's label that hides behind column headings. |
| `style` | `Style` | — | StyleX styles for the field's wrapper. |

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/input-group
```

With npm: `npx shadcn@latest add checkfu/uicontext/input-group`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/input-group`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/input-group`.

It writes these files:

- `src/ui/uicontext/components/FormControls.ts`
- `src/ui/uicontext/components/IconStroke.ts`
- `src/ui/uicontext/components/InlineNotice.ts`
- `src/ui/uicontext/components/InputAddons.ts`
- `src/ui/uicontext/components/Kbd.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`
- `src/ui/uicontext/components/touch.stylex.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/InputAddons.ts](https://github.com/checkfu/uicontext/blob/main/src/InputAddons.ts)
- [src/FormControls.ts](https://github.com/checkfu/uicontext/blob/main/src/FormControls.ts)

## Used in

- [SaaS dashboard](https://uicontext.com/blocks/saas-dashboard.md)
- [Chat](https://uicontext.com/blocks/ai-chat.md)
- [Task manager](https://uicontext.com/blocks/issue-tracker.md)
- [Workflow builder](https://uicontext.com/blocks/automation-builder.md)
- [Database](https://uicontext.com/blocks/tables.md)
- [CRM](https://uicontext.com/blocks/crm.md)
- [Analytics dashboard](https://uicontext.com/blocks/analytics.md)
- [Email](https://uicontext.com/blocks/email.md)
- [Scheduling](https://uicontext.com/blocks/scheduling.md)
- [Storefront](https://uicontext.com/blocks/storefront.md)
- [Helpdesk](https://uicontext.com/blocks/helpdesk.md)
- [Team assistant](https://uicontext.com/blocks/team-assistant.md)
- [Team chat](https://uicontext.com/blocks/team-chat.md)
- [Bots](https://uicontext.com/blocks/bot-console.md)

## Related

- [Button](https://uicontext.com/docs/components/button.md) (`button`): A button, and a link that looks like one, in every tone and three sizes.
- [Toggle](https://uicontext.com/docs/components/toggle.md) (`toggle`): A button that stays pressed, such as Bold or Pin.
- [Toggle Group](https://uicontext.com/docs/components/toggle-group.md) (`toggle-group`): Buttons that stay pressed and share one value, such as text styles.
- [Input](https://uicontext.com/docs/components/input.md) (`input`): Text inputs, selects and textareas in a labeled field with a hint and an error, and their styles for controls you lay out yourself.
- [Status](https://uicontext.com/docs/components/status.md) (`status`): Status badges and tags for runs and records.
- [Glyphs](https://uicontext.com/docs/components/glyphs.md) (`glyphs`): Small marks for status, priority, color and people.
