Skip to content
opsinjs
Edit this page

Components

ReadingInput

A field for typing in a measurement, with the unit shown and switchable beside the number, and room for an advisory the product wrote.

Usage

pnpm dlx shadcn@latest add @opsinjs/reading-input

The @opsinjs namespace is declared in your components.json. Everything it installs is code you then own. There is no runtime package to keep in step.

import { ReadingInput } from "@/components/ui/reading-input"
<ReadingInput
  label="Example measurement"
  unit={unit}
  units={["kg", "lb", "st"]}
  precision={1}
  value={reading}
  onChange={(next) => {
    setReading(next.value)
    setUnit(next.unit)
  }}
/>

Examples

Theme
Density
Text
Status
Accent theme
Accent
reading-input · base base · style base-lyraOpen under the product theme

Two numbers that are one measurement

segments turns the field into a <fieldset> with a <legend>, one box per part.

Theme
Density
Text
Status
Accent theme
Accent
reading-input-a-compound-reading · base base · style base-lyraOpen under the product theme
reading-input-a-compound-reading.tsx
"use client"

import { useState } from "react"

import { ReadingInput, type ReadingSegment } from "@/components/ui/reading-input"

export default function ReadingInputACompoundReading() {
  const [parts, setParts] = useState<ReadingSegment[]>([
    { label: "First part", value: 10 },
    { label: "Second part", value: 20 },
  ])

  return (
    <div className="w-full max-w-md">
      <ReadingInput
        hint="Both parts, in whole numbers"
        label="Example measurement"
        name="example-measurement"
        onChange={(next) => setParts(next.segments)}
        segments={parts}
        unit="mmHg"
      />
    </div>
  )
}

Switching the unit, and the sentence that follows it

precision sets the decimal places a conversion rounds to.

Theme
Density
Text
Status
Accent theme
Accent
reading-input-switching-the-unit · base base · style base-lyraOpen under the product theme
reading-input-switching-the-unit.tsx
"use client"

import { useState } from "react"

import { ReadingInput } from "@/components/ui/reading-input"

export default function ReadingInputSwitchingTheUnit() {
  const [reading, setReading] = useState<number | null>(10)
  const [unit, setUnit] = useState("kg")

  return (
    <div className="w-full max-w-md">
      <ReadingInput
        hint="To one decimal place"
        label="Example measurement"
        name="example-measurement"
        onChange={(next) => {
          setReading(next.value)
          setUnit(next.unit)
        }}
        precision={1}
        unit={unit}
        units={["kg", "lb", "st"]}
        value={reading}
      />
    </div>
  )
}

A sentence the product wrote

warning shows the product's sentence without comparing anything.

Theme
Density
Text
Status
Accent theme
Accent
reading-input-a-warning-the-product-owns · base base · style base-lyraOpen under the product theme
reading-input-a-warning-the-product-owns.tsx
"use client"

import { useState } from "react"

import { ReadingInput } from "@/components/ui/reading-input"

export default function ReadingInputAWarningTheProductOwns() {
  const [reading, setReading] = useState<number | null>(1400)

  return (
    <div className="w-full max-w-md">
      <ReadingInput
        hint="To one decimal place"
        label="Example measurement"
        name="example-measurement"
        onChange={(next) => setReading(next.value)}
        unit="kg"
        value={reading}
        warning={
          reading === null
            ? undefined
            : "That is a long way above most readings. It is worth checking the number before you save it."
        }
      />
    </div>
  )
}

When to use it

Use it when

  • Anywhere a person types a measurement of themselves: weight, blood pressure, glucose, temperature.
  • Entry where the unit is part of the value.
  • Entry where the product flags an unlikely number without stopping anybody.

Do not use it when

  • A quantity, a count or a code, not a measurement of a person.Use number-field instead.
  • The value is best chosen from a small set rather than typed.Use segmented-control instead.
  • The reader estimates the value, such as a pain score, not measures it.Use scale-input instead.
  • You are displaying a reading, not collecting one.Use value instead.
  • Several related measurements captured in one interaction.Use log-sheet instead.

Clinical meaning

Asserts. That the reader typed this number, in this unit, as a measurement of themselves. It is always self-reported.

Never read as. A validated measurement, a device reading or an assessed value.

Colour axis. Neither; the advisory is ordinary foreground.

Thresholds. None. The component holds no bound. The product supplies the finished warning, which never sets aria-invalid, moves focus or blocks submission. Staleness belongs to Trends and uncertainty.

