@embeddables/forms 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -13,6 +13,57 @@ and app user id, durable R2 writes run automatically on successful `.set()` and
13
13
  npm install @embeddables/forms @embeddables/core
14
14
  ```
15
15
 
16
+ For React apps, also install `react` (peer dependency) and wrap your tree in
17
+ `EmbeddablesProvider` from `@embeddables/core/react`. The React example below
18
+ also imports `@embeddables/analytics/react`, so install `@embeddables/analytics`
19
+ too if you want the auto-tracking shown there. If omitted, remove the
20
+ `useAnalytics` import, hook call, and `analyticsInstance` option.
21
+
22
+ ## React
23
+
24
+ Import hooks from `@embeddables/forms/react`:
25
+
26
+ ```tsx
27
+ import { EmbeddablesProvider } from '@embeddables/core/react'
28
+ import { useAnalytics } from '@embeddables/analytics/react'
29
+ import { useForms, useForm, useFormField } from '@embeddables/forms/react'
30
+
31
+ function SignupField() {
32
+ const analytics = useAnalytics()
33
+ useForms({ analyticsInstance: analytics ?? undefined })
34
+
35
+ const schema = {
36
+ id: 'signup',
37
+ fields: [{ key: 'email', label: 'Email', type: 'email', validations: { required: true } }],
38
+ } as const
39
+
40
+ const { form } = useForm(schema)
41
+ const { value, error, setValue, onBlur } = useFormField(form, 'email')
42
+
43
+ return (
44
+ <label>
45
+ Email
46
+ <input value={value ?? ''} onChange={(event) => setValue(event.target.value)} onBlur={() => void onBlur()} />
47
+ {error?.[0]}
48
+ </label>
49
+ )
50
+ }
51
+
52
+ export function App() {
53
+ return (
54
+ <EmbeddablesProvider config={{ projectId: 'proj_…', forms: [], experiments: [] }}>
55
+ <SignupField />
56
+ </EmbeddablesProvider>
57
+ )
58
+ }
59
+ ```
60
+
61
+ - `useForms` returns `null` until Core is ready inside `EmbeddablesProvider`.
62
+ - Pass `analyticsInstance` explicitly when you want auto-tracking on `.set()` / `.submit()`. Do not also call `trackEvent` for the same submission or you will double-count `form:submitted`.
63
+ - `useFormField` uses commit-on-blur: local typing updates draft state; `form.set()` runs on blur.
64
+ - One live `FormInstance` per `schema.id` is shared across hooks in the same client.
65
+ - Imperative consumers can also call `form.subscribe(listener)` to observe value and error changes.
66
+
16
67
  ## Quick start
17
68
 
18
69
  ```typescript