A number with − and + buttons, a range, the reader's currency, unit or percent format, and a label you drag to scrub.
You're using 9. Up to 500 on this plan.
$12.00 × 12$144.00 / month
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { NumberInput } from "./ui/uicontext/components/NumberInput.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, radius, space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ card: { display: "grid", gap: space.lg, inlineSize: "min(100%, 22rem)", padding: space.xl, borderWidth: 1, borderStyle: "solid", borderColor: color.line, borderRadius: radius.lg, backgroundColor: color.panel, }, total: { display: "flex", justifyContent: "space-between", margin: 0, fontSize: font.sm, color: color.muted, fontVariantNumeric: "tabular-nums" }, amount: { color: color.fg, fontWeight: 600 },})const PRICE = 12const money = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" })const Message = UiSchema.TaggedEnum({ UpdatedSeats: { seats: Schema.NullOr(Schema.Number) } })// choosing seats on a plan: the total follows every step, and an empty field reads as no changeexport const SeatPicker = Html.component({ name: "SeatPicker", model: Schema.Struct({ seats: Schema.NullOr(Schema.Number) }), message: Message,}).make({ init: () => ({ seats: 12 }), update: { UpdatedSeats: ($, { seats }) => $.evolve({ seats }) }, view: (h, { seats }) => h.div.attrs(sx(styles.card)).children( NumberInput(h, { label: "Seats", value: seats, min: 1, max: 500, suffix: seats === 1 ? "seat" : "seats", description: "You're using 9. Up to 500 on this plan.", onChange: (next) => Message.UpdatedSeats({ seats: next }), }), h.p.attrs({ role: "status", ...sx(styles.total) }).children( `${money.format(PRICE)} × ${seats ?? 0}`, h.span.attrs(sx(styles.amount)).children(`${money.format(PRICE * (seats ?? 0))} / month`), ), ),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/number-inputUsage
import { NumberInput } from "./ui/uicontext/components/NumberInput.ts"const Message = UiSchema.TaggedEnum({ UpdatedSeats: { seats: Schema.NullOr(Schema.Number) } })export const SeatsInput = (h: Html.H<typeof Message.Type>, seats: number | null) => NumberInput(h, { label: "Seats", value: seats, min: 1, max: 500, onChange: (next) => Message.UpdatedSeats({ seats: next }), })Examples
Formats and states
Currency in German, a unit, a percentage, a big budget with no buttons, read-only, and an error.
Set by your workspace admin.
Choose 0 to 10.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { NumberInput } from "./ui/uicontext/components/NumberInput.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ grid: { display: "grid", gridTemplateColumns: "repeat(auto-fill, minmax(12rem, 1fr))", gap: 20, inlineSize: "min(100%, 34rem)" } })const Message = UiSchema.TaggedEnum({ UpdatedValue: { field: Schema.String, value: Schema.NullOr(Schema.Number) } })// the reader's number format: a German price, a weight, a discount, an annual budget, and a limit only an admin setsexport const NumberFormats = Html.component({ name: "NumberFormats", model: Schema.Struct({ values: Schema.Record(Schema.String, Schema.NullOr(Schema.Number)) }), message: Message,}).make({ init: () => ({ values: { price: 1234.5, weight: 2.4, discount: 0.15, budget: 1250000, limit: 50 } }), update: { UpdatedValue: ($, { field, value }) => $.evolve({ values: (v) => ({ ...v, [field]: value }) }) }, view: (h, { values }) => { const field = (key: string) => ({ key, value: values[key] ?? null, onChange: (value: number | null) => Message.UpdatedValue({ field: key, value }) }) return h.div.attrs(sx(styles.grid)).children( NumberInput(h, { ...field("price"), label: "Preis", locale: "de-DE", step: 0.5, min: 0, format: { style: "currency", currency: "EUR" } }), NumberInput(h, { ...field("weight"), label: "Parcel weight", step: 0.1, min: 0, max: 30, format: { style: "unit", unit: "kilogram", maximumFractionDigits: 1 } }), NumberInput(h, { ...field("discount"), label: "Discount", step: 0.05, min: 0, max: 1, format: { style: "percent" } }), NumberInput(h, { ...field("budget"), label: "Annual budget", step: 1000, min: 0, format: { style: "currency", currency: "USD", maximumFractionDigits: 0 }, buttons: false }), NumberInput(h, { ...field("limit"), label: "API calls per minute", readOnly: true, description: "Set by your workspace admin." }), NumberInput(h, { ...field("empty"), label: "Retries", min: 0, max: 10, placeholder: "Default (3)", error: "Choose 0 to 10." }), ) },})Scrub
Drag a label left or right to change its value, as in a design tool. The value follows the pointer; it saves once, on release.
Saved 0 times
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { NumberInput } from "./ui/uicontext/components/NumberInput.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ grid: { display: "grid", gridTemplateColumns: "repeat(2, minmax(0, 1fr))", gap: 12, inlineSize: "min(100%, 18rem)" }, note: { gridColumnStart: "1", gridColumnEnd: "-1", margin: 0, fontSize: font.xs, color: color.muted, fontVariantNumeric: "tabular-nums" },})const Field = Schema.Literals(["x", "y", "width", "height"])const Message = UiSchema.TaggedEnum({ UpdatedFrame: { field: Field, value: Schema.NullOr(Schema.Number) }, CommittedFrame: {},})// an inspector's frame: drag a label to scrub (Shift for ten, Alt for a tenth); one save per dragexport const FrameInspector = Html.component({ name: "FrameInspector", model: Schema.Struct({ x: Schema.Number, y: Schema.Number, width: Schema.Number, height: Schema.Number, saves: Schema.Number }), message: Message,}).make({ init: () => ({ x: 120, y: 48, width: 320, height: 180, saves: 0 }), update: { UpdatedFrame: ($, { field, value }) => (value === null ? $.model : { ...$.model, [field]: value }), CommittedFrame: ($) => $.evolve({ saves: (n) => n + 1 }), }, view: (h, model) => h.div.attrs(sx(styles.grid)).children( Field.literals.map((field) => NumberInput(h, { key: field, label: field === "x" ? "X" : field === "y" ? "Y" : field === "width" ? "W" : "H", value: model[field], min: field === "width" || field === "height" ? 1 : -10000, max: 10000, buttons: false, suffix: "px", onChange: (value) => Message.UpdatedFrame({ field, value }), onCommit: () => Message.CommittedFrame(), })), h.p.attrs({ role: "status", ...sx(styles.note) }).children(`Saved ${model.saves} ${model.saves === 1 ? "time" : "times"}`), ),})API reference
Options
| Name | Type | Default | Description |
|---|---|---|---|
key | string | — | Names the field among others in the same view; needed for more than one. |
labelrequired | string | — | What the number is: "Seats", "Price". |
valuerequired | number | null | — | The number, or null for an empty field. The caller keeps it. |
min | number | — | — |
max | number | — | — |
step | number | 1 | What a key, a button press and a dragged pixel change it by; 1 by default. |
largeStep | number | — | What Shift with an arrow changes it by; ten steps by default. |
format | Format | — | How it reads, as Intl.NumberFormat options: { style: "currency", currency: "EUR" }, { style: "unit", unit: "kilogram" }, { style: "percent" }, { maximumFractionDigits: 2 }. |
locale | string | — | The reader's locale: "de-DE" reads "1.234,5". The browser's by default. |
suffix | string | — | A word after the number that Intl has no unit for: "seats", "per month". |
scrub | boolean | true | Drag the label to change the value; true by default. |
buttons | boolean | true | The − and + buttons; true by default. |
placeholder | string | — | — |
description | string | — | — |
error | string | — | — |
disabled | boolean | — | — |
readOnly | boolean | — | — |
required | boolean | — | — |
name | string | — | — |
onChangerequired | (value: number | null) => Message | — | Each value asked for: while typing, per step and during a scrub. Store it as value. |
onCommit | (value: number | null) => Message | — | The value once the person is done: on blur, after a key, on releasing a button or a scrub. Save here. |
style | Style | — | StyleX styles applied after the field's own. |