# 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.

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

Group: Primitives.

## When to use

Text inputs, selects and textareas in a labeled field with a hint and an error, and their styles for controls you lay out yourself. Use [Form](https://uicontext.com/docs/components/form.md) instead when you want a whole form from a schema, with validation and submit.

## Usage

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

const Message = UiSchema.TaggedEnum({ ChangedEmail: { email: Schema.String } })

export const EmailField = (h: Html.H<typeof Message.Type>, email: string) =>
	FormField(h, { id: "invite-email", label: "Email", description: "We'll send the invitation here." }, (attrs) =>
		Input(h)
			.attrs({ ...attrs, type: "email", autocomplete: "email" })
			.value(email)
			.on("input", ({ value }) => Message.ChangedEmail({ email: value })))
```

## Examples

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

### A new project form

The example the page opens with: Input in “A new project form”.

This example also uses Button. Install it too:

```sh
npx shadcn@latest add checkfu/uicontext/button
```

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

const styles = stylex.create({
	form: { display: "grid", gap: 16, inlineSize: "min(100%, 24rem)", justifyItems: "start" },
	field: { justifySelf: "stretch" },
})

const Message = UiSchema.TaggedEnum({
	UpdatedName: { name: Schema.String },
	Submitted: {},
})

export const NewProjectForm = Html.component({
	name: "NewProjectForm",
	model: Schema.Struct({ name: Schema.String, tried: Schema.Boolean }),
	message: Message,
}).make({
	init: () => ({ name: "", tried: false }),
	update: {
		UpdatedName: ($, { name }) => $.evolve({ name }),
		Submitted: ($) => $.evolve({ tried: true }),
	},
	view: (h, { name, tried }) => {
		const error = tried && name.trim().length < 2 ? "Give the project a name of at least two letters." : undefined
		return h.form.attrs({ novalidate: "", ...sx(styles.form) }).on("submit", Message.Submitted()).children(
			FormField(h, { id: "project-name", label: "Name", style: styles.field, ...(error === undefined ? {} : { error }) }, (attrs) =>
				Input(h, { invalid: error !== undefined })
					.attrs({ ...attrs, placeholder: "Website refresh", autocomplete: "off" })
					.value(name)
					.on("input", ({ value }) => Message.UpdatedName({ name: value }))),
			FormField(h, { id: "project-access", label: "Who can see it", description: "You can change this later.", style: styles.field }, (attrs) =>
				Select(h).attrs(attrs).children(
					h.option.attr("value", "team").children("Everyone on the team"),
					h.option.attr("value", "private").children("Only people I invite"),
				)),
			FormField(h, { id: "project-summary", label: "Summary", style: styles.field }, (attrs) =>
				Textarea(h).attrs({ ...attrs, rows: "3", placeholder: "What is this project for?" })),
			Button(h, { tone: "primary", type: "submit" }).children("Create project"),
		)
	},
})
```

### With a hint and an error

The hint stays when an error appears. The input is marked invalid, and both are read with it.

```ts title="src/hint-and-error.ts" source="www/src/examples/input/hint-and-error.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 { sx } from "./ui/uicontext/components/Sx.ts"

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

const Message = UiSchema.TaggedEnum({ ChangedHandle: { handle: Schema.String } })

/** The app's rule for a handle, checked as the person types. */
const problemWith = (handle: string): string | undefined =>
	handle === "" ? "Choose a handle."
		: /\s/u.test(handle) ? "A handle can't have spaces."
		: handle.length < 3 ? "Use at least three characters."
		: undefined

export const HandleField = Html.component({
	name: "HandleField",
	model: Schema.Struct({ handle: Schema.String }),
	message: Message,
}).make({
	init: () => ({ handle: "maya chen" }),
	update: { ChangedHandle: ($, { handle }) => $.evolve({ handle }) },
	view: (h, { handle }) => {
		const error = problemWith(handle)
		return h.div.attrs(sx(styles.form)).children(
			// the hint and the error both describe the input, so a screen reader reads them with it
			FormField(h, { id: "profile-handle", label: "Handle", description: "Teammates mention you with @handle.", ...(error === undefined ? {} : { error }) }, (attrs) =>
				Input(h, { invalid: error !== undefined })
					.attrs({ ...attrs, autocomplete: "off", spellcheck: "false" })
					.value(handle)
					.on("input", ({ value }) => Message.ChangedHandle({ handle: value }))),
		)
	},
})
```

### Select

A native select, so the phone's own picker opens on touch screens.

```ts title="src/select.ts" source="www/src/examples/input/select.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { FormField, Select } from "./ui/uicontext/components/FormControls.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	form: { display: "grid", gap: 12, inlineSize: "min(100%, 22rem)" },
	note: { margin: 0, color: color.muted, fontSize: font.sm },
})

const zones = [
	{ value: "America/Los_Angeles", label: "Pacific Time (Los Angeles)" },
	{ value: "America/New_York", label: "Eastern Time (New York)" },
	{ value: "Europe/Lisbon", label: "Western European Time (Lisbon)" },
	{ value: "Asia/Singapore", label: "Singapore Time" },
] as const

const Message = UiSchema.TaggedEnum({ PickedZone: { zone: Schema.String } })

export const TimeZoneSelect = Html.component({
	name: "TimeZoneSelect",
	model: Schema.Struct({ zone: Schema.String }),
	message: Message,
}).make({
	init: () => ({ zone: "Europe/Lisbon" }),
	update: { PickedZone: ($, { zone }) => $.evolve({ zone }) },
	view: (h, { zone }) =>
		h.div.attrs(sx(styles.form)).children(
			FormField(h, { id: "workspace-zone", label: "Time zone", description: "Due dates and digests use it." }, (attrs) =>
				Select(h).attrs(attrs).value(zone).on("change", ({ value }) => Message.PickedZone({ zone: value })).children(
					zones.map((option) => h.option.key(option.value).attr("value", option.value).children(option.label)),
				)),
			h.p.attrs(sx(styles.note)).children(`Saved as ${zone}.`),
		),
})
```

### Read-only and disabled

Read-only for a value people may copy but not change; disabled for one that doesn't apply right now.

```ts title="src/disabled.ts" source="www/src/examples/input/disabled.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { FormField, Input, Textarea } from "./ui/uicontext/components/FormControls.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"

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

export const LockedFields = <M>(h: Html.H<M>) =>
	h.div.attrs(sx(styles.form)).children(
		// read-only: still focusable and copyable, and sent with the form
		FormField(h, { id: "workspace-url", label: "Workspace URL", description: "Only an owner can change it." }, (attrs) =>
			Input(h).attrs({ ...attrs, readonly: "" }).value("northpeak.app/acme-studio")),
		// disabled: skipped by Tab and left out of the form
		FormField(h, { id: "billing-note", label: "Billing note" }, (attrs) =>
			Textarea(h).attrs({ ...attrs, rows: "2", disabled: "" }).value("Invoices go to finance@acme-studio.example.")),
	)
```

## API reference

### 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. |

## Keyboard and accessibility

- Each field connects its label, hint and error to the control with `for`, `aria-describedby` and `aria-invalid`.
- Errors are announced as they appear. Forms set `novalidate`, so your validation decides what to show.

## Installation

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

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

It writes these files:

- `src/ui/uicontext/components/FormControls.ts`
- `src/ui/uicontext/components/IconStroke.ts`
- `src/ui/uicontext/components/ShowText.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`

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

## Source

- [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)
- [Issue workspace](https://uicontext.com/blocks/issue-workspace.md)
- [Automation runs](https://uicontext.com/blocks/automation-runs.md)
- [Sign in](https://uicontext.com/blocks/sign-in.md)
- [Sign in, split](https://uicontext.com/blocks/sign-in-split.md)
- [Sign up](https://uicontext.com/blocks/sign-up.md)
- [Forgot password](https://uicontext.com/blocks/forgot-password.md)
- [Reset password](https://uicontext.com/blocks/reset-password.md)
- [Two-step challenge](https://uicontext.com/blocks/two-factor-challenge.md)
- [Workspace setup](https://uicontext.com/blocks/workspace-setup.md)
- [Checkout](https://uicontext.com/blocks/checkout.md)
- [Profile](https://uicontext.com/blocks/profile-settings.md)
- [Notification preferences](https://uicontext.com/blocks/notification-settings.md)
- [Danger zone](https://uicontext.com/blocks/danger-zone.md)
- [Error pages](https://uicontext.com/blocks/error-pages.md)
- [Hero](https://uicontext.com/blocks/hero.md)
- [Newsletter](https://uicontext.com/blocks/newsletter.md)
- [Contact form](https://uicontext.com/blocks/contact-form.md)
- [Footer with columns](https://uicontext.com/blocks/footer-large.md)
- [Status page](https://uicontext.com/blocks/status-page.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.
- [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.
- [Action Toolbar](https://uicontext.com/docs/components/action-toolbar.md) (`action-toolbar`): A row of grouped buttons with one Tab stop, such as formatting toggles or bulk actions.
