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…

React
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>
  );
}

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

  1. Install

    npm install @clara.care/form react react-dom. React 18 or newer is a peer dependency. ESM and CommonJS, with TypeScript types.

  2. Render ClaraForm

    Pass template (inline) or templateId plus apiKey. onSubmit receives (values, { fhir, json, files }); onInvalid receives the errors by field.

  3. Control the values

    useClaraForm({ template, defaultValues }) returns formProps to spread on the component, plus values, setValue and toFhir for the rest of your UI.

  4. 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.

Cookie settings

Choose which optional cookies Clara Care may use. They stay off until you turn them on, and you can change your choice at any time from Cookie settings in the footer. Privacy policy