|
1 | | -# FHIRPath Editor |
| 1 | +# Aidbox Forms Renderer |
2 | 2 |
|
3 | | -A React component library for editing and evaluating FHIRPath expressions with real-time feedback. |
4 | | - |
5 | | -## Online Demo |
6 | | - |
7 | | -Try the interactive demo at [https://healthsamurai.github.io/aidbox-forms-renderer/](https://healthsamurai.github.io/aidbox-forms-renderer/) |
8 | | - |
9 | | -## Installation |
10 | | - |
11 | | -```bash |
12 | | -npm install aidbox-forms-renderer |
13 | | -# or |
14 | | -yarn add aidbox-forms-renderer |
15 | | -``` |
16 | | - |
17 | | -## Usage |
18 | | - |
19 | | -The library provides an `Editor` component for working with FHIRPath expressions: |
20 | | - |
21 | | -### Uncontrolled Component (with defaultValue) |
22 | | - |
23 | | -```tsx |
24 | | -import { Editor } from "aidbox-forms-renderer"; |
25 | | -import r4 from "fhirpath/fhir-context/r4"; |
26 | | - |
27 | | -function MyFhirPathEditor() { |
28 | | - // FHIRPath expression to evaluate |
29 | | - const defaultExpr = "(1 + 2) * 3"; |
30 | | - const handleChange = (newValue) => |
31 | | - console.log("Expression changed:", newValue); |
32 | | - |
33 | | - // Simple FHIR resource to evaluate FHIRPath against |
34 | | - const data = { |
35 | | - resourceType: "Patient", |
36 | | - id: "example", |
37 | | - name: [ |
38 | | - { |
39 | | - family: "Smith", |
40 | | - given: ["John"], |
41 | | - }, |
42 | | - ], |
43 | | - birthDate: "1970-01-01", |
44 | | - }; |
45 | | - |
46 | | - const fhirSchema = []; // Replace with actual schema data |
47 | | - |
48 | | - return ( |
49 | | - <Editor |
50 | | - defaultValue={defaultExpr} |
51 | | - onChange={handleChange} |
52 | | - data={data} |
53 | | - schema={fhirSchema} |
54 | | - model={r4} |
55 | | - /> |
56 | | - ); |
57 | | -} |
58 | | -``` |
59 | | - |
60 | | -### Controlled Component (with value) |
| 3 | +Minimal React renderer for HL7® FHIR® Questionnaires. State is always a canonical `QuestionnaireResponse`, so the data you display is the data you can submit. |
61 | 4 |
|
62 | 5 | ```tsx |
63 | | -import { Editor } from "aidbox-forms-renderer"; |
64 | | -import r4 from "fhirpath/fhir-context/r4"; |
| 6 | +import { Renderer, type Questionnaire, type QuestionnaireResponse } from "aidbox-forms-renderer"; |
65 | 7 | import { useState } from "react"; |
66 | 8 |
|
67 | | -function MyFhirPathEditor() { |
68 | | - // FHIRPath expression state |
69 | | - const [expression, setExpression] = useState("(1 + 2) * 3"); |
70 | | - |
71 | | - // Simple FHIR resource to evaluate FHIRPath against |
72 | | - const data = { |
73 | | - resourceType: "Patient", |
74 | | - id: "example", |
75 | | - name: [ |
76 | | - { |
77 | | - family: "Smith", |
78 | | - given: ["John"], |
79 | | - }, |
80 | | - ], |
81 | | - birthDate: "1970-01-01", |
82 | | - }; |
| 9 | +const questionnaire: Questionnaire = { |
| 10 | + resourceType: "Questionnaire", |
| 11 | + item: [ |
| 12 | + { linkId: "first", text: "First name", type: "string", required: true }, |
| 13 | + { linkId: "consent", text: "Consent to treatment", type: "boolean" }, |
| 14 | + ], |
| 15 | +}; |
83 | 16 |
|
84 | | - const fhirSchema = []; // Replace with actual schema data |
| 17 | +export function IntakeForm() { |
| 18 | + const [response, setResponse] = useState<QuestionnaireResponse | null>(null); |
85 | 19 |
|
86 | 20 | return ( |
87 | | - <Editor |
88 | | - value={expression} |
89 | | - onChange={setExpression} |
90 | | - data={data} |
91 | | - schema={fhirSchema} |
92 | | - model={r4} |
| 21 | + <Renderer |
| 22 | + questionnaire={questionnaire} |
| 23 | + initialResponse={response ?? undefined} |
| 24 | + onChange={setResponse} |
| 25 | + onSubmit={setResponse} |
93 | 26 | /> |
94 | 27 | ); |
95 | 28 | } |
96 | 29 | ``` |
97 | 30 |
|
98 | | -## Props |
99 | | - |
100 | | -| Prop | Type | Required | Description | |
101 | | -| -------------- | ----------------------- | -------- | ----------------------------------------------- | |
102 | | -| `value` | string | No | Current FHIRPath expression (controlled mode) | |
103 | | -| `defaultValue` | string | No | Initial FHIRPath expression (uncontrolled mode) | |
104 | | -| `onChange` | (value: string) => void | No | Callback for expression changes | |
105 | | -| `data` | any | Yes | The context data to evaluate FHIRPath against | |
106 | | -| `variables` | Record<string, any> | No | External bindings available to expressions | |
107 | | -| `schema` | FhirSchema[] | Yes | FHIR schema definitions for validation | |
108 | | -| `model` | Model | Yes | FHIR version model data | |
109 | | -| `debug` | boolean | No | Enable debug mode | |
110 | | - |
111 | | -## Component Modes |
112 | | - |
113 | | -The Editor component supports both controlled and uncontrolled modes: |
114 | | - |
115 | | -- **Controlled Mode**: Provide the `value` prop and handle changes with `onChange`. |
116 | | -- **Uncontrolled Mode**: Provide only the `defaultValue` prop (initial value) and optionally handle changes with `onChange`. |
117 | | - |
118 | | -Note: Do not provide both `value` and `defaultValue` at the same time. If both are provided, `defaultValue` will be ignored and a warning will be logged to the console. |
119 | | - |
120 | | -## Features |
121 | | - |
122 | | -- Low-code environment for developing and testing FHIRPath expressions |
123 | | -- Interactive visual editor with instant feedback |
124 | | -- Real-time evaluation against FHIR resources |
125 | | -- Support for external variable bindings |
126 | | -- Schema-based validation with autocomplete suggestions |
127 | | -- Syntax highlighting and error detection |
128 | | -- Reduces development time for complex FHIRPath queries |
| 31 | +Useful scripts: `npm run dev` (playground), `npm run build` (type-check + bundle), `npm test`, `npm run lint`. |
129 | 32 |
|
130 | | -## Todo |
131 | | -- [x] Support $total special variable |
132 | | -- [x] Support type evaluation of `aggregate` call |
133 | | -- [ ] Support dynamic index access |
134 | | -- [ ] Support unit token |
| 33 | +Architecture and rationale live as doc comments in the source (`lib/state/`, `lib/form-provider.tsx`, `lib/questionnaire-renderer.tsx`). |
0 commit comments