# Fields

> How each field kind's value looks in a record, from pills to dates.

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

Group: Records.

## When to use

How each field kind's value looks in a record, from pills to dates. Use [Description List](https://uicontext.com/docs/components/description-list.md) instead when you lay out read-only facts, not field values.

## Usage

```ts
import { type Item, type Property, ValueView } from "./ui/uicontext/components/Property.ts"

export const FieldValue = <M>(h: Html.H<M>, field: Property, record: Item) =>
	ValueView(h, { property: field, value: record.values[field.key] ?? null, locale: "en-US" })
```

## Examples

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

### Every kind

Fifteen kinds of field on one company. A formula that can't compute shows `#ERROR` and says why on hover.

```ts title="src/every-kind.ts" source="www/src/examples/fields/every-kind.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { type Item, type Property, ValueView } from "./ui/uicontext/components/Property.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

// A company record from a CRM base, one row per field kind. The same field and value read the
// same in a grid cell, on a card and in a record's side panel.

export const companyFields: ReadonlyArray<Property> = [
	{ key: "name", label: "Legal name", kind: "text" },
	{ key: "arr", label: "ARR", kind: "number", format: { style: "currency", currency: "USD", decimals: 0 } },
	{ key: "renewal", label: "Renewal", kind: "date" },
	{
		key: "stage",
		label: "Stage",
		kind: "select",
		options: [
			{ value: "trial", label: "Trial", tone: "blue" },
			{ value: "customer", label: "Customer", tone: "green" },
		],
	},
	{
		key: "segments",
		label: "Segments",
		kind: "multiSelect",
		options: [
			{ value: "logistics", label: "Logistics", tone: "orange" },
			{ value: "enterprise", label: "Enterprise", tone: "purple" },
		],
	},
	{
		key: "owner",
		label: "Owner",
		kind: "person",
		options: [{ value: "maya", label: "Maya Chen", glyph: { _tag: "Person", id: "maya", name: "Maya Chen", agent: false } }],
	},
	{ key: "signed", label: "Signed DPA", kind: "checkbox" },
	{ key: "website", label: "Website", kind: "url" },
	{ key: "email", label: "Billing email", kind: "email" },
	{ key: "phone", label: "Phone", kind: "phone" },
	{ key: "fit", label: "Fit", kind: "rating", max: 5 },
	{ key: "files", label: "Contracts", kind: "attachment" },
	{
		key: "contacts",
		label: "Contacts",
		kind: "relation",
		relation: { target: "Contacts", multiple: true },
		options: [
			{ value: "ct1", label: "Priya Raman" },
			{ value: "ct2", label: "Jonas Berg" },
		],
	},
	{
		key: "deals",
		label: "Open deals",
		kind: "rollup",
		result: "number",
		rollup: { relation: "deals", field: "status", aggregate: "count" },
	},
	{ key: "health", label: "Health", kind: "formula", formula: 'prop("ARR") / prop("Seats")', error: "Unknown field Seats" },
]

export const northwind: Item = {
	id: "co-1",
	title: "Northwind Freight",
	values: {
		name: "Northwind Freight Ltd.",
		arr: 184000,
		renewal: "2027-03-31",
		stage: "customer",
		segments: ["logistics", "enterprise"],
		owner: "maya",
		signed: true,
		website: "https://northwind.example",
		email: "billing@northwind.example",
		phone: "+44 20 7946 0321",
		fit: 4,
		files: ["https://files.example/northwind/msa-2026.pdf", "https://files.example/northwind/order-form.pdf"],
		contacts: ["ct1", "ct2"],
		deals: 3,
		health: null,
	},
}

const styles = stylex.create({
	list: {
		display: "grid",
		gridTemplateColumns: "minmax(7rem, max-content) minmax(0, 1fr)",
		columnGap: space.xl,
		rowGap: space.md,
		margin: 0,
		fontSize: font.sm,
	},
	label: { color: color.muted },
	value: { margin: 0, minWidth: 0 },
})

export const EveryKind = <M>(h: Html.H<M>) =>
	h.dl.attrs(sx(styles.list)).children(
		...companyFields.flatMap((field) => [
			h.dt.key(`l:${field.key}`).attrs(sx(styles.label)).children(field.label),
			h.dd
				.key(`v:${field.key}`)
				.attrs(sx(styles.value))
				.children(ValueView(h, { property: field, value: northwind.values[field.key] ?? null, locale: "en-US" })),
		]),
	)
```

### Empty values

Quiet in a row (a faint ring, a dash or nothing) and in words in a panel.