Unit switches. Only definitional conversions from tokens/units.json: kg, lb, st, °C and °F. mmol/L to mg/dL is refused, because the factor is the substance's molar mass, so that switch clears the entry and says so. See Numbers and units.

Accessibility

From the source. Field renders a real <label for>, and a compound reading is a <fieldset> of one Field per part. The unit's spoken form joins the box's description, each option carries its plural on aria-label, and the box is type="text" with inputMode defaulting to decimal. The advisory and the effect line are descriptions, with no aria-invalid and no focus move. The audit made autoComplete a prop reaching Field.Control, defaulting to "off", because a self-reported reading has no WCAG autofill token and the component invents none (1.3.5).

Residual items.

  • The effect line reporting a switch sits in no live region, so a product must wire effectText to a polite announcer. Without that, a non-sighted reader hears the new unit but not that the number changed.
  • No hit area is measured.
  • The boundary inherits --border without a gated pair.
KeyActionNotes
TabMoves to the box, then the unit switchThree stops on a compound reading: two boxes and the switch.
Shift+TabMoves backwards through the same stopsReverse order matches the visual order.
Any digit, . or ,Types into the focused boxNothing is rejected, clamped or reformatted while typing.
Up / DownChanges the unit when the switch has focusNative select. The entry converts or clears, and the line says which.
EnterWhatever the surrounding form doesNothing is swallowed. Set enterKeyHint to name the return key's action.
EscapeNothing hereNo overlay to dismiss, no entry to revert.
Contrast: 36 measured token pairs, 15 below floor
PairThemeAPCA LcWCAG 2.2Floor
body text on the pagelight101.617.18:1Pass
body text on the pagedark-100.518.00:1Pass
body text on a cardlight104.717.96:1Pass
body text on a carddark-99.616.32:1Pass
body text on the muted groundlight98.116.32:1Pass
body text on the muted grounddark-97.513.77:1Pass
secondary text on the pagelight83.97.42:1Pass
secondary text on the pagedark-80.713.44:1Pass
secondary text on a cardlight87.07.76:1Pass
secondary text on a carddark-79.812.19:1Pass
secondary text on the muted groundlight80.47.05:1Pass
secondary text on the muted grounddark-77.810.28:1Pass
a hairline boundary on the pagelight19.31.41:1Below floor
a hairline boundary on the pagedark-8.21.90:1Below floor
a hairline boundary on a cardlight22.41.47:1Below floor
a hairline boundary on a carddark-7.31.72:1Below floor
a hairline boundary on the muted groundlight15.81.34:1Below floor
a hairline boundary on the muted grounddark0.01.45:1Below floor
a placeholder boundary on the pagelight69.84.52:1Pass
a placeholder boundary on the pagedark-51.07.62:1Pass
a placeholder boundary on a cardlight72.84.72:1Pass
a placeholder boundary on a carddark-50.16.91:1Pass
a placeholder boundary on the muted groundlight66.24.29:1Pass
a placeholder boundary on the muted grounddark-48.15.83:1Pass
a placeholder fill on the pagelight8.31.18:1Below floor
a placeholder fill on the pagedark-8.21.90:1Below floor
a placeholder fill on a cardlight11.41.23:1Below floor
a placeholder fill on a carddark-7.31.72:1Below floor
a placeholder fill on the muted groundlight0.01.12:1Below floor
a placeholder fill on the muted grounddark0.01.45:1Below floor
a placeholder boundary on its own filllight58.93.84:1Pass
a placeholder boundary on its own filldark-41.54.01:1Below floor
the card hairline on the pagelight69.84.52:1Pass
the card hairline on the pagedark-28.74.19:1Below floor
the card hairline on a cardlight72.84.72:1Pass
the card hairline on a carddark-27.93.80:1Below floor

These are the measured token pairs this component draws colour from, not a measurement of the component itself.

Props interface

Prop

Type

Generated from ReadingInputProps in registry/bases/base/reading-input.tsx.

unit is required and value is in it. Without precision a conversion is not rounded. There is no error prop: an error belongs to Field.

Data attributes

AttributeConditionValue
data-slotOn the root, the control row, every segment and the unit, always.reading-input, reading-input-control, reading-input-segment, reading-input-unit
data-slotLegend and hint on a compound reading, warning when supplied, effect after a switch.reading-input-legend, reading-input-hint, reading-input-warning, reading-input-effect
data-opsinjs-valueOn every ReadingInput.Segment, alwaysThe number in that box, read off the box not the prop
data-status, data-categoryNever, at any value of any propNever set