{
  "$schema": "https://zoblocks.design/schema/registry-item.json",
  "name": "result-value",
  "type": "zoblocks:component",
  "title": "Result Value",
  "description": "Value, unit and interpretation on one line, qualifiers on a second only when they exist. Seven distinct absence reasons instead of an em dash, a stated interpretation that always beats a derived one, a reference range that says so when there isn't one, and a correction that shows the superseded value rather than a badge.",
  "categories": [
    "clinical",
    "data-display"
  ],
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "registryDependencies": [
    "utils",
    "tokens",
    "result-value-core",
    "clinical-status"
  ],
  "files": [
    {
      "path": "registry/zoblocks/result-value/result-value.tsx",
      "type": "zoblocks:component",
      "target": "components/zoblocks/result-value.tsx",
      "content": "/**\n * ResultValue — a single observation, rendered so the four ways it can lie are\n * all impossible.\n *\n *     <ResultValue value={potassium} now={serverTime} />\n *     <ResultValue value={fromObservation(observation)} density=\"compact\" />\n *\n * The four failures it exists to prevent, each with the shape that prevents it:\n *\n *   a preliminary result that looks final      → status is always rendered\n *   a rangeless result that looks normal       → \"not interpreted\", never blank\n *   a correction that replaced what you read   → the superseded value is shown\n *   an absence rendered as an em dash          → seven reasons, seven sentences\n *\n * `now` is a prop, not a clock. A relative time computed at render is\n * untestable, non-deterministic in a screenshot, and on a ward workstation\n * left open all shift it silently ages.\n *\n * Memoised on `(id, versionId)`: a grid of five hundred rows re-renders none of\n * them when a filter changes, and re-renders exactly the one that was\n * corrected.\n *\n * Styling lives in `styles/zoblocks-result-value.css`, installed alongside.\n */\n\nimport * as React from \"react\";\nimport { cn } from \"@/lib/utils\";\nimport { ClinicalStatus } from \"@/components/zoblocks/clinical-status\";\nimport {\n  ABSENCE,\n  PROVENANCE_LABEL,\n  describeElapsed,\n  describeResult,\n  formatRange,\n  resolveDelta,\n  resolveInterpretation,\n  type ResultValueData,\n} from \"@/lib/zoblocks-result-value\";\n\nexport {\n  ABSENCE,\n  ABSENT_REASONS,\n  PROVENANCE_LABEL,\n  describeElapsed,\n  describeRange,\n  describeResult,\n  formatRange,\n  fromDataAbsentReason,\n  fromObservation,\n  resolveDelta,\n  resolveInterpretation,\n  type ResultAbsence,\n  type Interpretation,\n  type PriorValue,\n  type Provenance,\n  type ReferenceRange,\n  type ResultValueData,\n} from \"@/lib/zoblocks-result-value\";\n\nexport interface ResultValueProps extends Omit<React.HTMLAttributes<HTMLDivElement>, \"children\"> {\n  /**\n   * The result itself, already adapted from FHIR. Use `fromObservation()` rather than building\n   * it by hand — the adapter leaves undefined everything it cannot determine, which is what\n   * keeps a missing range from becoming an assumed one.\n   */\n  value: ResultValueData;\n  /**\n   * ISO 8601, supplied by the host.\n   *\n   * Without it no relative time is rendered at all — which is correct. A\n   * result with no age is less misleading than one whose age is wrong.\n   */\n  now?: string;\n  /** Row height and type scale. Inherited from the nearest density provider when omitted. */\n  density?: \"compact\" | \"default\";\n  /** Hides the analyte name, for a grid whose column header already carries it. */\n  hideAnalyte?: boolean;\n  /** Opens the specimen chain. Makes the value a button; inert without it. */\n  onOpenReport?: (value: ResultValueData) => void;\n}\n\nfunction ResultValueImpl({\n  value: data,\n  now,\n  density = \"default\",\n  hideAnalyte = false,\n  onOpenReport,\n  className,\n  ...rest\n}: ResultValueProps) {\n  const interpretation = resolveInterpretation(data);\n  const delta = resolveDelta(data);\n  const range = formatRange(data.range);\n  const sentence = describeResult(data, now);\n\n  /*\n   * One accessible name for the whole thing, and everything inside hidden\n   * from the tree.\n   *\n   * The alternative is six labelled nodes, which a screen reader reads as six\n   * fragments with pauses between them — and the clinical meaning of a result\n   * is in the *combination*: 6.8 is unremarkable until you hear the unit, the\n   * range and the word \"critical\" in the same breath.\n   */\n  const shell = {\n    className: cn(\"zb-rv\", className),\n    \"data-zb-result\": \"\",\n    \"data-zb-density\": density,\n    \"data-zb-absent\": data.absent ?? undefined,\n    \"data-zb-interpretation\": interpretation?.step ?? undefined,\n    \"data-zb-status\": data.status ?? undefined,\n    \"aria-label\": sentence,\n    ...rest,\n  };\n\n  const body = data.absent ? <Absent data={data} /> : <Present data={data} />;\n\n  const qualifiers = (\n    <>\n      {/* The range, or the explicit absence of one. Never nothing: a number\n          with nothing beside it reads as in range, and that is the second of\n          the four failures. */}\n      {range ? (\n        <span className=\"zb-rv__range\">\n          {range}\n          {data.range?.appliesTo ? (\n            <span className=\"zb-rv__applies\"> {data.range.appliesTo}</span>\n          ) : null}\n        </span>\n      ) : data.noRangeReason ? (\n        <span className=\"zb-rv__range\" data-zb-norange=\"\">\n          {data.noRangeReason}\n        </span>\n      ) : null}\n\n      {data.status ? (\n        <ClinicalStatus\n          scale=\"result-status\"\n          step={data.status}\n          shape=\"chip\"\n          density=\"compact\"\n          aria-hidden=\"true\"\n        />\n      ) : null}\n\n      {data.provenance && data.provenance !== \"lab\" ? (\n        <span className=\"zb-rv__provenance\" data-zb-provenance={data.provenance}>\n          {PROVENANCE_LABEL[data.provenance]}\n        </span>\n      ) : null}\n\n      {now && data.resultedAt ? <Age at={data.resultedAt} now={now} /> : null}\n\n      {(data.notes ?? []).map((note) => (\n        <span key={note} className=\"zb-rv__note\">\n          {note}\n        </span>\n      ))}\n    </>\n  );\n\n  const content = (\n    <>\n      <span className=\"zb-rv__line\" aria-hidden=\"true\">\n        {hideAnalyte ? null : <span className=\"zb-rv__analyte\">{data.analyte}</span>}\n        {body}\n        {interpretation && !data.absent ? (\n          <ClinicalStatus\n            scale=\"criticality\"\n            step={interpretation.step}\n            shape=\"chip\"\n            density=\"compact\"\n            aria-hidden=\"true\"\n          />\n        ) : null}\n        {delta && delta.direction !== \"flat\" ? (\n          <span className=\"zb-rv__delta\" data-zb-direction={delta.direction}>\n            {delta.direction === \"up\" ? \"▲\" : \"▼\"} {Math.abs(delta.change)}\n            <span className=\"zb-rv__delta-since\"> / {describeElapsed(delta.sinceMs)}</span>\n          </span>\n        ) : null}\n      </span>\n\n      {/*\n        The superseded value, struck through, with when it changed.\n        Not a badge saying \"corrected\": the hazard is the clinician's memory of\n        the old number, and a badge does not address it. Showing the old number\n        with a line through it does.\n      */}\n      {data.superseded ? (\n        <span className=\"zb-rv__superseded\" aria-hidden=\"true\">\n          <s>{data.superseded.value}</s> superseded {data.superseded.at}\n        </span>\n      ) : null}\n\n      <span className=\"zb-rv__qualifiers\" aria-hidden=\"true\">\n        {qualifiers}\n      </span>\n    </>\n  );\n\n  if (onOpenReport) {\n    return (\n      <button\n        {...(shell as React.ButtonHTMLAttributes<HTMLButtonElement>)}\n        type=\"button\"\n        onClick={() => onOpenReport(data)}\n      >\n        {content}\n      </button>\n    );\n  }\n\n  // `role=\"group\"` with a label rather than a focusable div: there is nothing\n  // to activate, so there must be no tab stop.\n  return (\n    <div {...(shell as React.HTMLAttributes<HTMLDivElement>)} role=\"group\">\n      {content}\n    </div>\n  );\n}\n\nfunction Present({ data }: { data: ResultValueData }) {\n  return (\n    <span className=\"zb-rv__value\">\n      {data.comparator ? <span className=\"zb-rv__comparator\">{data.comparator}</span> : null}\n      <span className=\"zb-rv__number\">{data.value}</span>\n      {data.unit ? <span className=\"zb-rv__unit\">{data.unit}</span> : null}\n    </span>\n  );\n}\n\n/**\n * Absence, in the slot the number would occupy.\n *\n * A word and a sentence, never an em dash — which is indistinguishable from a\n * rendering bug, and which asks the reader to guess between \"nobody ordered\n * it\", \"the specimen haemolysed\" and \"you are not allowed to see it\".\n */\nfunction Absent({ data }: { data: ResultValueData }) {\n  // Only ever rendered when `absent` is set; the guard keeps that true at the\n  // type level rather than by assertion.\n  if (!data.absent) return null;\n  const copy = ABSENCE[data.absent];\n  return (\n    <span className=\"zb-rv__value\" data-zb-absent-value=\"\">\n      <span className=\"zb-rv__absent-word\">{copy.short}</span>\n      <span className=\"zb-rv__absent-detail\">{data.absentDetail ?? copy.detail}</span>\n    </span>\n  );\n}\n\nfunction Age({ at, now }: { at: string; now: string }) {\n  const elapsed = Date.parse(now) - Date.parse(at);\n  if (!Number.isFinite(elapsed) || elapsed < 0) return null;\n  return <span className=\"zb-rv__age\">{describeElapsed(elapsed)} ago</span>;\n}\n\n/**\n * Memoised on identity and version.\n *\n * A results grid re-renders on every filter keystroke, and each row does range\n * arithmetic, delta arithmetic and sentence composition. Comparing the whole\n * object would defeat that — hosts rebuild these from a query response, so the\n * reference changes every time even when nothing did. `versionId` is the\n * field that actually moves when the record does.\n */\nexport const ResultValue = React.memo(ResultValueImpl, (a, b) => {\n  return (\n    a.value.id === b.value.id &&\n    a.value.versionId === b.value.versionId &&\n    a.now === b.now &&\n    a.density === b.density &&\n    a.hideAnalyte === b.hideAnalyte &&\n    a.onOpenReport === b.onOpenReport &&\n    a.className === b.className\n  );\n});\n\nResultValue.displayName = \"ResultValue\";\n"
    }
  ]
}