```ts title="src/empty-values.ts" source="www/src/examples/fields/empty-values.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { type Property, ValueView } from "./ui/uicontext/components/Property.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

// An empty value is quiet in a dense view and says what's missing in a side panel. `emptyAs` picks
// the quiet form per field; `labelEmpty` asks for the words.

const fields: ReadonlyArray<Property> = [
	{ key: "assignee", label: "Assignee", kind: "person", empty: "No assignee", options: [] },
	{ key: "priority", label: "Priority", kind: "select", empty: "No priority", emptyAs: "dash", options: [] },
	{ key: "estimate", label: "Estimate", kind: "number", empty: "No estimate" },
	{ key: "due", label: "Due", kind: "date", empty: "No due date", emptyAs: "label" },
]

const styles = stylex.create({
	table: { borderCollapse: "collapse", fontSize: font.sm },
	head: { paddingBlock: space.sm, paddingInline: space.md, color: color.muted, fontWeight: 500, textAlign: "start" },
	cell: {
		paddingBlock: space.sm,
		paddingInline: space.md,
		borderTopWidth: 1,
		borderTopStyle: "solid",
		borderTopColor: color.line,
	},
})

export const EmptyValues = <M>(h: Html.H<M>) =>
	h.table.attrs(sx(styles.table)).children(
		h.thead.children(
			h.tr.children(
				h.th.attrs({ scope: "col", ...sx(styles.head) }).children("Field"),
				h.th.attrs({ scope: "col", ...sx(styles.head) }).children("In a row"),
				h.th.attrs({ scope: "col", ...sx(styles.head) }).children("In a panel"),
			),
		),
		h.tbody.children(
			...fields.map((field) =>
				h.tr.key(field.key).children(
					h.th.attrs({ scope: "row", ...sx(styles.cell, styles.head) }).children(field.label),
					h.td.attrs(sx(styles.cell)).children(ValueView(h, { property: field, value: null })),
					h.td.attrs(sx(styles.cell)).children(ValueView(h, { property: field, value: null, labelEmpty: true })),
				),
			),
		),
	)
```

## API reference

**Options**

| Name | Type | Description |
| --- | --- | --- |
| `property` (required) | `Property` | The field the value belongs to, which decides how it's drawn |
| `value` (required) | `Value` | The value to show; undefined shows it empty |
| `compact` | `boolean` | For dense rows: a choice with a glyph shows only the glyph, and fewer attachments show |
| `locale` | `string` | The locale numbers and dates are formatted in; the browser's by default |
| `labelEmpty` | `boolean` | An empty value shows its words ("No assignee"), as a property panel does |

## Keyboard and accessibility

- Every value reads as words: a rating is an image named like "4 of 5", an empty value says what's missing ("No assignee"), and a broken formula reads its error.
- The marks beside a value say who filled it, whether it's still being filled and whether its inputs changed, as named images with the same words in a tooltip.

## Installation

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

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

It writes these files:

- `src/ui/uicontext/components/Glyph.ts`
- `src/ui/uicontext/components/IconStroke.ts`
- `src/ui/uicontext/components/Property.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/domain/Format.ts`
- `src/ui/uicontext/components/domain/Glyph.ts`
- `src/ui/uicontext/components/domain/Outcome.ts`
- `src/ui/uicontext/components/domain/Record.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`

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

## Source

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

## Used in

- [Task manager](https://uicontext.com/blocks/issue-tracker.md)
- [Database](https://uicontext.com/blocks/tables.md)
- [CRM](https://uicontext.com/blocks/crm.md)
- [Helpdesk](https://uicontext.com/blocks/helpdesk.md)

## Related

- [Data Grid](https://uicontext.com/docs/components/data-grid.md) (`data-grid`): A spreadsheet-like grid for editing records, with typed columns, sorting and grouping.
- [Pivot Table](https://uicontext.com/docs/components/pivot-table.md) (`pivot-table`): Records summed two ways, such as revenue by region and quarter, with totals on both edges and cells tinted by size.
- [Table](https://uicontext.com/docs/components/table.md) (`table`): A simple read-only table with a caption, row headers and an empty state.
- [Group Header](https://uicontext.com/docs/components/group-header.md) (`group-header`): The line over a group of rows with its count, sticky while they scroll, folding the group.
- [Description List](https://uicontext.com/docs/components/description-list.md) (`description-list`): A record's facts as label and value pairs, in rows or columns, with Copy for ids.
- [Fact Grid](https://uicontext.com/docs/components/fact-grid.md) (`fact-grid`): A few numbers about one thing, such as a customer's plan and seats, as small tiles.
