Skip to main content

UnitToggle

ComplexClinical values<co-unit-toggle>

UnitToggle shows a stored measurement in the unit the reader picks (lb or kg, in or cm, °F or °C, mg/dL or mmol/L), converting value and range with exact factors.

import { UnitToggle } from 'health-components';

Examples​

Families​

Storybook

Controlled​

Storybook

Usage​

<UnitToggle kind="weight" value={36.3} unit="kg" defaultUnit="[lb_av]" showStored />

When to use​

  • Weight, height and temperature where staff and patients use different units.
  • Glucose, creatinine and A1c for clinicians trained on SI units.

When not to use​

  • Data entry: the input stores one canonical unit; use TextField with a unit suffix.
  • Doses: never convert a dose for display.

Variants and states​

VariantWhat it is
weightkg / lb, 1 dp.
heightcm / in, 1 dp.
temp°C / °F, 1 dp.
glucosemg/dL 0 dp / mmol/L 1 dp.
creatininemg/dL 2 dp / µmol/L 0 dp.
a1c% 1 dp / mmol/mol 0 dp.
showStoredShows the stored value under the converted one.

Accessibility​

  • Unit buttons are a labelled group of toggle buttons with aria-pressed.
  • The converted value keeps its flag and range.

Do and don't​

  • Do: Convert from the stored number.
  • Don't: Store the converted, rounded number back.

Props​

PropTypeDefaultDescription
kind *UnitToggleKind—Measure family: 'weight' | 'height' | 'temp' | 'glucose' | 'creatinine' | 'a1c'; required
value *number | null—Stored value, in `unit`; required
unit *string—UCUM code of the stored value ('kg', '[lb_av]', 'Cel', 'mg/dL'...); required
defaultUnitstring`unit`Unit shown first (uncontrolled)
displayUnitstringnoneUnit shown (controlled)
onChange(unit: string) => voidnoneCalled with the UCUM code the reader picks
showStoredbooleanfalseShows "Stored as ..." under the converted value
labelstringthe measure labelLabel
sizeClinicalValueSize'md''sm' | 'md' | 'lg'
measurestring`kind`Measure key when it differs from `kind`
showMetabooleanfalseShows the range and meta line (in the measure's own unit only)
rangeContextRangeContextIdthe global context, else 'outpatient'Which shared reference range flags use; the lab range on a result still wins
defaultRangeContextRangeContextIdcontext, used only when no prop, provider or global context applies; default noneThe containing set's
ageYearsnumbernoneAge in years for the pediatric bands

Also accepts native attributes, including defaultChecked, defaultValue, autoFocus, plus className and event handlers on the root element.

Angular, Vue and HTML​

Register the elements once (Angular guide), then use <co-unit-toggle>. Attributes take strings; arrays and objects are set as properties (Angular [prop], Vue :prop) or as JSON attributes. Callbacks are DOM events with the value in event.detail.

Angular template
<co-unit-toggle kind="…" value="…" [defaultValue]="defaultValue" (co-change)="onChange($event.detail)"></co-unit-toggle>
KindNameDescription
attributekindMeasure family: 'weight' | 'height' | 'temp' | 'glucose' | 'creatinine' | 'a1c'; required
attributevalueStored value, in `unit`; required
attributeunitUCUM code of the stored value ('kg', '[lb_av]', 'Cel', 'mg/dL'...); required
attributedefault-unit / defaultUnitUnit shown first (uncontrolled); default `unit`
attributedisplay-unit / displayUnitUnit shown (controlled); default none
attributeshow-stored / showStoredShows "Stored as ..." under the converted value; default false
attributelabelLabel; default the measure label
attributesize'sm' | 'md' | 'lg'; default 'md'
attributemeasureMeasure key when it differs from `kind`; default `kind`
attributeshow-meta / showMetaShows the range and meta line (in the measure's own unit only); default false
attributerange-context / rangeContextWhich shared reference range flags use; the lab range on a result still wins; default the global context, else 'outpatient'
attributedefault-range-context / defaultRangeContextThe containing set's default context, used only when no prop, provider or global context applies; default none
attributeage-years / ageYearsAge in years for the pediatric bands; default none
attributedefault-checked / defaultChecked
property / JSON attributedefault-value / defaultValue
attributeauto-focus / autoFocus
eventco-changeCalled with the UCUM code the reader picks; default none (React: onChange)