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
Controlled
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
| Variant | What it is |
|---|---|
| weight | kg / lb, 1 dp. |
| height | cm / in, 1 dp. |
| temp | °C / °F, 1 dp. |
| glucose | mg/dL 0 dp / mmol/L 1 dp. |
| creatinine | mg/dL 2 dp / µmol/L 0 dp. |
| a1c | % 1 dp / mmol/mol 0 dp. |
| showStored | Shows 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
| Prop | Type | Default | Description |
|---|---|---|---|
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 |
defaultUnit | string | `unit` | Unit shown first (uncontrolled) |
displayUnit | string | none | Unit shown (controlled) |
onChange | (unit: string) => void | none | Called with the UCUM code the reader picks |
showStored | boolean | false | Shows "Stored as ..." under the converted value |
label | string | the measure label | Label |
size | ClinicalValueSize | 'md' | 'sm' | 'md' | 'lg' |
measure | string | `kind` | Measure key when it differs from `kind` |
showMeta | boolean | false | Shows the range and meta line (in the measure's own unit only) |
rangeContext | RangeContextId | the global context, else 'outpatient' | Which shared reference range flags use; the lab range on a result still wins |
defaultRangeContext | RangeContextId | context, used only when no prop, provider or global context applies; default none | The containing set's |
ageYears | number | none | Age 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>
| Kind | Name | Description |
|---|---|---|
| attribute | kind | Measure family: 'weight' | 'height' | 'temp' | 'glucose' | 'creatinine' | 'a1c'; required |
| attribute | value | Stored value, in `unit`; required |
| attribute | unit | UCUM code of the stored value ('kg', '[lb_av]', 'Cel', 'mg/dL'...); required |
| attribute | default-unit / defaultUnit | Unit shown first (uncontrolled); default `unit` |
| attribute | display-unit / displayUnit | Unit shown (controlled); default none |
| attribute | show-stored / showStored | Shows "Stored as ..." under the converted value; default false |
| attribute | label | Label; default the measure label |
| attribute | size | 'sm' | 'md' | 'lg'; default 'md' |
| attribute | measure | Measure key when it differs from `kind`; default `kind` |
| attribute | show-meta / showMeta | Shows the range and meta line (in the measure's own unit only); default false |
| attribute | range-context / rangeContext | Which shared reference range flags use; the lab range on a result still wins; default the global context, else 'outpatient' |
| attribute | default-range-context / defaultRangeContext | The containing set's default context, used only when no prop, provider or global context applies; default none |
| attribute | age-years / ageYears | Age in years for the pediatric bands; default none |
| attribute | default-checked / defaultChecked | |
| property / JSON attribute | default-value / defaultValue | |
| attribute | auto-focus / autoFocus | |
| event | co-change | Called with the UCUM code the reader picks; default none (React: onChange) |