Clara Form
React medical form: the ClaraForm component, controlled with a hook
Embed a medical form in React with @clara.care/form: the ClaraForm component, the useClaraForm hook, validation, theming and FHIR on submit.
Loading the preview…
import { useState } from 'react';
import { ClaraForm, useClaraForm } from '@clara.care/form';
// Inline template (the same JSON Clara Platform stores). With a publishable key you
// can pass templateId="…" apiKey="…" instead and load it from the API.
const template = {
id: 'visit-demo',
name: 'Visit',
sections: [
{ key: 'visit', label: 'Visit', fields: [
{ id: 'reason', type: 'text', label: 'Reason for visit', required: true },
{ id: 'notes', type: 'textarea', label: 'Notes' },
] },
{ key: 'vitals', label: 'Vital signs', fields: [
{ id: 'temperature', type: 'number', label: 'Temperature (°C)', min: 30, max: 45, step: 0.1 },
{ id: 'blood_pressure', type: 'text', label: 'Blood pressure', placeholder: '120/80', pattern: '\\d{2,3}/\\d{2,3}' },
] },
],
};
export function Visit() {
// Controlled mode: the hook owns the values; formProps = { value, onChange, onReady }.
const form = useClaraForm({ template, defaultValues: { reason: 'Annual check-up' } });
const [fhir, setFhir] = useState(null);
return (
<section style={{ '--clara-form-primary': '#2a4276' }}>
<button type="button" onClick={() => form.setValue('temperature', 37.2)}>
Set 37.2 °C from the parent
</button>
<ClaraForm
{...form.formProps}
lang="en"
theme="light"
submitLabel="Save visit"
onSubmit={(_values, ctx) => setFhir(ctx.fhir)}
onInvalid={(errors) => console.info('Fields with errors:', Object.keys(errors))}
/>
<h2>Values</h2>
<pre>{JSON.stringify(form.values, null, 2)}</pre>
{fhir && (
<>
<h2>FHIR QuestionnaireResponse</h2>
<pre>{JSON.stringify(fhir, null, 2)}</pre>
</>
)}
</section>
);
}
<!-- Preview: the same form as the React snippet, rendered with the <clara-form> web component
(no bundler needed in this sandbox). In React, use the ClaraForm component instead. -->
<script src="https://cdn.clara.care/form/latest.js"></script>
<main class="demo demo-split">
<section>
<div class="demo-actions">
<button type="button" id="set-temp">Set 37.2 °C from the parent</button>
</div>
<clara-form lang="en" theme="light" submit-label="Save visit"></clara-form>
</section>
<section class="demo-out" aria-labelledby="values-h">
<h2 id="values-h">Values</h2>
<pre id="values" aria-live="polite"></pre>
<h2>FHIR QuestionnaireResponse</h2>
<pre id="fhir">Submit the form to see the resource.</pre>
</section>
</main>
// The same template as the React snippet.
const template = {
id: 'visit-demo',
name: 'Visit',
sections: [
{ key: 'visit', label: 'Visit', fields: [
{ id: 'reason', type: 'text', label: 'Reason for visit', required: true },
{ id: 'notes', type: 'textarea', label: 'Notes' },
] },
{ key: 'vitals', label: 'Vital signs', fields: [
{ id: 'temperature', type: 'number', label: 'Temperature (°C)', min: 30, max: 45, step: 0.1 },
{ id: 'blood_pressure', type: 'text', label: 'Blood pressure', placeholder: '120/80', pattern: '\\d{2,3}/\\d{2,3}' },
] },
],
};
const form = document.querySelector('clara-form');
form.template = template;
form.defaultValues = { reason: 'Annual check-up' };
const values = document.querySelector('#values');
const fhir = document.querySelector('#fhir');
// In React, useClaraForm keeps these values in state; here clara-change does the same job.
form.addEventListener('clara-ready', () => { values.textContent = JSON.stringify(form.getValues(), null, 2); });
form.addEventListener('clara-change', (e) => { values.textContent = JSON.stringify(e.detail.values, null, 2); });
form.addEventListener('clara-submit', (e) => { fhir.textContent = JSON.stringify(e.detail.fhir, null, 2); });
form.addEventListener('clara-invalid', (e) => console.info('Fields with errors:', Object.keys(e.detail.errors)));
// form.setValue('temperature', 37.2) in the hook; setFieldValue on the element.
document.querySelector('#set-temp').addEventListener('click', () => form.setFieldValue('temperature', 37.2));
Every value on this page is synthetic. Never paste real patient data into a public page. The React snippet of some examples is shown as a reference; the preview runs the web component version.
How it works
Install
npm install @clara.care/form react react-dom. React 18 or newer is a peer dependency. ESM and CommonJS, with TypeScript types.
Render ClaraForm
Pass template (inline) or templateId plus apiKey. onSubmit receives (values, { fhir, json, files }); onInvalid receives the errors by field.
Control the values
useClaraForm({ template, defaultValues }) returns formProps to spread on the component, plus values, setValue and toFhir for the rest of your UI.
Theme it
theme light, dark or auto, and CSS variables such as --clara-form-primary on any ancestor. Your own fonts: the package loads none by default.
@clara.care/form ships a React component, ClaraForm, and a hook, useClaraForm, that keeps the values in your component so the rest of your app can read and set them. The template can be inline or loaded by id with a publishable key; the submit handler receives the values together with a labeled JSON and a FHIR QuestionnaireResponse.
The React snippet is the reference code of this page. The live preview runs the equivalent <clara-form> web component, which needs no build step inside this sandbox; both render the same form from the same template.
Controlled or uncontrolled
Uncontrolled: give defaultValues and read the result in onSubmit. Controlled: useClaraForm owns the values; form.setValue('temperature', 37.2) from a button of yours updates the field, and form.values re-renders whatever you show next to the form. A ref (ClaraFormHandle) adds validate(), submit(), reset(), toJSON() and toFhir().
Other frameworks
Vue, Angular, Svelte or plain HTML use the same package through import '@clara.care/form/element', which registers <clara-form>. The web component example on this site shows the attributes and events.
Questions about this example
Does ClaraForm work with Next.js?
Yes. Render it in a client component ('use client'), since it uses browser APIs. Templates by id are fetched in the browser with your publishable key; inline templates need no network.
Can I use React Hook Form or my own state?
The component works in controlled mode with value and onChange, so you can keep the values in any store. useClaraForm is a convenience that wires those two props for you.
How do I lock fields the user must not edit?
lockedFields takes a list of field keys. Those inputs are disabled for the person while values from your code or from a live consultation still apply.
Where does the FHIR resource come from?
onSubmit's second argument includes fhir, a FHIR R4 QuestionnaireResponse with status completed. toFhir() on the hook or the ref builds one at any moment with the options you pass.
Why does the preview use the web component?
The sandbox on this page runs plain HTML and JavaScript without a bundler, so JSX cannot execute here. The web component version renders the same template with the same package; the React snippet is the reference for your app.
Planning an integration?
Tell us about your system and your forms. We run small pilots with technology partners.


